One script to reset motion sensor + revert to previous saved state

I have tons of Hue lights that im using for double duty. The lights turns on mood lighting at sundown and turns off at sunrise, through the Hue app (not in HA, i want it that way).

After it gets below a certain lux lvl i want the lights to save the state they are in (done), turn on bright light scene (done) and then i have a timer that reverts the lights to previous state (done).

However, the automation only runs once, and i want to keep the lights bright as long as the sensor sees motion. When sensor does not see motion for X time, revert.
If i do reset on the automation type, it runs the whole script again which overwrites the state. Any help would be much appreciated.

alias: Stairs
description: ''
trigger:
  - type: motion
    platform: device
    device_id: c8fb919c884affb764cf7f7df4254371
    entity_id: binary_sensor.trappa_sensor_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: c8fb919c884affb764cf7f7df4254371
    entity_id: sensor.trappa_sensor_illuminance
    domain: sensor
    below: 250
action:
  - service: scene.create
    data:
      scene_id: before_trappa
      snapshot_entities:
        - light.trappa_spot_1
        - light.trappa_spot_2
        - light.trappa_spot_3
        - light.trappa_spot_4
        - light.trappa_ceiling_1
  - service: scene.turn_on
    target:
      entity_id: scene.trapphus_concentrate
    metadata: {}
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: scene.turn_on
    target:
      entity_id: scene.before_trappa
    metadata: {}
mode: single

What about something like this:

alias: Stairs
description: ''
trigger:
trigger:
  - platform: state
    entity_id:
      - binary_sensor.trappa_sensor_motion
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.trappa_sensor_illuminance
    below: '250'
action:
  - service: scene.create
    data:
      scene_id: before_trappa
      snapshot_entities:
        - light.trappa_spot_1
        - light.trappa_spot_2
        - light.trappa_spot_3
        - light.trappa_spot_4
        - light.trappa_ceiling_1
  - service: scene.turn_on
    target:
      entity_id: scene.trapphus_concentrate
    metadata: {}
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.bedroom_motion_sensor_motion
        from: 'on'
        to: 'off'
        for:
          minutes: 3
    continue_on_timeout: false
  - service: scene.turn_on
    target:
      entity_id: scene.before_trappa
    metadata: {}
mode: single

I have changed the trigger and condition to states rather than devices as this is generally better practice.

By changing the delay to a wait for no motion for 3 mins this should prevent the scene from reverting back until there has been no motion for those 3 mins. Although further motion would in theory trigger this automation again, all the time its set to mode: single this would be prevented.

There may be better ways to do this, but the above should in theory work!

1 Like

You sir, are a genius :smiley: So far this seems to work just fine. Will continue testing on my whole setup and get back here so others can use this. I think i might make this a blueprint, has to be more people that want the lights to do double duty! :smiley:

It did work! And it does, somewhat…however it does seem to not re-trigger if i go out of the motion sensor and then back again after say one minute. Then it just stays bright. Very strange…

Ok so this wait 3 mins timed out because motion continued, not what I expected to be honest.

I am thinking maybe you could add a 2nd trigger of no motion for 3 mins and the alter the automation to include a choose function. That was have the initial motion and lux condition and have the scene create, lights change and the 2nd choose with the same lux condition replay the light scene.

I am not in front of a computer so won’t attempt the yaml, if I get time in front of a PC i will post up an example if no one else suggests anything in the mean time.

EDIT:
Just quickly knocked this up, should work, let me know:

alias: Stairs
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.trappa_sensor_motion
    from: 'off'
    to: 'on'
    id: motion triggered
  - platform: state
    entity_id:
      - binary_sensor.trappa_sensor_motion
    to: 'off'
    from: 'on'
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: 'no motion '
condition:
  - condition: numeric_state
    entity_id: sensor.trappa_sensor_illuminance
    below: '250'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion triggered
        sequence:
          - service: scene.create
            data:
              scene_id: before_trappa
              snapshot_entities:
                - light.trappa_spot_1
                - light.trappa_spot_2
                - light.trappa_spot_3
                - light.trappa_spot_4
                - light.trappa_ceiling_1
          - service: scene.turn_on
            target:
              entity_id: scene.trapphus_concentrate
            metadata: {}
      - conditions:
          - condition: trigger
            id: 'no motion '
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.before_trappa
            metadata: {}
    default: []
mode: single

I see the thinking behind this, and it works when you trigger it once. If i trigger again after say 30 seconds it gets stuck. I think this might have to do with the Hue sensor, i think the timeout is 10 seconds. Attached the trace, i trigger it - then move away for 15 seconds and then trigger it again.

And thanks again for taking the time, really appreciate it!