Need help finishing an automation please

Howdy folks. Wondering if someone can help me complete an automation I’m stuck on.

The idea- have the lights on my patio increase to full brightness when motion is detected, until two minutes after last motion detected, then revert to previous brightness.

I already have an automation to turn on the entity String_Lights, at a certain time. Then, throughout the evening at set intervals (every few hours), the brightness steps down, until eventually turning off in the late hours of the night. I do this so I’m not a complete ass to my neighbors if I’m not actually out there all night.

I’d like though, that if I run out there for some late night fetch with the dog, or need to check on my bbq pits during an overnight smoke, for the lights to come on full brightness for a little while.

I already have the first half of an automation that says: If the string_lights are on, and motion is detected, turn lights to 100%. What I can’t figure out is how to then return them to their previous state. So if at 11 pm and the lights were only at 25%, crank up to 100 with motion, then back down to the previous brightness of 25.

Here’s what I have so far. Thanks to anyone who wants to take a stab at it.

alias: String-Light Motion
description: >-
If string lights are on, and motion is detected, ramp lights to full
brightness for two minutes, then return to previous brightness.
trigger:

  • type: motion
    platform: device
    device_id: 6f23c1a8f1a708c4c752167d75f4052c
    entity_id: 3986e19c5855faf04fd2b7e6acff24eb
    domain: binary_sensor
    condition:
  • condition: state
    entity_id: light.the_string_lights
    state: “on”
    action:
  • service: light.turn_on
    metadata: {}
    data:
    brightness_pct: 100
    target:
    device_id: 62665fe2f6d582de1d27480d8b14b39a
    mode: single

I think scenes are what you want. They will snapshot the state of entities and let you restore them.

Do scene.create with snapshot_entities listing the entities your want to snapshot.

please use preformatted text tag when pasting code so that indents and such are preserved. It is otherwise hard to read code and impossible to catch indent issues

2 Likes
alias: String-Light Motion
description: >-
  If string lights are on, and motion is detected, ramp lights to full
  brightness for two minutes, then return to previous brightness. 
trigger:
  - type: motion
    platform: device
    device_id: 6f23c1a8f1a708c4c752167d75f4052c
    entity_id: 3986e19c5855faf04fd2b7e6acff24eb
    domain: binary_sensor
condition:
  - condition: state
    entity_id: light.the_string_lights
    state: "on"
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 100
    target:
      device_id: 62665fe2f6d582de1d27480d8b14b39a
mode: single

first, you should avoid using device_id. use entity’s instead.

here’s why:

it’ll save you lots of headaches. it’ll also make it easier for folks like me to edit your code.

to use a scene, try something like this:

you should create a scene first with the entities you want in it. below, i just put one (light.outside). first go to the scenes section of homeassistant, create a scene and put all the ligthts you want to be included in there. then in the code below, list them all under “snapshot_entities” this should take a snapshot of the state of each entity at that time. then you can turn it all the way up … and when you’re done,you actviate the scene, which turns it back to where it was.

action:
  - service: scene.create
    metadata: {}
    data:
      scene_id: scene.outdoor
      snapshot_entities:
        - light.outside
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 100
    target:
      device_id: 62665fe2f6d582de1d27480d8b14b39a

#####   insert yourwait or whatever...
## the below will revert your lights.  so if you call it immediately then the turn_on to 100% will get overrridden immediately.
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.outdoor
1 Like