Automation with calculated negative offset from sunset

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:

offset = 2.75 + 0.75 * COS( day-of-year * 2 * PI() / 365)

Note: This is in the southern hemisphere, for the northern hemisphere “day-of-year” would have to have 172 subtracted from it

My problem is that I can’t figure out how to integrate this into an automation that turns off my switch at the right time. Can someone help? Thanks

For my sunrise “turn on” switch, I’ve simply added a delay, but I’m not sure how to do this for a negative delay for the sunset:

alias: Pool pump on (adjusted)
description: Adjusted based on sunrise
triggers:
  - trigger: sun
    event: sunrise
conditions: []
actions:
  - delay: >
      {% set pi = 3.14159 %}
      {% set day = now().timetuple().tm_yday %}
      {% set
      delay_hours = 2.5 + 0.5 * math.cos(day * 2 * pi / 365) %}
      {% set
      total_seconds = delay_hours * 3600 %}
      {% set hours = (total_seconds //
      3600)|int %}
      {% set minutes = ((total_seconds % 3600) // 60)|int %}
      {% set
      seconds = (total_seconds % 60)|int %}
      {{ "%02d:%02d:%02d" | format(hours,
      minutes, seconds) }}
  - type: turn_on
    device_id: 87c334d523de7aa38669b58defb0e26a
    entity_id: 0566db6ab160fd9f7ad1dec55043bb24
    domain: switch
mode: single

Just add an offset to the trigger. Read the documentation:

Documentation makes no mention of templates in the offset though, so if it does not work, use a template trigger instead.

Or make automations that use numeric state triggers based on the sun elevation instead, as I assume that is what your calculation compensates for.

And how would I create a template trigger that does that? Because you are right, templates are not allowed in the offset

Just get the datetime object of the next sunset and add whatever offset you need.

- trigger: template
  value_template: >-
    {{ state_attr('sun.sun', 'next_setting') | as_datetime - timedelta(hours=1) }}

Have you considered using a Sun Elevation Trigger?

It’s a common way to adjust the Sun Trigger for seasonality.

1 Like

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.

This solution works really well. I had to adjust the code slightly for my preferences, but it makes the automation so easy! Thank you

1 Like