A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

Overview

Date Countdown

A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

hacs_badge

Important note

I no longer use homeassistant so if this script stops being compatible with homeassistant that will be the end of the line. That said, it hasn't needed any maintenence in over a year and was working well so it will probably be fine to use for a long while yet.

How it works

This script creates a sensor that a counts down to the next occurrence of a date, like a birthday or anniversary and gives the number of years as an attribute.

You can optionally reverse the counter so it counts up from the last occurrence of a date, such as when you quit smoking.

Requires python_script: to be enabled in your configuration

Installation

Copy the Python script in to your /config/python_scripts directory or install via HACS.

Script arguments

key required type description
name: True string Name of the date (eg. John)
type: True string Type of date (eg. Birthday)
date: True string Date, in format DD/MM/YYYY
friendly_name: False string Display name of the sensor
append_year: False boolean Appends the number of years to the friendly name
icon: False string Icon of the sensor, defaults to 'mdi:calendar-star'
reverse: False boolean Reverses the sensor to count up instead of down. (Defaults to False)

The date can be in the future if you want to countdown to the date itself, and then the anniversaries thereafter.

Usage

Each sensor requires:

name: NAME_OF_DATE
type: TYPE_OF_DATE
date: DD/MM/YYYY_OF DATE

You can also customize the sensor icon and friendly names :

icon: "mdi:ICON_OF_DATE"
friendly_name: FRIENDLY_NAME_OF_DATE
append_year: True

And you can reverse the sensor so it counts up from a date:

reverse: True

examples:

name: John
type: birthday
date: 17/08/1971

or

name: Our wedding
type: anniversary
date: 14/02/1994
icon: "mdi:ring"
append_year: True

or

name: Quit Smoking
type: celebration
date: 01/10/2008
reverse: True

Generated sensors

Each sensor is given the following automatically:

entity_id: sensor.<type>_<name>
friendly_name: <name> <type>
state: <Days to the date from today>
nextoccur: <Date of next occurance>
years: <Number of years it will be>

So, the three sensors we created above would come out as:

sensor.birthday_john
friendly_name: John’s birthday
state: However many days it is until 17th August
nextoccur: 17/08/YYYY (either this year or next year as appropriate)
years: However old John will be on his next birthday

sensor.anniversary_our_wedding
friendly_name: Our wedding anniversary (20)
state: However many days to 14th February
nextoccur: 14/02/YYYY (either this year or next year as appropriate)
years: How many years you will have been married on that day

sensor.celebration_quit_smoking
friendly_name: Quit smoking celebration
state: However many days SINCE 1st October
nextOccur: 01/10/YYYY (either this year or next year as appropriate)
years: How many years SINCE you quit smoking

Note that if the type is 'birthday' the sensor will automatically add an apostrophe.

Example configuration.yaml entry

An example automation to create and refresh the above three sensors daily would be:

automation:
  - alias: Reminder - Refresh date countdown sensors
    trigger:
      - platform: time
        at: '00:00:01'
      - platform: homeassistant
        event: start
    action:
      - service: python_script.date_countdown
        data:
          name: John
          type: birthday
          date: 17/08/1971
      - service: python_script.date_countdown
        data:
          name: Our wedding
          type: anniversary
          date: 14/02/1994
          icon: "mdi:ring"
          append_year: True
      - service: python_script.date_countdown
        data:
          name: Quit smoking
          type: celebration
          date: 01/10/2008
          reverse: True

Example automation

An example automation to remind you of an event 7 days before it occurs would be:

automation:
  - alias: Reminder - John's birthday is coming up
    trigger:
      - platform: state
        entity_id: sensor.birthday_john
        to: '7'
    action:
      - wait_template: "{{ states('sensor.time') == '10:00' }}"
      - service: notify.notify
        data:
          message: "John's birthday is only a week away!"

Example Lovelace representation

Utilising the attributes provided and the custom lovelace card for adding secondary info to an entity row.

type: entities
show_header_toggle: false
title: Our Events
entities:
  - entity: sensor.anniversary_our_wedding
    secondary_info: '[[ {entity}.attributes.nextoccur ]]  ( [[ {entity}.attributes.years ]] Years )'
    type: 'custom:secondaryinfo-entity-row'

Will provide the following lovelace representation:

Lovelace example

Credits

Thanks to @myle for the idea for the extra attributes and the lovelace presentation.

Thanks to AJax2012 for the PR that caters for future dates, and makes the apostrophes in the friendly names only append to birthday sensors.

Thanks to Cyrosy for adding customization options and fixing some bugs.

Comments
  • Æ/Ø/Å breaks sensors

    Æ/Ø/Å breaks sensors

    Thanks for a awesome script.

    When using Danish unique letters (Æ, Ø & Å) the sensor breaks. I'm not sure if there exists a official way to handle that issue regarding writing codes, but Home Assistant transform them to: æ: ae ø: o å: aa

    Config

    ---
    # https://github.com/mf-social/ps-date-countdown
    alias: "[System] Date Countdown"
    trigger:
      - platform: time
        at: '00:00:01'
      - platform: homeassistant
        event: start
    action:
      - service: python_script.date_countdown
        data:
          name: Juleaften
          type: anniversary
          date: 24/12/1981
          icon: "mdi:pine-tree"
      - service: python_script.date_countdown
        data:
          name: 'Nytårsaften'
          type: anniversary
          date: 31/12/1981
          icon: "mdi:pine-tree"
    

    Log

    Log Details (ERROR)
    Logger: homeassistant.components.python_script.date_countdown.py
    Source: core.py:736
    Integration: Python Scripts (documentation, issues)
    First occurred: 4:01:51 PM (1 occurrences)
    Last logged: 4:01:51 PM
    
    Error executing script: Invalid entity id encountered: sensor.anniversary_nytårsaften. Format should be <domain>.<object_id>
    Traceback (most recent call last):
      File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
        exec(compiled.code, restricted_globals)
      File "date_countdown.py", line 50, in <module>
      File "/usr/src/homeassistant/homeassistant/core.py", line 954, in set
        context,
      File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 435, in result
        return self.__get_result()
      File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
        raise self._exception
      File "/usr/src/homeassistant/homeassistant/util/async_.py", line 53, in run_callback
        future.set_result(callback(*args))
      File "/usr/src/homeassistant/homeassistant/core.py", line 994, in async_set
        state = State(entity_id, new_state, attributes, last_changed, None, context)
      File "/usr/src/homeassistant/homeassistant/core.py", line 736, in __init__
        f"Invalid entity id encountered: {entity_id}. "
    homeassistant.exceptions.InvalidEntityFormatError: Invalid entity id encountered: sensor.anniversary_nytårsaften. Format should be <domain>.<object_id>
    

    I'm aware of the use of friendly_name, but many names in Denmark fx have special letters.

    opened by allanpersson 18
  • Negative number of years

    Negative number of years

    Hi there,

    since last update for some reason the "years" attribute became a negative value. image

    I have checked now the configuration and documentation to see if there was something new I wasn't aware of but it doesn't look like.

    The config it's fairly simple, the only difference is that I am using the secret file to store the dates:

      - service: python_script.date_countdown
        data:
          name: Andrea
          type: birthday
          date: !secret andrea_birthday
    

    Any clue?

    Thanks :) Andrea

    opened by SeLLeRoNe 12
  • Please provide more detailed example

    Please provide more detailed example

    It’s not clear to me exactly how I define the birthdays... I see from the readme which fields you expect but what’s not clear to me is where I define these... Do they go in the configuration.yaml ?

    opened by johntdyer 12
  • Error python 3.9.0

    Error python 3.9.0

    HA 0.116.4 with python 3.9.0 Reminder - Refresh date countdown sensors: Error executing script. Unexpected error for call_service at pos 1: Unknown slice type: <ast.Constant object at 0x51578490> While executing automation automation.reminder_refresh_date_countdown_sensors

    opened by MARIOP69 7
  • Feature Request: Ability to add 'years' to sensors friendly_name

    Feature Request: Ability to add 'years' to sensors friendly_name

    Hello, I think it would be great to allow appending 'years' to a sensors friendly_name, so for example... John's Birthday - 122 days would become... John's Birthday (50) - 122 days This way it's easier to spot milestone events

    Cheers, James

    opened by jimbob1001 6
  • Small Updates

    Small Updates

    Just made some small updates:

    • If the date of event is more than 1 year in the future (like 2021 if current year is 2019), use the event date to calculate distance to event.
    • add apostrophe to the friendly name of the sensor only if the event type is birthday. easy to change if desired.
    opened by AJax2012 2
  • Example automation is confusing...

    Example automation is confusing...

    Having

          - platform: time
            at: '00:00:01'
    

    In the example automation means the sensor isn't created until that time. Best to remove that and have the sensor created on HA restart. Helpful for noobs like me.

    opened by rv-kip 1
  • fix: past events won't get negative year anymore

    fix: past events won't get negative year anymore

    With this commit, years will never be negative again.

    • If the date is in the past, the diffrence between the next occurence year and the initial year is the number of years
    • If the date is in the future, it has never happened so years will always be 0

    Should fix #8

    opened by cyrosy 0
Releases(v6.0)
Owner
Marc Forth
Marc Forth
Yet another automation project because a smart light is more than just on or off.

Automate home Yet another home automation project because a smart light is more than just on or off. Overview When talking about home automation there

Maja Massarini 62 Oct 10, 2022
A Raspberry Pi Pico plant sensor hub coded in Micropython

plantsensor A Raspberry Pi Pico plant sensor hub coded in Micropython I used: 1x Raspberry Pi Pico - microcontroller 1x Waveshare Pico OLED 1.3 - scre

78 Sep 20, 2022
Watson-Assistant with integration capabilities

Watson-Assistant-Integration Watson-Assistant with integration capabilities "main.py" should be deployed as Cloud Function (Action) on IBM Cloud. For

Sergey Usachev 1 Dec 20, 2021
Python information display framework aimed at e-ink devices

My display, using a Raspberry Pi Zero W and Waveshare 6" e-paper hat infodisplay Modular information display framework aimed at e-ink devices. Built u

Niek Blankers 3 Apr 08, 2022
Implemented robot inverse kinematics.

robot_inverse_kinematics Project setup # put the package in the workspace $ cd ~/catkin_ws/ $ catkin_make $ source devel/setup.bash Description In thi

Jianming Han 2 Dec 08, 2022
Examples to accompany the

Examples to accompany the "Raspberry Pi Pico Python SDK" book published by Raspberry Pi Trading, which forms part of the technical documentation in support of Raspberry Pi Pico and the MicroPython po

Raspberry Pi 589 Jan 08, 2023
A ch341dll Wrap is for using in Python 32bits windows to access I2C SPI and MDIO (by GPIO), and Demo with display PC sreen on OLED by i2c or SPI .

ch341dll_wrap_typcal_app A ch341dll Wrap is for using in Python 32bits windows to access I2C SPI and MDIO (by GPIO). In addition, I provided 3 Demo. I

13 Jan 02, 2023
Poupool is an overflow swimming pool control software

Poupool - The swimming pool controller Poupool is a swimming pool control software. It is based on Transitions, Pykka and Paho MQTT. The user interfac

Cyril Jaquier 8 Jul 18, 2022
It is a serial communicator(controller, receiver...), communicate with sensor LP20 which is a laser ranger.

Intro It is a serial communicator(controller, receiver...), communicate with sensor LP20 which is a laser ranger. Its datasheet is contained in this r

3 Sep 19, 2022
3D-printable hexagonal mirror array capable of reflecting sunlight into arbitrary patterns

3D-printable hexagonal mirror array capable of reflecting sunlight into arbitrary patterns

Ben Bartlett 2.3k Dec 30, 2022
Homeautomation system created with Raspberry Pi 3 and Firebase.

Homeautomation System - Raspberry Pi 3 Desenvolvido com Python, Flask com AJAX e Firebase permite o controle local e remoto Itens necessários Raspberr

Joselino Santos 0 Mar 09, 2022
Nordpool_diff custom integration for Home Assistant

nordpool_diff custom integration for Home Assistant Requires https://github.com/custom-components/nordpool Applies non-causal FIR differentiator1 to N

Joonas Pulakka 45 Dec 23, 2022
Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment

Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment. On row of LEDs shows the cost of the hour, the other row the co

Johan Jonk Stenström 3 Sep 08, 2022
Hardware: CTWingSKIT_BC28 Development Toolkit

IoT Portal Monitor Tools hardware: CTWingSKIT_BC28 Development Toolkit serial port driver: ST-LINK hardware development environment: Keli 5 MDK IoT pl

Fengming Zhang 1 Nov 07, 2021
Raspberry Pi Pico and LoRaWAN from CircuitPython

Raspberry Pi Pico and LoRaWAN from CircuitPython Enable LoRaWAN communications on your Raspberry Pi Pico or any RP2040-based board using CircuitPython

Alasdair Allan 15 Oct 08, 2022
Home Assistant custom components MPK-Lodz

MPK Łódź sensor This sensor uses unofficial API provided by MPK Łódź. Configuration options Key Type Required Default Description name string False MP

Piotr Machowski 3 Nov 01, 2022
Philippe 1 Jan 09, 2022
Jarvis: a personal assistant which can help you to manage your system

Jarvis Jarvis is personal AI based assistant which can help you to manage stuff in your computer. This is demo but I decided to make it more better so

2 Jun 02, 2022
Alternative firmware for ESP8266 with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at

Alternative firmware for ESP8266/ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability

Theo Arends 59 Dec 26, 2022
Home Assistant component to handle key atom

KeyAtome Home Assistant component to handle key atom, a Linky-compatible device made by Total/Direct-Energie. Installation Either use HACS (default),

18 Dec 21, 2022