Total beginner here, so doing all in UI…struggling with the following loop;
I created (blueprint based) an automation to;
a) monitor motion detect status of a camera. When the status changes to detect
b) check whether it’s after sun-down, and
c) turn on a light if sun-down
Now I struggle with turning it off again. I tried;
turning the light off after 120 seconds
using another automation to turn the light off after motion detect is cleared
added a delay AFTER turning the light off, trying to stop the script from continuing (see below)
tried ‘random’ stuff with timers.
However; once the light turns off, the camera picks this up and interprets it as motion, the status changes to motion detected and we loop back to the beginning.
in your off automation, turn off the motion detect automation, then turn off the lights, use a wait template to wait for the motion sensor to clear then turn on the motion detection automation again. You can use the homeassistant.turn_off action to turn off an automation, likewise for on.
Or
in you motion detect automation add a template condition that checks to make sure it has been x seconds since the off automation triggered. Where x is time it takes for your motion sensor to clear.
Failing miserably. I cannot get the condition to evaluate to true. I’ve triple checked the automation, tried to read up on template conditions then ended up in a rabbithole about when it evaluates to true…
Can I ask what the motivation is for this? Is it for you so a light turns on when you go past the camera or is it for visitors/intruders to get a light when they approach your house?
If it’s for you & your family to avoid manually turning on the light, could you trigger the light_on another way. Perhaps by a door sensor, triggering when the door opens. Then you also get the benefit of using a trigger in your “off” automation that says something like “when door sensor is closed for xx minutes”.
There are different schools of thought on this kind of thing, but another idea this kind of automation - You could create a timer helper & start the timer for an amount of time when triggered (motion or otherwise), then turn off the light when the timer ends (idle). Timers are non-volatile. Meaning they survive an HA restart. Pretty sure the built-in time-based triggers & actions (motion off for x minutes, or a wait action) don’t survive a restart.
I’m no automation expert, but here’s how I approach situations like this: a single automation with two triggers and “single” run mode. This one turns the light off 120 seconds after motion stops, then sleep 10 seconds before it can be triggered again.
triggers:
- entity_id:
- binary_sensor.motion_detection
from: "off"
to: "on"
trigger: state
id: turn_on
- entity_id:
- binary_sensor.motion_detection
from: "on"
to: "off"
for:
seconds: 120
trigger: state
id: turn_off
conditions:
- condition: sun
before: sunrise
after: sunset
actions:
- action: homeassistant.{{trigger.id}}
target:
entity_id: light.porch
- delay:
seconds: 10 # avoid re-trigger automation
mode: single
max_exceeded: silent
No, I cannot use another trigger I can think of; the camera(s) monitor entire rooms/spaces; not all entries are monitored with sensors. As soon as motion is detected anywhere in that space, the light should go on.
I tried this, but failed, not sure why. I’ve now deleted that config and am trying the solution suggested by Tom. The last code change seems to work, but since I’m also at work, I’ve not tested in depth yet. Will update later! @tom_l
I am using Imou for these, for this setup it’s a Ranger 4MP.
Unfortunately, I still do not have it working, it still seems to fail on processing the condition (condition failed).
The reason I asked was because I was curious about the camera’s sensor motion status relayed to HA. For instance, Blink cameras have a huge delay because of the cloud. I assume these are local cams, correct?
Yes, they are local; there certainly is a delay, but I am watching them toggle the state (clear to detect and v.v.) on another screen, the delay seems to be only a few seconds.
Oddly enough; before adding the condition they would constantly detect motion and switch the light on, even when the detected motion was the light going off. Now; it throws no error, but the light does not go on.
The camera shows;
cleared (no motion detected)
13:09:04 - 1 minute ago
detected motion
13:08:27 - 2 minutes ago
Older entries are at least 10 minutes old.
The trace shows;
Triggered by the state of binary_sensor.lc_motion_alarm_2 at January 10, 2025 at 13:08:27
Test If template renders a value equal to true
Stopped because a condition failed at January 10, 2025 at 13:08:27 (runtime: 0.00 seconds)
The time in the code suggested is set to 25 seconds
So at 13:08:27 the automation was triggered by state. The previous trigger was at least 10 minutes old, yet the condition failed.
Am I reading that correct? Sorry for being slow, I have almost zero experience with both HA and code.
Thanks all, I am fairly certain it’s not stable! Somewhat related question; if you would work with an OR scenario, would you create 2 automations (1 for trigger A, a duplicate for trigger B), or would it be something like if A OR B then C?