Motion Light Automation

I have bunch of these motion light automations. They start by creating a scene with all of the lights current state, then activate a new scene with the desired brightness and color, then wait for a specified period of time, then revert to the snapshot scene created at the beginning.

The way it works right now, during periods of intermittent motion, the timer runs out and motions needs to be detected again to start the automation again. I would prefer that the timer restart when motion is detected again.

Originally I had this automation set to a ā€œRestartā€ mode, but the problem with that is that it overwrites the original snapshot scene with the original state of the lights.

Iā€™m looking for some suggestions on how to have my cake and eat it too.

alias: Porch Motion Sensor
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 1e0770ec7afb2c2453eb41b5a6cb7000
    entity_id: binary_sensor.third_reality_inc_3rms16bz_814fe7ff_ias_zone
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 3c7fcc437205db629b6e7f5d16c359a1
    entity_id: b37c327445c903d343540c3f2e61b703
    domain: binary_sensor
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: scene.create
    data:
      snapshot_entities:
        - light.wiz_bulb_12
        - light.wiz_bulb_21
        - light.wiz_bulb_22
        - light.wiz_bulb_23
        - light.wiz_bulb_24
      scene_id: porch_motion_sensor_snapshot
  - service: scene.turn_on
    target:
      entity_id: scene.porch_motion_light
    metadata: {}
  - wait_template: >-
      {{ is_state('binary_sensor.third_reality_inc_3rms16bz_814fe7ff_ias_zone',
      'off') and
         is_state('binary_sensor.front_door_sensor_4aafe7ff_ias_zone', 'off') }}
    continue_on_timeout: true
    timeout: "20"
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: scene.turn_on
    data:
      transition: 4
    target:
      entity_id: scene.porch_motion_sensor_snapshot
mode: single

I would suggest using mode: restart instead of single. This will restart the automation from the beginning as soon as it is triggered again

Originally I had this automation set to a ā€œRestartā€ mode, but the problem with that is that it overwrites the original snapshot scene with the original state of the lights.

As mentioned, the problem with restart is that it repeats this step, which defeats the purpose of the automation.

action:
  - service: scene.create
    data:
      snapshot_entities:
        - light.wiz_bulb_12
        - light.wiz_bulb_21
        - light.wiz_bulb_22
        - light.wiz_bulb_23
        - light.wiz_bulb_24
      scene_id: porch_motion_sensor_snapshot

I wonder if I could switch this automation to restart, and add an if / then to skip the snapshot if the automation was restarted. Is that possible?

Iā€™m an idiot and know very little about HA. But i did a similar thing where i set a helper as true and waited until completion of the automation before resetting to false.

It eliminated sending me multiple alerts when i only wanted one.

Maybe this helps?

Edit:
Iā€™m very curious if you get this figured out because i need my motion lights to do exactly (literally) the same as you.

Right now a motion event will shut the lights off after 5min regardless of what state they were in (via switches) and its super annoying.

1 Like

That honestly sounds like a decent solution. I do this with other automations to throttle notifications (Uber Eats where Iā€™m notifying on order status but sometimes that will flip to unavailable and back causing the same notification to be sent again).

So first action becomes an if statement to check if the input is off and, if it is, captures the scene. Second action turns the input helper on, do whatever else, and add an action after the wait period to turn the helper off. Change the mode to restart.

When the automation triggers the first time, the helper is turned on and scene is captured. If itā€™s triggered again before the timeout, then capturing the scene would be skipped since the helper would be on.

1 Like

Great suggestions, Iā€™ll give them a try!

1 Like

This worked great, thanks for the suggestion

alias: Litter Box Motion Sensor Occupied Light
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 75d02f774904fb122417072ce7b84f04
    entity_id: e3a51303235c0f09fb1bcf17161f7f1a
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 74374cf98ac0656d85c93f794fccc720
    entity_id: 3d5cb39717ed3db31b9558c3318b5561
    domain: binary_sensor
condition: []
action:
  - if:
      - condition: state
        entity_id: input_boolean.litterbox_motion_light_automation_helper
        state: "off"
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: litterbox_motion_sensor_snapshot
          snapshot_entities:
            - light.hue_color_lamp_15
  - service: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.litterbox_motion_light_automation_helper
    enabled: true
  - service: scene.turn_on
    target:
      entity_id: scene.litter_box_occupied
    metadata: {}
  - wait_template: |2-
        {{ is_state('binary_sensor.litter_box_motion_sensor', 'off') and
         is_state('binary_sensor.litter_box_outhouse_motion_sensor_7', 'off') }}
    continue_on_timeout: true
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.litter_box_available
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.litterbox_motion_sensor_snapshot
  - service: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.litterbox_motion_light_automation_helper
mode: restart

2 Likes

Neat, glad it worked.

You might enjoy this:

value_template: "{{ states.sun.sun.attributes.elevation | float < -2.5 }}"

Allows you to set how far ā€˜belowā€™ the horizon you want the automation to start working. Itā€™s still pretty bright out where I am, so I wait a bit to enable things.