Motion sensor + light = light stuck sometimes

Hi!
I am running the latest hass and it’s been working fine for years.
I have a two Philips Hue motion sensors and one from Ikea.
They control lights.

I used to have a simpler automation setup but decided to make it more fancy and use snapshot scenes of the previous state before changing the state of a light.
I used to have one automation to turn on the light when the sensor detected motion, then one automation that turned off the light when there was no motion detected. This worked fine.

Now I have an automation that creates a scene with a snapshot of the light before turning on the light.
Then restores to the scene when there is no motion detected. This works most of the time.
But for some reason sometimes the light stays on sometimes. It does this until I manually turn it off, it’s not enough for me to walk by the motion sensor in order to reset.

I don’t understand why. Any ideas?

I have had this to happen with all three of my sensors.
They are configured the same way.
Here is an example:

- id: '1638993671137'
  alias: Hallway lightstrip Brighter
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 9e71f353163e4b1987439e4fb1240995
    entity_id: binary_sensor.motion_sensor
    domain: binary_sensor
  condition:
  - condition: time
    after: 07:00:00
    before: '23:59:00'
  action:
  - service: scene.create
    data:
      scene_id: snapshot_hallway
      snapshot_entities:
      - light.extended_color_light_10
  - service: light.turn_on
    target:
      device_id: 74582386c5ab68a129915f3e9b8c53e8
    data:
      rgb_color:
      - 200
      - 200
      - 100
      brightness_pct: 90
      transition: 1
  mode: single
- id: '1638995626349'
  alias: Hallway lightstrip restore
  description: ''
  trigger:
  - type: no_motion
    platform: device
    device_id: 9e71f353163e4b1987439e4fb1240995
    entity_id: binary_sensor.motion_sensor
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  condition:
  - condition: time
    after: 07:00:00
    before: '23:59:00'
  action:
  - scene: scene.snapshot_hallway
  mode: single

Sounds like you have the automation restart or run multiple instances at the same time.
When this happens then the first run turns one the lights and before it’s finished and have restored the lights the next one have started and made a snapshot of lights set up from the first run, which means that it overwrites the first snapshot

This was something I suspected as well.
But I have mode: single on all my automations.
I have double checked the condition times as well and I can’t find anything that would explain this.

1 Like

What does logbook and logviewer say?

There are no errors and nothing out of the ordinary.
I will wait until it happens the next time and see exactly what the logs say about triggered automations.

One of the lights stuck again and I investigated the logs.
I know what happens but I don’t know how to solve it.

There is motion detected.
The snapshot scene is created.
The light is turned on.
There is no motion for 20 seconds, the light is not yet turned off since I have a 30 second time under for under trigger (5 seconds in the above example).
Then there is motion detected and the snapshot is created again before the light is turned off.
This creates a snapshot scene with the light turned on.

How do I do this properly with a custom timeout for the light being on after motion is detected, without the snapshot scene being overwritten with the wrong value?

If it was me -

Create a timer helper.
Change the mode to restart.
Use a choose, to check the state of the timer, ONLY create the snapshot if the state of the timer is idle
Listen for the trigger ( platform: event) timer.finished and restore the created snapshot when the trigger fires.

Here is how I listen for the timer event as an example:

  - platform: event
    event_type: timer.finished
    id: timer_finished
    event_data:
      entity_id: timer.music_walk_with_me_hall

Cancel and restart the timer if the timer state is NOT idle and that will extend it when motion was detected while it was still running.

        sequence:
          - service: script.turn_on
            target:
              entity_id: script.music_walk_volume_adjust
            data:
              variables:
                media_target: media_player.hall_speaker
                volume_target: >-
                  {{ states('input_number.master_volume_walk_with_me')|float /10
                  }}
          - service: timer.cancel
            target:
              entity_id: timer.music_walk_with_me_hall
          - service: timer.start
            data:
              duration: '01:30:00'
            target:
              entity_id: timer.music_walk_with_me_hall

That’s how I deal with all my motion lights, and in the example I have given - turning up media volume when I have music playing across the whole house.

Thank you for all the replies and help!

Andrews way of solving the problem felt a little complicated since there already is a counter.
I did some more digging and solved the problem by using an input boolean.
Thanks to this thread: