Countdown sensor

Hi all, quick python_script to display the number of days until some future date, which I update using an automation at midnight daily. Enjoy!

"""
Script to create a sensor displaying the number of days until some event.
Automate to update every day.
"""

EVENT = 'renew_letsencrypt'
YEAR = 2018
MONTH = 2
DAY = 8

day_of_interest = datetime.datetime(YEAR, MONTH, DAY)
now = datetime.datetime.now()
diff = day_of_interest - now

hass.states.set('sensor.' + EVENT, diff.days,{
    'unit_of_measurement': 'days',
    'friendly_name': 'Days until renew LetsEncrypt',
    'icon': 'mdi:calendar'
})

4 Likes

That looks cool however since I’m a complete noob… can you please show how you:
a) set the date that it is counting down to,
b) update it via your automation (I’m guessing that will become clearer once I understand ‘a’)

I guess I need to further understand the python_script component a bit more.

Hi Dave, set the variables YEAR etc, and us the Automations Editor on the front end to create the automation, triggering on a time.
Cheers!

How do you write/edit this in the front end editor?

Only the automation is edited via front end

I also have a script editor :neutral_face:

https://home-assistant.io/docs/scripts/editor/

Unfortunately that only handles yaml scripts, not python scripts. You may be interested in this thread in the future.
Cheers

Man great work… Is there a way to have the script runn multiple timers without have to create mulitple py scripts

To do that, just create a list of dicts (1 dict per timer) then just iterate the list :+1:

You could also use the HASS Configurator.

1 Like

Looks very exciting. But I’m having some trouble getting it to work :disappointed_relieved:

Copied the configurator.py and settings.conf to my config folder. Made configurator.py executable, adding the component in my yaml file. I got the menu item now but nothing shows when I click it.

Guess it has to be some parameters that needs to be changed. But it doesn’t seem obvious to me what needs a change.

/Kaspar

You don’t necessarily need to access it via the HASS UI while getting it to work. Initially you can also just use the IP address of the machine the configurator is running on, together with the port. So http://192.168.1.23:3218 for example. Or https of course if you have set a certificate and key. And of course it also has to be started (sudo ./configurator.py or have a look at the daemonizing section of the documentation).

Well… bummer. I get this error when trying to run the py script:

pi@hassbian:/home/homeassistant/.homeassistant $ sudo ./configurator.py
./configurator.py: 8: ./configurator.py: Syntax error: newline unexpected

So I guess I’m not that lucky today :slight_smile:

Try this command to fetch the configurator.py again:
curl -O https://raw.githubusercontent.com/danielperna84/hass-configurator/master/configurator.py
Then set the permissions again. The error you have described usually is a result of the content of the URL not being loaded properly.

Now it works… Can you tell me what the difference was from the file I’ve downloaded to the curl-thing from your link?

If you still have it compare the content. I can’t look on your computer. :wink:

Fair enough :smile:

Is there some security build into this configurator or is my connection wide open now I’ve opened the 3218 port in my router?

As mentioned in the documentation, you can set CREDENTIALS, ALLOWED_NETWORKS, BANNED_IPS and BANLIMIT.

Is it possible to have an annual date.
eg. every occurring 24/12 regardless of year :slight_smile:

Is it possible to add a list of dates? once one date is “hit”, it counts down to the next date in the list?