#How would I do this: camera detects
1 messages · Page 1 of 1 (latest)
Ugly but simple:
restartmode in the automation. Actions to turn on the light, delay 10 minutes, then turn off the light
Less ugly but more complex:
- (Re)start a 10 minute timer when motion is detected, turn on the light
- Turn off the light when the timer expires
Essentially: turn light on when motion sensor goes to the “motion detected” state. Turn light off when the motion sensor has been in the “motion not detected” state for 10 minutes
@kind perch so I need to setup two separate automation rules right?
It requires two trigger, but you can have both triggers in the same automation. You can just copy the code from the link and change the entity_id’s to match yours.
alias: "Living room motion - after sunset- turn on light "
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.living_room_motion
to: "on"
- platform: state
entity_id:
- binary_sensor.living_room_motion
to: "off"
for:
hours: 0
minutes: 0
seconds: 5
condition:
- condition: sun
after: sunset
after_offset: "-01:00:00"
enabled: false
action:
- service: light.turn_on
metadata: {}
data:
color_temp: 500
brightness_pct: 100
target:
device_id: 942b00d7c45f9ce9cb9eae1f8be18369
mode: single
This is not working... the light just stays on...
It's a camera that is detecting motion.
Check the trace
I'm totally new to this... what am I looking for in the trace?
To test an automation there's three stages you can follow. Testing the action, the condition and action, and the whole automation:
- Use Configuration -> Automations to find the automation and then select Run in the three dots menu. If this fails your problem is in the
action:section, and details should be found in your log file - Use Developer tools -> Services and call
automation.triggeron the automation withskip_condition: false. If the first passes but this fails then the problem is in yourcondition:block - Use Developer tools -> States to find the trigger entity, click the name, then change the state (at the top) to something that'll trigger the automation before pushing Set State. If this fails then the problem is with your
trigger:section, or the automation is turned off (you can check that in Automations, automations that are turned off will show Disabled)
You can also see this section in the docs about testing and automation traces.
You're looking to see what happened
I run the automation manually. The light turns on immediately. Never goes off.
Right
What else would happen?
- Your automation never turns off the light
Try two automations, one with the on trigger, and another with the off trigger that turns off the light
To make what you've done above work you'll need choose with a trigger id or templates
I guess I don't understand your code:
trigger:
- platform: state
entity_id: binary_sensor.your_motion_sensor_here # change this
to: 'on'
- platform: state
entity_id: binary_sensor.your_motion_sensor_here # change this
to: 'off'
for:
minutes: 2 # adjust as necessary
action:
- service: "light.turn_{{ trigger.to_state.state }}"
target:
entity_id: light.your_light_here # change this
mode: single
is trigger.to_state.state a variable?
Sorry. Before I ask more questions, I should learn the syntax.
That's a template
Well, {{ ... }} is
What you have there will work, but now you can't manually run the automation
So, follow step 3 of the testing
Here is documentation on trigger variables; specifically the link is for trigger variables that are available for state triggers: https://www.home-assistant.io/docs/automation/templating/#state
So in the example code, trigger.to_state.state will be on when the automation triggers from the motion sensor turning on, and it will be off when triggered by the motion sensor turning off
@kind perch Got it. That makes sense.
Would a 'repeat' construct work? Or is that too processor intensive.
repeat turn on light...until motion detection is off.
action:
- service: light.turn_{{ trigger.to_state.state }}
metadata: {}
data:
color_temp: 500
brightness_pct: 100
target:
device_id: 942b00d7c45f9ce9cb9eae1f8be18369
My YAML editor would not keep "light.turn{{ trigger.to_state.state}}"
It would always remove the quotes...
@kind perch @stiff flame Is my syntax not correct?
Using Home Assistant to edit YAML.
The built in automation editor?
Yeah.
Then that's fine
I get this error in the Trace panel:
Error: Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'
You pushed Run?
yup
To test an automation there's three stages you can follow. Testing the action, the condition and action, and the whole automation:
- Use Configuration -> Automations to find the automation and then select Run in the three dots menu. If this fails your problem is in the
action:section, and details should be found in your log file - Use Developer tools -> Services and call
automation.triggeron the automation withskip_condition: false. If the first passes but this fails then the problem is in yourcondition:block - Use Developer tools -> States to find the trigger entity, click the name, then change the state (at the top) to something that'll trigger the automation before pushing Set State. If this fails then the problem is with your
trigger:section, or the automation is turned off (you can check that in Automations, automations that are turned off will show Disabled)
You can also see this section in the docs about testing and automation traces.
Follow step (3)
I eventually did this:
id: '1715670321601'
alias: Toggle Living Light - based on Motion (after Sunset + 3 hrs)
description: ''
trigger:
- type: motion
platform: device
device_id: ---------------------------
entity_id: ---------------------------
domain: binary_sensor
id: living_room_yes_motion
- type: no_motion
platform: device
device_id: --------------
entity_id: --------------
domain: binary_sensor
id: living_room_no_motion
for:
hours: 0
minutes: 10
seconds: 0
condition:
- condition: sun
after: sunset
after_offset: '3:00:00'
action:
- choose:
- conditions:
- condition: trigger
id:
- living_room_yes_motion
sequence:
- service: light.turn_on
metadata: {}
data:
brightness: 255
target:
device_id: -----------------
- conditions:
- condition: trigger
id:
- living_room_no_motion
sequence:
- service: light.turn_off
metadata: {}
data: {}
target:
device_id: -----------------
mode: single
Used the UI to configure this.
Maybe it's overkill and it's not as simple as your original suggestion but it works.
This was the original suggestion. https://community.home-assistant.io/t/motion-activated-lights-automation/608968
FYI devices and entity ids aren't sensitive... knowing them is as useful to an attacker as knowing the IP addresses of your home network aka not at all
And also fyi device triggers and actions are best avoided entirely: