Trigger "on" for a specific time, than trigger "off" for the rest of the day

I’m running an automation like this:

alias: UVB - Lampe Stall
description: 11 Hours of Daylight in the Coop
triggers:
  - alias: Trigger 11:00hr before sunset
    value_template: |
      {% set sunset = states('sensor.home_sun_setting')|as_datetime%}
      {{ now() >= sunset - timedelta(hours=11, minutes=00) }}
    id: "on"
    trigger: template
  - event: sunrise
    offset: "00:30:00"
    id: "off"
    trigger: sun
conditions:
  - alias: "Test: Don't run if today is already going to be more than 11 hours"
    condition: template
    value_template: "{{ states('sensor.home_sun_daylight') | float < 11 }}"
actions:
  - type: toggle
    device_id: ebf555b8f79539499f5865b086c02cc6
    entity_id: caad71fe3892a9cab3999d89a5853821
    domain: switch

This usually works just fine. However “sometimes” the electric plug that is controlled by this, misses the trigger (maybe once a month) to switch on. I assume that the Zigbee connection is at its limits here.

This then makes my chickens all confused :slight_smile: :chicken:

My idea would be, to specifically use an “on” trigger (rather than the toggle), for the whole time the light should be on. And then trigger “off” for the whole time the light should be off. Do this every 5 minutes or so. That way it would be a bit more resilliant against these “one off misses”.

But now I’m stuck on implementing this. Can someone help me out?

Yeah… toggle is never a good idea.
You have the trigger id you can use.

This is untested, I’m not sure about the last line, the rest should work.

actions:
  - action: "light.turn_{{ trigger.id }}"
    metadata: {}
    data: {}
    target:
      entity_id: light.light
  - delay:
      seconds: 5
  - repeat:
      sequence:
        - action: "light.turn_{{ trigger.id}}"
          metadata: {}
          data: {}
          target:
            entity_id: light.light
        - delay:
            seconds: 5
      while:
        - condition: state
          entity_id: light.light
          state: "{{ !trigger.id }}"

Hello Kai,

Also don’t be afraid to delay a minute or so, and send the light on/off command again a couple of times. It won’t hurt and might help. I also think there is a Custom Integration on GitHub found thru HACS that does that for you in some way. I have not used it.

My solution looks like this. Which seems to do what I need. Thanks for your help @Hellis81 I did not know that I can use the trigger name in this way. This was kind of eye opening to me and let me change a lot of my other stuff as well.

:bowing_man:

alias: UVB-Lampe Stall (Neu)
description: 11 Hours of Daylight in the Coop
triggers:
  - alias: Trigger 11:00hr before sunset
    value_template: |
      {% set sunset = states('sensor.home_sun_setting')|as_datetime%}
      {{ now() >= sunset - timedelta(hours=11, minutes=00) }}
    id: "on"
    trigger: template
  - event: sunrise
    offset: "00:30:00"
    id: "off"
    trigger: sun
conditions:
  - alias: "Test: Don't run if today is already going to be more than 11 hours"
    condition: template
    value_template: "{{ states('sensor.home_sun_daylight') | float < 11 }}"
actions:
  - repeat:
      sequence:
        - type: turn_on
          device_id: ebf555b8f79539499f5865b086c02cc6
          entity_id: caad71fe3892a9cab3999d89a5853821
          domain: switch
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
      while:
        - condition: trigger
          id:
            - "on"
  - repeat:
      sequence:
        - type: turn_off
          device_id: ebf555b8f79539499f5865b086c02cc6
          entity_id: caad71fe3892a9cab3999d89a5853821
          domain: switch
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
      while:
        - condition: trigger
          id:
            - "off"
mode: single

That will probably work but it will also mean the automation are running ALL the time.

This is based on your automation but at least it will stop at some point.

alias: UVB-Lampe Stall (Neu)
description: 11 Hours of Daylight in the Coop
triggers:
  - alias: Trigger 11:00hr before sunset
    value_template: |
      {% set sunset = states('sensor.home_sun_setting')|as_datetime%}
      {{ now() >= sunset - timedelta(hours=11, minutes=00) }}
    id: "on"
    trigger: template
  - event: sunrise
    offset: "00:30:00"
    id: "off"
    trigger: sun
conditions:
  - alias: "Test: Don't run if today is already going to be more than 11 hours"
    condition: template
    value_template: "{{ states('sensor.home_sun_daylight') | float < 11 }}"
actions:
  - repeat:
      sequence:
        - type: turn_on
          device_id: ebf555b8f79539499f5865b086c02cc6
          entity_id: caad71fe3892a9cab3999d89a5853821
          domain: switch
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
      while:
        - condition: trigger
          id:
            - "on"
        - condition: state
          entity_id: switch.caad71fe3892a9cab3999d89a5853821
          state: "off"
  - repeat:
      sequence:
        - type: turn_off
          device_id: ebf555b8f79539499f5865b086c02cc6
          entity_id: caad71fe3892a9cab3999d89a5853821
          domain: switch
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
      while:
        - condition: trigger
          id:
            - "off"
        - condition: state
          entity_id: switch.caad71fe3892a9cab3999d89a5853821
          state: "on"
mode: single

I assume the entity id is switch.caad71fe3892a9cab3999d89a5853821.
You should stop using device trigger/condition/actions in automations. It’s much better to use the entity ID as I did above.

The condition I added means it will only keep looping as long as the state is “incorrect”.

1 Like