Not much of a project but hopefully it’ll help others facing the same problem: I’ve got some automations I want to trigger at a certain time before sunset vaguely defined as “before the night insects start flying”, which changes by season. I thought it’d be easy enough to do, just vary the offset from sunset:
template:
- sensor:
- name: "Time Offset Summer/Winter"
unique_id: uniqueid__time_offset_summer_winter
state: "{{ {'spring': '-01:30:00', 'summer': '-01:30:00', 'autumn': '-01:00:00', 'winter': '-01:00:00'}.get(states('sensor.season'), 'summer') }}"
triggers:
- trigger: sun
event: sunset
offset: "{{ states('sensor.time_offset_summer_winter') }}"
However this doesn’t work because the sun offset field doesn’t accept templates.
To make this work, you need to perform the required calculation inside the template sensor:
template:
- sensor:
- name: "Sunset With Seasonal Offset"
unique_id: uniqueid__sunset_with_seasonal_offset
device_class: timestamp
state: >
{% set offsets = {'spring': 90, 'summer': 120, 'autumn': 90, 'winter': 60} %}
{% set mins = offsets.get(states('sensor.season'), 120) %}
{{ state_attr('sun.sun', 'next_setting') | as_datetime - timedelta(minutes=mins) }}
and then trigger on the resulting time value:
triggers:
- trigger: time
at: sensor.sunset_with_seasonal_offset