AND function in triggers of automations

It would be nice to have a “AND” function in triggers of a automation so when both/all conditions of triggers are met the action get fired.

Example :

If temperature sensor A get above 50 degrees “AND” temperature senor B get above 70 degrees then trigger action

You can already do this with templates… and… do vote for your own idea :slight_smile:

Just add the triggers in the conditions too.

trigger:
  - platform: numeric_state
    entity_id: sensor.temperature_A
    above: '50'
  - platform: numeric_state
    entity_id: sensor.temperature_B
    above: '70'
condition:
  - condition: numeric_state
    entity_id: sensor.temperature_A
    above: '50'
  - condition: numeric_state
    entity_id: sensor.temperature_B
    above: '70'
3 Likes

I wished I was that experienced to know how to do this with templates :roll_eyes:

Yes but if sensor A is above 50 it will try to trigger, if sensor B then is not above 70 it will not trigger action… then later if sensor B is above 70 but sensor A did fall under 50 it again will not trigger action… and I am not sure what happens if after this sensor A get again above 50 degrees… will it then try to trigger action again… or will it not because already did fail 2 times :thinking:

The problem with that is that triggers are a snapshot of a moment in time - when the trigger goes from false to true.

so if you have ‘and’ triggers unless the two triggers hapen literally at the EXACT SAME instant in time then the triggers can’t work. the trigger that turns true first won’t wait around for the other trigger to be true. That is how conditions work (and even referred to them that way in your post - “both/all conditions”) and should be used as such as noted above.

the work around as has also been mentioned above is to wrap both entities in a template that returns true when both tests have evaluated to true. Then you still only have a single trigger going true but it’s monitoring both entities internally.

Other than those methods already available I’m not sure how you would implement an ‘and’ trigger.

1 Like

Templating with Jinja is something you indeed need to get into and if not done regularly then it will always be a bit challenging. However, the power of it is massive and with quite simple statements you can achieve a lot.
For a trigger template that require temp to be higher then, the template just has to deliver ‘true’ which can be achieved by

{{ states.("YOURTEMPSENSOR") | float > 15 }}

or combining 2 (with my sensors)

{{ states("sensor.sensor_gang_temperature") | float > 20  and states("sensor.nspanel_temperature") | float > 20  }}

The state gives you the value of the sensor but it is a string, the | ‘pipe’ float makes it a number and then you can compare with a number

Template - Home Assistant (home-assistant.io)

EDIT: forgot to add that you should / can test most of this in the Developer tools > Templates tab without needing to reload/start. Since you can also update the states via the States tab you can simulate whatever you like

1 Like

I believe the behavior you want is achieved by this:

trigger:
  - platform: state
    entity_id: 
      - sensor.first
      - sensor.second
condition:
  - condition: numeric_state
    entity_id: sensor.first
    above: 10
  - condition: numeric_state
    entity_id: sensor.second
    above: 20
action:
   ... your actions ...

Its behavior is effectively the same as using a Template Trigger suggested by finity and vingerha.

trigger:
  - platform: template
    value_template: >
      {{ states('sensor.first') | float(0) > 10  and states('sensor.second ') | float(0) > 20  }}
condition: []
action:
   ... your actions ...

Obviously the Template Trigger is more compact but if you are unfamiliar with composing templates, you can use the combination of a State Trigger and Numeric State Conditions as shown.


NOTE

There’s an important difference in the behavior of the two suggested techniques.

The Template Trigger will trigger the moment when both sensors exceed their respective threshold values. If either sensor continues to rise above its threshold, the automation will not be re-triggered (i.e. it won’t execute its actions again). One of the two sensors must first decrease below its threshold and then rise above it in order to re-trigger the automation.

The combination of State Trigger and Numeric State Conditions behaves differently. It will trigger, and execute its actions, when both sensors exceed their respective threshold values and trigger again if the sensor values continue to increase.

1 Like