Automation: Service Template using sunrise and sunset (struggling)

Hi all,

I’ve been searching all through the forums and haven’t been able to find the code that would assist me on doing the following:

Starting my pool pump 3 hours after sunrise for 30 min and then again 6 hours after sunrise for 30 min. Taking into account redundancy. In short I just don’t know how to use sunrise/sunset in a value template like you would time or a float value. Here is what I have currently that does the job if there’s no interruptions.

  - alias: Run Pool Pump 3 & 6 Hours After Sunrise for 30 Min
    trigger:  
      - platform: sun
        event: sunrise
        offset: '+03:00:00'
      - platform: sun
        event: sunrise
        offset: '+06:00:00'
    action:
      - service: switch.turn_on
        entity_id: switch.pool_pump
      - delay: 00:30:00
      - service: switch.turn_off
        entity_id: switch.pool_pump

So what is going wrong? Without testing the automation looks right.

It does work, it’s just the redundancy I can’t work out. So for example if the power goes off, or even if I restart HA at say 6H 23M after sunrise, the automation will never trigger again until the following day at 3H after sunrise.

Create two automations. One for on, one for off.

  - alias: Run Pool Pump 3 & 6 Hours After Sunrise
    trigger:  
      - platform: sun
        event: sunrise
        offset: '+03:00:00'
      - platform: sun
        event: sunrise
        offset: '+06:00:00'
    action:
      - service: switch.turn_on
        entity_id: switch.pool_pump
  - alias: Turn off Pool Pump after 30 Min
    trigger:  
      - platform: sun
        event: sunrise
        offset: '+03:30:00'
      - platform: sun
        event: sunrise
        offset: '+06:30:00'
    action:
      - service: switch.turn_off
        entity_id: switch.pool_pump

Thanks but funny enough that is what I had before :slight_smile:

I wanted to consolidate into a single automation.

If you want one automation you could do this:

  - alias: Run Pool Pump 3 & 6 Hours After Sunrise for 30 Min
    trigger:  
      - platform: sun
        event: sunrise
        offset: '+03:00:00'
        id: 'on'
      - platform: sun
        event: sunrise
        offset: '+06:00:00'
        id: 'on'
      - platform: sun
        event: sunrise
        offset: '+03:30:00'
        id: 'off'
      - platform: sun
        event: sunrise
        offset: '+06:30:00'
        id: 'off'
    action:
      - service: "switch.turn_{{ trigger.id }}"
        entity_id: switch.pool_pump
2 Likes

That is perfect, I wasn’t aware of the “id:” option, thank you!