Need help with trigger AND

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?

trigger:
  - entity_id: binary_sensor.motion_sensor_158d00015b89f6
for: 00:02:00
from: 'on'
platform: state
to: 'off'
  condition:
  - condition: state
entity_id: binary_sensor.motion_sensor_158d000123265f
state: 'off'
for: 00:02:00
  action:
  - alias: ''
data:
  entity_id: group.wc
service: switch.turn_off

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.

- alias: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015b89f6
      from: 'on'
      to: 'off'
      for:
        minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.motion_sensor_158d000123265f
      state: 'off'
      for:
        minutes: 2  
  action:
    service: switch.turn_off
    entity_id: group.wc

Read these documents to understand triggers, conditions, and actions.

Also, don’t mix tabs and spaces when entering your whitespace. Use one or the other. I suggest spaces because it will always be correct.

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.

service: switch.turn_off

change to

service: homeassistant.turn_off

If you want to use “AND” in a trigger, you need to create a value_template trigger. It can be complicated:

Or is pretty simple:

- alias: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015b89f6
      from: 'on'
      to: 'off'
      for:
        minutes: 2
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d000123265f
      from: 'on'      
      to: 'off'
      for:
        minutes: 2  
  action:
    service: homeassistant.turn_off
    entity_id: group.wc

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.

1 Like

Thank you so much! That´s the AND i was looking for.

1 Like

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.

    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
2 Likes

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!

You can use and in the trigger, it just needs to be inside a value_template. Yes it’s harder, but it’s doable.

Thanks @petro, I forgot about that. Makes it a little simpler. :slight_smile:

1 Like

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.

3 Likes