this better:
- platform: template
sensors:
garbage_day:
friendly_name: 'garbage_day'
icon_template: >
{% if is_state('binary_sensor.garbage_day', 'on') %}
mdi:delete-alert
{% else %}
mdi:delete-outline
{% endif %}
# sensors need their entities declared, so they can watch them
# using sensor.date_time (or sensor.time) makes it update every minute
entity_id:
- sensor.date_time
- calendar.garbage_day
# ON 7 hours (25200s) before all_day event = 17:00 the day before, and OFF at 17:00 on the day,
# or 1 hour (3600s) before normal event (Problemmüll)
# CAREFUL: Binary sensors need true/false as input, NOT UI niceties like on/off or the like!
value_template: >-
{% set calendar = 'calendar.problemmuell' %}
{% set start = state_attr(calendar,'start_time') %}
{# Prevent error when no calendar event (we won't have a start_time then) #}
{% if start != None %}
{% set now = now().timestamp() %}
{% set start = as_timestamp(start) %}
{% set end = as_timestamp(state_attr(calendar,'end_time')) %}
{% if now < end %}
{% if state_attr(calendar,'all_day') %}
{{ now < end - 25200 and start - now < 25200 }}
{% else %}
{{ start - now < 3600 }}
{% endif %}
{% else %}
false
{% endif %}
{% else %}
false
{% endif %}