Help with Automation: When a light is turned on, toggle it a few times

I have a light (not smart) for my back yard that when you turn it on it defaults to “motion”. That is to say it turns off and waits to detect movement before turning on. However it has a “feature” that if you toggle the light a few times (3) the light will stay on until the next time you turn it off.

I have recently put a smart switch to control this light, and I thought maybe I could set up an automation to have it stay on without having to manually toggle the light a bunch of times.

That is to say, when I turn a light on, I want the system to turn it on and off 3 times automatically in quick succession (ending on an on state)

I believe I am about 80% there, however I am unable to tweak the values to get it to work.

Either the light doesn’t toggle the correct number of times (and ends up off) or it keeps toggling over and over again in an infinite loop (as the state changes to on repeatedly and triggers new automations I think).

This is what I have:

trigger:
  - platform: state
    entity_id:
      - light.deck_light
    from: "off"
    to: "on"
condition: []
action:
  - repeat:
      count: "4"
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 300
          enabled: true
        - type: toggle
          device_id: **********************
          entity_id: light.deck_light
          domain: light
mode: single

Any and all help on this will be most appreciated :slight_smile:

Try mode: restart

Also if it only cycles through 4 times that is two on/off cycles, not 3.

Hello,

Thank you for your response! Based on my understanding of Automation Modes - Home Assistant mode: restart would cause the first execution to stop and a new execution context would fire up (which would continue ad-infinititum as the trigger for the automation is also the action of the automation.)

And yes, the 4 toggles would be 2 cycles, however the one that is manually triggered (i.e. pressing the switch) would be the 3rd. That said, if the hardware needs two more toggles I can add that once I get the multiple toggles working :slight_smile:

Edit: I was able see that there is a timeline trace log that I can view - seems the order that things are triggering is not working as anticipated.

Ok, so I updated the timings, as well as added a 10 second delay to the end of the execution to prevent a new instance from firing up. This seems to prevent the infinite loop, and the order tends to fire in the correct order (mostly).

I would prefer to have something that does not rely on consistent network latency. The pseduo code of what I would like would be something like this:

When (light turns on):
   Repeat 4 times:
      light_state_variable = light.state
      light.toggle
      while (light.state == light_state_variable)
          Sleep 100 miliseconds
      endWhile //wait for the state of the light to register a change before moving to the next repeat
   endRepeat

Here is my YAML (Yes I know the light ID changed, that light is just one I can see from my PC for testing purposes)

description: >-
  Deck light defaults to motion sensor.  Turning it on and off 3 times causes it
  to stay on
trigger:
  - platform: state
    entity_id:
      - light.front_hall_light
    from: "off"
    to: "on"
condition: []
action:
  - repeat:
      count: "4"
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0
        - type: toggle
          device_id: ****************
          entity_id: light.front_hall_light
          domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
mode: single

Trace of a run:

So with some more fiddling and some trial and error I ended up with this.

It does not seem super elegant to me, but it does seem to be working. If you have suggestions on how to improve this I am all ears :smiley:

description: >-
  Deck light defaults to motion sensor.  Turning it on and off 3 times causes it
  to stay on
trigger:
  - platform: state
    entity_id:
      - light.deck_light_light
    from: "off"
    to: "on"
condition: []
action:
  - variables:
      light_state: "{{ states('light.deck_light_light') }}"
  - repeat:
      count: "4"
      sequence:
        - type: toggle
          device_id: ffb6b0cfb3d36c059985fdc99461e097
          entity_id: light.deck_light_light
          domain: light
        - repeat:
            while:
              - condition: template
                value_template: "{{ light_state == states('light.deck_light_light') }}"
            sequence:
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 0
                  milliseconds: 100
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 300
        - variables:
            light_state: "{{ states('light.deck_light_light') }}"
        - service: logbook.log
          data:
            name: loggin_state
            message: The state is {{ light_state }}
  - type: turn_on
    device_id: ffb6b0cfb3d36c059985fdc99461e097
    entity_id: light.deck_light_light
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
mode: single