#Turn off a light via motion automation only if the same automation previously turned it on

16 messages · Page 1 of 1 (latest)

empty briar
#

The scenario is a light that should mostly run on a motion sensor, but sometimes will also be manually set to something else and should then ignore motion until it is turned off again.

For that, I'd like the automation to be aware of itself having been what turned on the light in the first place.
I'm sure there could be some workarounds involving helper entities, but looking at the logbook, I can see that the data exists in home assistant.

Question is:
Can I already access that data?
And if not, am I only a custom_component away from having access to said data?

Answer:

Yes. Like this:

{{ states['light.se_laemp'].context.origin_event.data.entity_id == this.entity_id }}
#

Turn off a light via motion automation only if the same automation previously turned it on

vapid osprey
#

Simpler solution (not the most complete. Experts could find better solution. But definitely a simple one)

Make an automation that triggers when motion
And in the sequence of action make it do

  • turn on light
  • wait until
    • no motion
  • turn off light
empty briar
#

The issue (and why my current automation is unintentionally latching atm) is that the occupancy sensor can turn to clear, however I only want to turn off the light after it being clear for at least 20s. Usually, within that 20s span, it should see movement again

#

you could add a second automation to debounce the sensor or check sensor settings, but given that this is (I think) a very common use-case, I'd really like to see a proper "standard"-ish solution

empty briar
#

Oh wow. ChatGPT actually did it and built a custom_component that provides that data as a new sensor.
It only took exhausting the free tier with some back and forth

#

amazing

#

now it's just a matter of cleaning it up, adding config flow and then ship it
what a time to be alive

#

Still, would be elegant if the automation was able to know this without a helper entity

hallow night
#

You can solve this with context. So, you could use something like this to see if it was a user or an automation that changed the light. (I think that syntax is correct for defined).

{{ states['light.light_1'].context.user_id is defined }}
empty briar
#

Huh!

Indeed I can do the same as that custom_component and access

{{ states['light.se_laemp'].context.origin_event }}

origin_event in a template

But how do I access the properties of that Event object?

#

{{ states['light.se_laemp'].context.origin_event.data.entity_id }}

there we go

#

Thanks!

#

{{ ((((states['sensor.doesnotexist'] | default({})).context | default({})).origin_event | default({})).data | default({})).entity_id | default("unknown") }}

btw is there a less convoluted way to do this?

sacred pebble
#

Hacs + entity controller