How to AND together two triggers in an automation?

Scenario:
I’m receiving temperature measurements from sensors in my freezer and in my refrigerator on an RTL-SDR device, which publishes MQTT messages to the local broker and ultimately to Home Assistant. I want to ensure that the entire chain of measurement, from the sensors to HA, is always operating properly. If the RTL-SDR radio, or one of the sensors, or the broker goes dead, I want to know about it before the refrigerator or freezer goes south and all of the food spoils.

Solution:
I have an automation that sends me a text message if either the refrigerator or the freezer sensor does not change for two hours. I figure if no updates are sent for that long then something is wrong, most likely the radio (those things don’t seem to like running 24/7 for months). This works pretty well, except that “updates”, as far as HA is concerned, are synonymous with “changes”. In other words, if the temperature is being reported but it hasn’t changed from the last report, HA just discards it. Due to this, and also due to the fact that the sensors are inside a cold metal box that the RF has to punch through, the automation gets triggered falsely a lot.

What I want to do is only trigger the automation if BOTH the freezer and the refrigerator are not updated for two hours. Normally one or the other will get an update within that length of time. I’m not sure how to do it though. I can have it trigger on one and then check the other in a condition, but I have no way of knowing which may trigger first. Also if, say, the freezer triggers and while the condition is waiting for the refrigerator to time out the freezer gets an update, then the whole automation should be reset. The text should be sent only when an update hasn’t been received from either sensor for two hours or more.

This is the automation that I’m using:

alias: Text And Announce When Freezer Values Do Not Change
description: >-
  Sends a text message when one of the refrig or freezer temperatures
  does not change for two hours.  Indicates a radio problem. Note that it must
  receive at least one update before the timer will start.
trigger:
  - platform: state
    entity_id:
      - sensor.freezer_temperature
    for:
      hours: 2
      minutes: 0
      seconds: 0
    id: frzNoChange
  - platform: state
    entity_id:
      - sensor.refrigerator_temperature
    for:
      hours: 2
      minutes: 0
      seconds: 0
    id: refNoChange
condition: []
action:
  - service: notify.send_sms
    data:
      title: Possible Radio Problem
      message: >-
        The {{ trigger.from_state.attributes.friendly_name }} has not
        updated for over two hours. Check to see if the RTL-SDR device is still
        operating.
 mode: single 

you need to remove all of the ** and then add three backticks (```) on the line both before and after the code block.

but you should add both triggers as conditions (‘and’ by default) so either trigger will start the automation and then check if either condition is satisfied or not.

basically it will be if there is no change for 2 hours firing the first trigger then check that both sensors have been in the same state for 2 hours. if not then don’t run the automation actions. otherwise run the actions.

if i understand what you’re saying, another way to do it in addition to finity’s is to create a 2 hour timer helper. create an automation that resets the timer anytime anything updates. create your text notification on a different automation that is triggered only when the timer expires.

A trigger is a point-in-time event. There is no concept about having two triggers ANDed together.

Trigger off either sensor not changing for 2 hours, then check both in the conditions. Using template condition shorthand for brevity:

trigger:
  - platform: state
    entity_id:
      - sensor.freezer_temperature
      - sensor.refrigerator_temperature
    for: "02:00:00"
condition:
  - "{{ now()|as_timestamp - states['sensor.freezer_temperature']['last_updated']|as_timestamp >= 7200 }}"
  - "{{ now()|as_timestamp - states['sensor.refrigerator_temperature']['last_updated']|as_timestamp >= 7200 }}"
action:
YOUR ACTION HERE

The action will run when the second of the two sensors does not change for two hours.

You didn’t. See here. Three backticks (```) before and after the code.

1 Like

I like this idea the best. I’mma try it and see if it works. Thanks. I’ll mark this as the solution after I test it.

The backticks worked too. I didn’t see a button for it in the toolbar so I tried the bold and italics buttons. Bad choice apparently.

Of course it’s personal preference but I’m not sure why.

That way you have two automations and an extra timer to create to control the same outcome when my way it’s all in one automation and no additional helper.

I guess the reason is because my old brain doesn’t fully grok it. This part confuses me:

I don’t know how to do the bold part from within the condition block. It’s probably easy but I can’t think of how to do it. Helpers with timers are cake.

See my example. Helpers and timers are messy, If your old brain doesn’t like the templates, I think you can do it like this, although I don’t know for certain whether the state condition works without a state: declaration.

trigger:
  - platform: state
    entity_id:
      - sensor.freezer_temperature
      - sensor.refrigerator_temperature
    for: "02:00:00"
condition:
  - condition: state
    entity_id: sensor.freezer_temperature
    for: "02:00:00"
  - condition: state
    entity_id: sensor.refrigerator_temperature
    for: "02:00:00"
action:
YOUR ACTION HERE

So the sequence would go like this:

  • Freezer hasn’t reported for 2 hours! (trigger)
  • Check conditions: first one passes, second one fails, so no action
  • Fridge hasn’t reported for 2 hours! (trigger)
  • Check conditions: this time they both pass, action run.
1 Like

troon already pretty much spelled it out for you in either/both of their posts

Yup.

EDIT:

yeah, good catch. I don’t think it will.

Probably should go the template route.