Hi, I am trying to create an automation that will turn off a switch at a calculated time before sunset. The rationale is that I want to turn off my pool pump 2h before sunset in winter and 3.5h in summer. For this, I came up with the following equation:
The sun elevation trigger is an interesting idea, but doesn’t work for my use case: In summer, the pump would have to turn off at around 40°, and in winter around 20°. If I fixed it to a number, my pump would not run long enough in winter. So I would have to create a calculation no matter what.
I’m testing the template trigger, but somehow it hasn’t triggered by itself yet…
Like most (all?) triggers, it will only trigger when the/its state changes. If it already evaluates to true at the time the automation is (re)loaded, it will never trigger until it flips to false and then back to true again.
I would simply use two Sun Elevation Triggers along with a condition that only allows the automation to continue if it triggered during the correct season.
I would probably set up a template binary sensor. That way you can trigger on the sensor’s state for both. It also makes it easier to create more resilient automations because you can include other triggers and just use the sensor’s state in the conditions rather than duplicating the template.
template:
- binary_sensor:
name: It's Pool Pumping Time
state: |
{% set day = now().timetuple().tm_yday %}
{% set delay_hours = 2.5 + 0.5 * cos(day * 2 * pi / 365) %}
{% set d_sec = timedelta(seconds=(delay_hours * 3600) | int(0)) %}
{% set rise = states('sensor.sun_next_rising')|as_datetime|as_local %}
{% set sunset = states('sensor.sun_next_setting')|as_datetime|as_local %}
{{ (rise + d_sec).time() <= now().time() < (sunset - d_sec).time() }}
Note: You may want to add a delay_off and/or delay_on. Depending on where you are in the world, the difference, day-to-day, of sunrise and sunset may be enough to cause the sensor’s state to bounce. Using the delay configuration options can help smooth that out.