This code is intended as a sensor to know if today is a holiday for me, and gives True or False. It works perfectly. But the less we can say it’s the ugliest way to code this.
This code uses “ical Sensor” integration in HACS.
I have tried to use For
, or using expand
using filters like selectattr
, | list
and | count > 0
and so on. But clearly I missed something because I coudn’t figure out how to put the right pieces in the right syntax to make it work, despite my search in the docs.
Each sensor is tested to meet both conditions: the event’s date in the calendar must be today and
it’s description must be one of the three elements in the list. If at least one event meet the 2 conditions then it’s a day I’m not working.
Please help me simplify in a sensible way.
- binary_sensor:
- name: congé
state: >
{% if (as_timestamp(state_attr('sensor.ical_calend_event_0', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_0', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_1', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_1', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_2', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_2', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_3', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_3', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_4', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_4', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_5', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_5', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_6', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_6', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_7', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_7', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_8', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_8', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% elif (as_timestamp(state_attr('sensor.ical_calend_event_9', 'start')) | timestamp_custom('%j') == as_timestamp(now()) | timestamp_custom('%j') and state_attr('sensor.ical_calend_event_9', 'summary') in ['Congé', 'Férié', 'Vacances']) == True %}
True
{% else %}
False
{% endif %}