Automation Mode: Restart - Correct Logic - No Blind Alleys

Hey, my first post, woo!

Down to brass tacks

I think the restart function in Home Assistant automations is not user-friendly and could use some reconfiguration. In the meantime, I believe I’ve figured out a solution to use it effectively.

A bold claim for a newbie, I know! Don’t’ get me wrong, i think HA is great, but this feature needs some work and after combing through several similar topics (there are about five on this forum), none seemed to provide a truly satisfying solution. I think I’ve discovered a method that works, but I also believe we shouldn’t have to jump through these hoops to make it work.


Here’s my use case (if you’re still with me):

I want my hallway lights to turn on when someone enters the hall and stay on as long as there’s movement. Once the hall is empty, the lights should turn off. Simple, right?

So, I set up an automation triggered by an occupancy sensor (Hue Motion Sensor). The automation includes rules for time of day, whether the lights are already on, and a one-minute timer after which the lights turn off. I also set the mode to restart.

But…it doesn’t work. The lights stay on instead of turning off.


Why doesn’t it work?

Simply put, the conditions that initially triggered the automation change after the trigger. In my case, at least two variables are no longer the same:

  1. The lux levels are now higher because the lights are on.
  2. The lights themselves are on, whereas they were off before the trigger.

When the occupancy sensor is triggered a second time during the automation’s wait period, the trace shows that none of the conditions are met. The automation takes the wrong path — straight to the end — where no actions are executed.

I think the restart setting should work differently. It should assume that the logic which originally triggered the automation still applies and should re-trigger the actions accordingly.


The workaround: Start at the end and work backward to avoid blind alleys

To solve this, you need to think about what you want to happen after the automation successfully runs. Then, work backward to build the sequence of actions. This approach ensures there are no blind alleys for the automation to get stuck in.

Here’s my current hallway automation. It has a few extra variables and a few redundancies, but it solves the issue effectively.


Anyway, that’s my first post about a problem and what I think is the solution. What do you think?

alias: HallwayAutoMotion
description: HallwayAutoMotion
triggers:
  - type: occupied
    device_id: f154
    entity_id: 7059
    domain: binary_sensor
    trigger: device
conditions:
  - condition: or
    conditions:
      - condition: state
        entity_id: light.hallwaylights_2
        state: "off"
      - condition: state
        entity_id: input_boolean.togglesensorhallway
        state: "on"
actions:
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.togglesensor
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: sun
                    after: sunrise
                  - condition: state
                    entity_id: schedule.schedule_night_lights
                    state: "off"
              - condition: time
                before: "08:00:00"
              - condition: or
                conditions:
                  - type: is_illuminance
                    condition: device
                    device_id: f154
                    entity_id: b792
                    domain: sensor
                    below: 15
                  - condition: state
                    entity_id: input_boolean.togglesensorhallway
                    state: "on"
        sequence:
          - target:
              entity_id: scene.hallway_early_morning
            action: scene.turn_on
            data:
              transition: 1
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
          - target:
              entity_id: scene.hallway_off
            action: scene.turn_on
            data: {}
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.togglesensorhallway
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: sun
                    after: sunrise
                  - condition: state
                    entity_id: schedule.schedule_night_lights
                    state: "off"
              - condition: time
                after: "08:00:00"
              - condition: or
                conditions:
                  - type: is_illuminance
                    condition: device
                    device_id: f154
                    entity_id: b792
                    domain: sensor
                    below: 15
                  - condition: state
                    entity_id: input_boolean.togglesensorhallway
                    state: "on"
        sequence:
          - target:
              entity_id: scene.hallway_daytime
            action: scene.turn_on
            data:
              transition: 1
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
          - target:
              entity_id: scene.hallway_off
            action: scene.turn_on
            data: {}
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.togglesensorhallway
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: schedule.schedule_night_lights
                state: "on"
        sequence:
          - target:
              entity_id: scene.hallway_nighttime
            action: scene.turn_on
            data:
              transition: 1
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
          - target:
              entity_id: scene.hallway_off
            action: scene.turn_on
            data: {}
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.togglesensorhallway
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.hallwaylights_2
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.togglesensorhallway
mode: restart

And here is the code in editor view for clarity .

The purpose of mode is to determine how to handle the following situation:

The automation, or script, is requested to execute its actions while it’s already busy executing its actions.

It makes no assumptions, and has no knowledge of, what kind of actions are currently being executed or what the conditions were that led to their execution.

Reference

If you believe it should work another way, feel free to post a Feature Request.

Thank you thanks for this Jack but I don’t think there is an example on that page that addresses my point. Simple single turn and off is not the issue.

I’m trying to use a motion sensor and the restart function as a type of presence sensor to keep the lights on whilst needed and I think the functionality of the restart mode should be updated.

(A future feature request if there are others out there with the same issue and I think there are)