Now available on HACS
Thought I’d share this because:
- a - I haven’t seen anything similar, in spite of a lot of googling
- b - I’m pretty chuffed with it because I hate python but it actually works!
Lets say you want to add a couple of sensors to homeassistant that countdown to some significant dates. First make sure that you have python_script enabled in your configuration by adding
python_script:
to your configuration.yaml (or whichever of your packages suits you best)
The way that python_scripts work with homeassistant is that you have to run them whenever you want the results, so create an automation that runs the script as many times as you need for the number of sensors you want. Each sensor requires:
- name: NAME_OF_DATE
- type: TYPE_OF_DATE
- date: DD/MM/YYYY_OF DATE
examples:
- name: John
- type: birthday
- date: 17/08/1971
or
- name: Our wedding
- type: anniversary
- date: 14/02/1994
So if those were the two you wanted, an example automation to create and refresh the sensors daily would be:
automation:
- alias: Reminder - Refresh date countdown sensors
initial_state: on
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
The sensors will then appear next time you restart homeassistant and refresh at midnight. Each sensor is given the following automatically:
entity_id: sensor.<type>_<name>
friendly_name: <name> 's <type>
state: <Days to the date from today>
years: <Number of years it will be>
So, the two 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
years: However old John will be on his next birthday
sensor.anniversary_our_wedding
friendly_name: Our wedding anniversary
state: However many days to 14th February
years: How many years you will have been married on that day
You can then use the countdowns to send you reminders on the approach to the dates, or wish people happy birthday when they come home via TTS etc. Beware that you’ll probably want a wait_template so the automation doesn’t go off at midnight!!
Hope this is of some use to people