Automation in restart mode

I have an automation that turn on/off the lights based on a motion sensor.
When there is no motion for 1 minute, it will turn off a switch and after another minute it will turn off another switch.

Is a good practice to leverage on restart mode from automation?

alias: big bathroom lights
mode: restart
trigger:
  - platform: state
    entity_id: binary_sensor.bb
action:
  - choose:
      - conditions:
          - "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - service: switch.turn_on
            entity_id: switch.bb_mirror
          - condition: time
            after: "08:00:00"
          - service: switch.turn_on
            entity_id: switch.bb_spots
    default:
      - delay: "00:01:00"
      - service: switch.turn_off
        entity_id: switch.bb_spots
      - delay: "00:01:00"
      - service: switch.turn_off
        entity_id: switch.bb_mirror

In restart mode it will as you know start again from the top.
If the light was on for 59 seconds and motion is triggered it will restart the automation again. There is a possibility where one light is already off when it restarts…
You might see in the log it triggered several times.

I have a similar automation and my question is how are these lights getting turned on?
If it is from motion then maybe you could combine the on and off automation into one.

If there is no motion for 1 minute and 1 second and then I walked passed what would you expect to happen?

1 Like

In my case, it is working as expected.
The trigger for me is the motion sensor, no matter if the state is on or off.
If the motion is detected, my switches will turn on.
If no motion is detected for 1 minute, one switch will turn off and after another minute, the other switch will turn off.
This way, if I am in a dead zone or if I stay still, I can notice the first switch turning off and I can wave my hand.

To answer your question, after one minute and 1 second there will be only one switch turned on. Because I triggered the motion sensor, the automation will restart and it will turn on all the switches

I saw an old post that suggested to use a script and cancel it instead of using the restart function from automation. Altho, I cannot find it and I’m not sure if this is still accurate.

I guess it is achieving the same thing really. Reading the documentation using restart might be better because it simply stops all further actions from running anyway rather than having to call to stop a script running yourself. This is the hard part of coding and automation - there are different ways to achieve the same thing.

For example you could ditch the whole delay thing and utilise a timer you create when motion is detected. At 1 minute to go turn one light off and when timer is finished turn the other light off. Motion detected again? - restart timer but this is a complicated way in my mind.

My suggestion here might not be how others do it but I would simply combine the automations and because they are combined you are justifying having the restart mode there even more.

If motion detected - turns lights on if not on
If motion not detected for 1 minute - turn off bb_mirror
If motion not detected for 2 minutes - turn of bb_spots (no delay this way)

The trigger would simply be binary_sensor.bb with trigger ids

alias: big bathroom lights
mode: restart
trigger:
  - platform: state
    entity_id: binary_sensor.bb
    from: off
    to: on
    id: motion_detected
  - platform: state
    entity_id: binary_sensor.bb
    from: on
    to: off
    for: 00:01:00
    id: no_motion
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_detected
         # Having this here would stop it running if mirror lights were already on however if the bb_spots were on and mirror was off it would turn mirror back on
          - condition: state
            entity_id: switch.bb_mirror
            state: 'off'
        sequence:
          - service: switch.turn_on
            entity_id: switch.bb_mirror
          - condition: time
            after: "08:00:00"
          - service: switch.turn_on
            entity_id: switch.bb_spots
      - conditions:
          - condition: trigger
            id: no_motion
        sequence:
          - service: switch.turn_off
            entity_id: switch.bb_mirror
          - delay: "00:01:00"
          - service: switch.turn_off
            entity_id: switch.bb_spots
    default: []

Only an idea but there are so many ways to achieve this. As for what is better to do for performance etc? Guess we don’t really have that level of detail in the docs.

2 Likes