I’ve implemented robmarkle’s python script as a day countdown sensor with an automation:
Now, with exception of the bare minimum VB, I have no coding experience. I see he says to create some “dicts” should you wish to add more dates to the sensor. I googled “python dictionaries” and I get the premise. I tried implementing it and failed miserably.
Robmarkle’s original script:
"""
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'
})
My attempt at defining some more sensors (I appreciate I’m miles off, again this was a shot in the dark):
"""
Script to create a sensor displaying the number of days until some YEAR.
Automate to update every day.
"""
EVENT ={}
EVENT['john'] = 'jbday'
EVENT['kevin'] = 'kbday'
EVENT['wife'] = 'sbday'
EVENT['mot'] = 'motleft'
EVENT['tax'] = 'taxleft'
YEAR = {}
YEAR['john'] = '2018'
YEAR['kevin'] = '2018'
YEAR['wife'] = '2018'
YEAR['mot'] = '2018'
YEAR['tax'] = '2018'
MONTH = {}
MONTH['john'] = '12'
MONTH['kevin'] = '12'
MONTH['wife'] = '12'
MONTH['mot'] = '3'
MONTH['tax'] = '3'
DAY = {}
DAY['john'] = '1'
DAY['kevin'] = '2'
DAY['wife'] = '3'
DAY['mot'] = '4'
DAY['tax'] = '5'
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 EVENT', <--- Not too sure about this part either.
'icon': 'mdi:calendar'
})
events is actually what’s called a tuple. (It’s just Python’s fancy name for an immutable “list”.) In fact, it’s a two dimensional tuple. It’s definitely not a dictionary.
The for loop takes each outside tuple element (which are tuples themselves) in turn, and assigns/expands it to the variables specified (i.e., _, event and event_data, where the variable name _ is typically used when the value isn’t needed. It’s just a convention. You could easily use a variable like name.)
No, it’s just another value in the inside tuples. I put it there because you had it. Strictly speaking, it isn’t needed given what the rest of the script does.
Basically events is just a two-dimensional “array”. You can put whatever you like in it. Just make sure the number of variables you put between for and in in the for-loop is the same as the number of elements in each inside tuple.
BTW, I don’t mind helping like this, but this is really more about learning Python, which is kind of outside the scope of this forum. You should probably find a Python tutorial (there are a zillion of them on the web), or private message me.
would be really cool if one could simply jot in the birthday and year (instead of the current implementation with 2018), and the script could take it from there (and show the age as a nice little extra touch)…