and hurray, I see the original dates in the sensors attributes! At last.
Don’t understand why, but it works for now, so thats really nice, thanks for helping me out!
cool Mark,
now, another question: could the day (of the next occurence) be incorporated easily into the script? Is something I always need when checking for upcoming parties
Hey, just to let you know, your script has a no-no and there’s some simplifying that you can do. By no-no i mean you’re overriding the built in type method.
The optimization would just be a personal preference.
You can also build in some ‘safety’ in case people don’t fully configure the thing.
Also, I added the awesome and far superior US date style.
today = datetime.datetime.now().date()
name = data.get('name', 'special') # defaults to special
eventtype = data.get('type', 'event') # defaults to event
region = data.get('region', 'EU') # defaults to dirty european date format.
dateStr = data.get('date', r'01/01/{}'.format(today.year)) # defaults to jan 1st this year.
sensorName = "sensor.{}_{}".format(eventtype.lower(), name.replace(" " , "_").lower())
fmat = '%d/%m/%Y' if region == 'EU' else '%m/%d/%Y'
origin = datetime.datetime.strptime(dateStr, fmat).date()
event = origin.replace(year=today.year)
event = event if event > today else event.replace(year=event.year+1)
friendly_name = "{}'s {}".format(name, eventtype) if eventtype=="birthday" else "{} {}".format(name, eventtype)
hass.states.set(sensorName , (event-today).days,
{
"icon" : "mdi:calendar-star" ,
"unit_of_measurement" : "days" ,
"friendly_name" : friendly_name,
"nextoccur" : event.strftime(fmat),
"years" : (event-origin).days//365,
}
)
uses
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
- service: python_script.date_countdown
data:
name: Superior Date Style
type: birthday
date: 11/22/2019
region: US
Log Details (ERROR)
Fri Nov 22 2019 16:10:56 GMT+0100 (CET)
Error executing script: name 'dateStr' is not defined
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute
exec(compiled.code, restricted_globals, local)
File "event_date.py", line 12, in <module>
NameError: name 'dateStr' is not defined
and it takes down my HA instance once more, as reported above.
Unable to serialize to JSON: Object of type date is not JSON serializable
and then followed by the full config in the log for all entities and all their attributes…
must have to do with the 'next_occurandorigin` I added, because when I simply used dateString in the original script I got the same errors, and had to use:
Thanks for your response. it seams that i didn’t saved the file since i couldn’t find your script on my python file. All works good now.
Thanks for your help.