Check if Scene Exists in Automation

Hello.
I have created an automation for some lights which brightens them when motion is detected and after a few minutes restores their previous state.

I’ve noticed that sometimes the lights do not return to their original dim state.
I believe what may be happening is that the automation is triggering again while the lights are already in a bright state and the temporary state is being over written with these values.

I wanted to try and add an if statement before creating the temporary state file but I cannot find the correct syntax for it. The examples I’ve found don’t seem to apply to what I am doing.

Hopefully I’m explaining this clearly enough.
Any suggestions to get this to work properly are appreciated.
Thanks

I’m not sure that the saved scene always disappears immediately after it is used. If does with a reboot, but other than that… So I’d probably use some other entity to remember if I created the scene and changed the lights. An input helper could work, or if you are using a timer then you could check for the timer to see if it is running.

You could also check for the brightness using a numeric state condition on the brightness attribute (it goes from 0 to 255, this one is not a percentage!), if you know what brightness you are looking for.

Thanks for the suggestions.
At the end of my automation I have a Scene Delete action that is supposed to remove the temporary scene.

It does mostly work but occasionally I have to manually reset the lights to their previous state and run that scene delete command to get it back to normal.
I haven’t been able to track down where it is going wrong yet.

For reference here is my automation YAML taken from the editor:

alias: Porch Motion Activated Lights
description: ""
triggers:
  - entity_id: binary_sensor.motion_sensor_motion
    from: "off"
    to: "on"
    trigger: state
conditions:
  - condition: sun
    before: sunrise
    after: sunset
  - condition: or
    conditions:
      - condition: time
        after: "00:00:00"
        before: "01:00:00"
      - condition: time
        before: "23:59:00"
    enabled: false
actions:
  - action: scene.create
    metadata: {}
    data:
      scene_id: motion_porch_state
      snapshot_entities:
        - light.porch_lamp_left_light
        - light.porch_lamp_right_light
  - alias: Turn on the light
    action: light.turn_on
    data:
      transition: 3
      brightness_pct: 100
      kelvin: 6500
    target:
      device_id:
        - 79598c93465930b79133e76cb77e142d
        - db9eee64bbd9a248e2a3ed0c051877bb
  - alias: Wait until there is no motion from device
    wait_for_trigger:
      entity_id: binary_sensor.motion_sensor_motion
      from: "on"
      to: "off"
      trigger: state
    timeout:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - alias: Wait the number of seconds that has been set
    delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.motion_porch_state
  - action: scene.delete
    metadata: {}
    data: {}
    target:
      entity_id: scene.motion_porch_state
mode: restart
max_exceeded: silent

Well, the scene create, paired with restart mode in the automation will definitely ruin the scene. But I doubt you can test the state of a non existing scene. If so the state would probably be unknown.

Thank you. I’m not sure how or why I had changed the mode. I’ve set it back to single now.

Also, I found a post which seems to have helped.

I believe I have this working correctly now:

if:
  - condition: not
    conditions:
      - condition: template
        value_template: "{{ states.scene.motion_porch_state != None }}"
then:
  - action: scene.create
    metadata: {}
    data:
      scene_id: motion_porch_state
      snapshot_entities:
        - light.porch_lamp_left_light
        - light.porch_lamp_right_light

Thanks again for your input.

1 Like