I am creating a calendar display and am struggling with events that span multiple days. I would need to see whether a timecode is lower or equal to the current day. I created sensors that pull together all needed information from a ical calendar for the first 9 events.
The sensors contain 2 attributes: eta and endeta. Both have a code that is generated from:
for eta:
state_attr('sensor.ical_exchange_event_0', 'start').strftime('1%U%d')
and for endeta:
(state_attr('sensor.ical_exchange_event_0', 'end').strftime('1%U%d') | int) - 1
It gives me values like: 14617
for eta and 14618
for endeta for an all-day event spanning 2 days.
I then compare this to the date of today to see which events are for today and create a string with all the events data for that day in a sensor. I use the following code to do that per day of the week:
{% set etas = expand('sensor.calendaritem0', 'sensor.calendaritem1', 'sensor.calendaritem2',
'sensor.calendaritem3', 'sensor.calendaritem4', 'sensor.calendaritem5',
'sensor.calendaritem6', 'sensor.calendaritem7', 'sensor.calendaritem8', 'sensor.calendaritem9') %}
{% set today = (as_timestamp(now()) + (24*3600)) | timestamp_custom('1%U%d') | int %}
{% set alldayevents = etas | selectattr('attributes.endeta', 'eq', (today)) | map(attribute='state') | join('^') | default('empty', true) %}
Which gives me a ‘^’ separated list of all-day events for example:
all day^Netwerk Installatie^ ^
The issue is that if the all-day event spans more than 2 days the end date should not be equal (‘eq’) to today’s date but should be equal or less (‘le’) to todays date. I tried this:
{% set alldayevents = etas | selectattr('attributes.endeta', 'le', (today)) | map(attribute='state') | join('^') | default('empty', true) %}
But this generates the following error:
TypeError: '<=' not supported between instances of 'str' and 'int'
I understand that selectattr
parses everything as a string. Is there a way to be able to compare them as int
?