Are multiple Triggers an "or" or an "and"?

Forgive me if this is documented or has been discussed but I cannot seem to find it.

I have a few automation ideas that would benefit from multiple triggers.

For instance, if I am away, if a window or a door or a motion detector goes off, that would indicate intrusion. I would like to create an automation based on that that would work as an alarm. I have seen the alarm integration and with my code locks, I would just rather use them. Door unlocks by code, alarm is shut off. But, if any of those things trip, then I need to be concerned. I do have sensor groups set up for this. I just want to trip on the “or” of those for this process. I also have some other things like if a motion sensor trip OR the door opens, turn on the light. That kind of stuff with “or” for triggers would reduce my automations.

On the other side, I also have an “AND” need where say if window 1, 2, 3, and 4 are opened, assume I want to open the windows and shut down my heating or air conditioning.

Again, I have looked all over and could not find this.

1 Like

You can add multiple triggers to one automation and a condition after that triggers.

The trigger will indicate when the automation should run, the condition will be checked and if validated the event you set will run.

On the other hand you can also create a template sensor which will follow your conditions

That is not what I asked. I am asking if multiple triggers are considered an “OR” or an “AND” and how to change them to the other if I need to.

I can do ORs and ANDs all day in the conditions portion. I need to do this in the trigger section.

5 Likes

Ah, found it…looks like it is an “OR” by default on multiple triggers…now to figure out how to get them to be an “AND”.

4 Likes

Yup. All ‘or’.

You can’t. there is no way to do that and it wouldn’t really make sense either.

triggers are events that happen at an instance in time. Much like the device they are named after - you pull the trigger and at that instant the gun fires.

If the trigger wasn’t an ‘or’ then there would be any number of triggers “waiting” to go off when another trigger happens which doesn’t make sense.

any of the conditions to make a trigger conditional will be located in the aptly named “condition:” section of the automation.

By using correctly configured conditions you can then allow a trigger act like a trigger and use the conditions to check that the conditions are met to run the actions when the trigger fires.

also be aware that conditions in the “condition:” section of the automation won’t wait to be satisfied either. if the trigger fires the conditions are immediately checked and if the conditions all return false then the automation stops. It then needs a new trigger event before it will again check that the conditions are satisfied.

there are conditions you can use in the actions section to create more condition checks (waits, delays, choose, if-then, etc) to control action flow if needed.

4 Likes

Triggers defines when your conditions will be checked.

Triggers are always OR
Conditions are AND unless you explicitly add an OR condition.

In your case, door opening OR windows opening are your triggers, you away is the condition to run the automation.
The automation will never run while you are at home, and won’t check if you are away until a door or window is opened.

For this case you can have a state trigger checking all of your windows (OR) changing states to “opened”, then in your conditions you should check if all windows are open (AND).

1 Like

You’re right, of course. I should have spelled that out better above.

Thanks for clarifying it.

As mentioned by others, you cannot logically AND triggers.

One way to fulfill your “four windows open” requirement is by using a Template Trigger.

alias: example
description: Turn off climate system when all four windows are opened
trigger:
  - platform: template
    value_template: >
      {{ expand('binary_sensor.window1', 'binary_sensor.window2',
         'binary_sensor.window3', 'binary_sensor.window4')
         | selectattr('state', 'eq', 'on') | list | count == 4 }}
condition: []
action:
  - service: climate.turn_off
    target:
      entity_id: climate.main
1 Like

That’s why I suggested to use a template sensor, like group sensor you can use it as a trigger

Why?
Why can’t you use the condition section?

Hello All

If condition are always AND why is there and “AND” condiftion where you can add specific condition test ?

My need is to set a boolean to “away” when both me and my wife are not home.
So I was also looking for an AND trigger, but ended up with this.

Do I need to use the AND condiftion ? or just 2 conditions ?

Another question :
how does the time “for” works ?

let assume this scenario:

  • I leave the home at 7h (the automation should not go to the end)
  • then, my wife leave the home 1h hour after me, at 8h (both condition should match, after she’s been away for 10min or more) then the action should be activated.

What do you think ? would this work ?
thx a lot

alias: Leave home
description: ""
trigger:
  - platform: device
    device_id: 37bf5688103ed10747dadedc04220675
    domain: device_tracker
    entity_id: device_tracker.vog_l29
    type: leaves
    zone: zone.home
  - platform: device
    device_id: fb1895cf0550c3a8100600b12f9e7282
    domain: device_tracker
    entity_id: 5cacd7ca62429a6e102df673eaab23cd
    type: leaves
    zone: zone.home
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.vog_l29
        state: not_home
        for:
          hours: 0
          minutes: 10
          seconds: 0
      - condition: state
        entity_id: device_tracker.sm_a415fanny
        state: not_home
        for:
          hours: 0
          minutes: 10
          seconds: 0
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id:
        - input_boolean.away_etage
mode: single```

If you are the only two on the system, that’s a template binary sensor:

template:
  - binary_sensor:
      - name: "Noone at home"
        state: "{{ states('zone.home') == '0' }}"
        device_class: presence

The and condition is provided for complicated AND / OR constructions, such as “(a AND b) OR (c AND d)”

For your automation, use state triggers instead of device triggers, and do it like this:

trigger:
  - platform: state
    entity_id: device_tracker.vog_l29
    from: 'home'
    for: "00:10:00"
  - platform: state
    entity_id: device_tracker.sm_a415fanny
    from: 'home'
    for: "00:10:00"
condition:
  - condition: state
    entity_id: device_tracker.vog_l29
    state: 'not_home'
  - condition: state
    entity_id: device_tracker.sm_a415fanny
    state: 'not_home'

Triggers ten minutes after each leaving, only proceeds to the action if both of you are not_home.

Thx a lot,

I’m leaving home in 15min, I’ll be testing that :slight_smile:

EDIT : It works like a charm ^^ thx a lot

Whilst you can’t have true ‘AND’ triggers, you can chain multiple triggers together, but the order of execution would determine whether or not the automation fires, such as:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_motion_ac02_iaszone_2
    to: "on"
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id:
          - light.all_kitchen_light_group
        to: "on"

In this case, motion has to be detected, and then some lights turned on. You could put a timeout on the wait_for_trigger if the lights are not turned on within a specified period.

you can if you use a template trigger.

Just create the ‘and’ in the template.

If you mean a trigger template then i don’t think thats how they work.

if I reference 2 entities and combine them with an AND, then iirc, whenever either entities state changes, the trigger template will fire, and the state of both entities at that time will be evaluated. So its not the smae as two triggers firing at once.

I think I’ll say the same.

If you create a template that has an ‘and’ in the logic then it means that it needs both parts to be true before the template (and ultimately the trigger) turns true.

example:

{{ is_state('binary_sensor.sensor_1', 'on') and is_state('binary_sensor.sensor_2', 'on') }}

Since the logic is ‘and’ this will only return true when both sensors are ‘on’.

the template doesn’t need to look for a state change on both entities at the same instant. it just needs to evaluate both entities at a particular moment (which technically is when either entity’s state changes) and if they are both ‘on’ then the logic is satisfied and the template evaluates to true. That in turn sets the trigger to true and the trigger fires.

2 Likes

Having to resort to template triggers or repeating yourself in conditions to do logical ‘and’ is a UI stink, classic case of “you’re holding it wrong”. This should be high on Home Assistant priority list to resolve so conditions, triggers, and actions have a more uniform and intuitive interface.

That said, template triggers (as @finity examples) seem to be the best workaround until HA team gets around to brushing up the UI.

1 Like