I am a beginner in HA so i hope to get help from here.
So i have 2 motion sensors and i want that the light goes out only when neither of them is detecting motion. Also 1 motion sensor has to detect no motion for 2 minutes and the other one for 5 minutes. Like i understand then if i add many triggers then all of them are on OR state. I need them to be in AND state.
I found something like this on the web but this does not work. The lights will not turn off at all. What am i doing wrong?
The spacing is all off. Yaml is very dependent on whitespace ant the whitespace in your pasted code is all over the place. The format for âforâ in your condition and trigger was not correct either. First âforâ requires hours, minutes, or seconds (or all 3) as as attribute. Second If the yaml requires you to use the 00:00:00 format (Reminder, for does not require this format), it always needs to be in quotes.
Lastly, its always a good idea to put everything in order that makes sense. If you use the automation editor in HA, it will place everything with the same whitespace in alphabetical order. Itâs hard to read like that so youâll never see your issues.
Sorry for the spacing. The spacing in my file is correct but after pasting it here it went wrong. Also i have read those documents several times. There is nothing about AND, OR in trigger section. I have made about 15 automations till now and all of them are working fine. And this one isn´t. I have used timing in this format(00:00:00) in all my automations and i have no problems so far. I use only millisecond like you.
I changed the code with your corrections and now the same thing happens. The lights will not turn off ant all.
To get the âandâ youâre looking for, I think you need to add conditions. E.g.:
(Updated per @petroâs suggestion.)
- alias: Turn off light when no motion detected
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_158d00015b89f6
to: 'off'
for:
minutes: 2
- platform: state
entity_id: binary_sensor.motion_sensor_158d000123265f
to: 'off'
for:
minutes: 5
condition:
- condition: state
entity_id: binary_sensor.motion_sensor_158d00015b89f6
state: 'off'
for:
minutes: 2
- condition: state
entity_id: binary_sensor.motion_sensor_158d000123265f
state: 'off'
for:
minutes: 5
action:
service: switch.turn_off
entity_id: group.wc
This will trigger whenever either the first sensor has been off for two minutes, or the second one has been off for five minutes, but the action will only run when BOTH are true.
FYI, @soulman@pnbruckner: and doesnât need to be specified in conditions, it defaults to and. Triggers treat the lists as or, conditions treat the lists as and. Kinda odd they did that but it saves steps.
It would be really nice if you could choose between and, or in trigger part like it is with conditions. But now my automation works like i wanted and i know how to do things it the future. Thanks for the knowledge!
Triggers respond to events, not conditions. E.g., a switch goes on, not that a switch is on; or a motion detector begins to indicate motion, not a motion detector is indicating motion. By its nature, it doesnât make sense to âandâ triggers. If you did, you would basically be saying âtrigger the automation if these two events happen at exactly the same timeâ which is not normally what youâd want (especially since that would almost never happen.) And, I suspect (although I havenât verified this), HA probably processes events one at a time while itâs determining which automationsâ actions should run, so if thatâs true, then it would be impossible for two events to be true at that same time. This is why it only makes sense to âorâ triggers. âIf this event happens or this event happens, then âŚâ
Regarding @petroâs comment about using âandâ inside a template trigger, that of course is true. However, in that case youâre still not "and"ing triggers, youâre creating a single trigger event which happens when the two (or more) expressions become true. Thatâs not the same as "and"ing two triggers/events.
Think of it this way - the trigger(s) decide when an automation should run, the condition(s) decide if the automation should run (when a trigger event happens), and of course, the action(s) decide what should happen when the automation runs.