now this would be something to actually use multiple times in a config:
a jinja replacement for a date countdown, we’ve all worked on: Python_script for countdown to birthdays/anniversaries/significant dates - #58 by petro
my personal v version is like this now:
today = datetime.datetime.now().date()
name = data.get('name')
type = data.get('type')
event = data.get('event')
sensorName = 'sensor.{}_{}'.format(type ,name.replace(' ','_'))
dateStr = data.get('date')
dateSplit = dateStr.split('/')
dateDay = int(dateSplit[0])
dateMonth = int(dateSplit[1])
dateYear = int(dateSplit[2])
date = datetime.date(dateYear,dateMonth,dateDay)
thisYear = today.year
nextOccur = datetime.date(thisYear,dateMonth,dateDay)
numberOfDays = 0
years = int(thisYear) - dateYear
if today < nextOccur:
numberOfDays = (nextOccur - today).days
elif today > nextOccur:
nextOccur = datetime.date(thisYear+1,dateMonth,dateDay)
numberOfDays = int((nextOccur - today).days)
years = years+1
phrase = 'dag' if numberOfDays == 1 else 'dagen'
hass.states.set(sensorName,numberOfDays,
{
'entity_picture': '/local/family/{}.jpg'.format(name.lower()),
'unit_of_measurement': phrase,
'friendly_name': '{} wordt {} over:'.format(name,years),
'persoon': name,
'type': '{}'.format(type.title()),
'years': years,
'datum': '{}-{}-{}'.format(dateDay,dateMonth,dateYear)
}
)
binarysensorName = 'binary_sensor.' + sensorName.split('.')[-1]
binarysensorState = 'on' if numberOfDays == 0 else 'off'
hass.states.set(binarysensorName,binarysensorState,
{
'persoon': name,
'type': '{}'.format(type.title()),
'years': years,
'datum': '{}-{}-{}'.format(dateDay,dateMonth,dateYear),
'event': event,
'name': name
}
)
and I would hope to be able to move that all to some jinja wizardry
could it be done in your macro’s?
probably need some adaptation of GitHub - Petro31/easy-time-jinja: Easy Time calculations for Home Assistant templates because I can not do:
{% from 'easy_time.jinja' import count_the_days %}
{{ count_the_days("2023-07-27 00:00:00", utc=True) }}
because of
TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'
this however:
{% from 'easy_time.jinja' import count_the_days %}
{{ count_the_days("2023-07-27 00:00:00") }}
or even
{% from 'easy_time.jinja' import count_the_days %}
{{ count_the_days("2023-07-27") }}
return a day count alright,
ofc that is not the same, as the python is built upon an original date of an event (birthday…) and calculate from that, showing current age, and next date day count…
update
this seems to bring me the correct year count:
{{(states('sensor.date') | as_timestamp - as_timestamp('1964-08-24'))/( 86400*365)}}
so that could be done with a setter:
{% set date = '1964-08-24' %}
{{(states('sensor.date') | as_timestamp - as_timestamp(date))/( 86400*365)}}
but then we’d need some easy way to use that same date
and calculate the
{% from 'easy_time.jinja' import count_the_days %}
{% set current_date = date.replace('1964', (now().year)|string) %}
{{count_the_days(current_date)}}
…
had hoped to use a true datetime for those, but we cant… my dates precede the 1970 epoch.
this can be done:
{% set event = '1964-08-24' %}
{{((states('sensor.date') | as_timestamp -
as_timestamp(event))/( 86400*365))|int}}
{% from 'easy_time.jinja' import count_the_days %}
{% set current_date = event.replace((event|as_datetime).year|string, (now().year)|string) %}
{{count_the_days(current_date)}}