Triggering an automation with multiple sensors and time based triggers

Hi. I know that scripting in HA has progressed a lot over the last couple of years and i was wondering if there are any better/more elegant solutions possible to triggering an automation when more than one sensor is in a specific state for a set period of time or longer - other than using a sensor group or very complex templates?

The following State Trigger will trigger when the value of any one of the three sensors changes to 50 and remains 50 for at least 1 minute.

alias: example
trigger:
  - platform: state
    entity_id:
      - sensor.one
      - sensor.two
      - sensor.three
    to: 50
    for:
      minutes: 1
condition: []
action:
  ... etc ...

OP asked for more than one though.

More than one doing what exactly?

Do you mean, in order to trigger, two or more sensors must change to the exact same value for X minutes?

In that case I suggest a Template Trigger … but it may depend on what dinth believes is a “complex template”.

Currently im using something like this:

{{ ( ( (as_timestamp(strptime(states.sensor.date_time.state, ''%Y-%m-%d,
      %H:%M'') ) - as_timestamp(states.binary_sensor.living_room_motion.last_updated)
      ) / 60) | int) >= 20 | int and ...

for each of the sensors.
But with multiple sensors, those templates are getting very long and unreadable. Since a lot of new functionalities regarding scripting have been added to HA in the last couple of years, i was hoping it it possible to do it better/easier (without groups)

This will trigger when any of the three entities has not changed for 20 minutes. The condition block will only allow the flow through to the action if all of the entities have not changed for 20 minutes.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.living_room_motion
      - binary_sensor.kitchen_motion
      - binary_sensor.blue_drawing_room_motion
    for:
      minutes: 20
condition:
  - condition: state
    entity_id:
      - binary_sensor.living_room_motion
      - binary_sensor.kitchen_motion
      - binary_sensor.blue_drawing_room_motion
    for:
      minutes: 20
action:
...

If that’s not what you want, be more explicit. There’s certainly no need for complex templates like that for simple binary sensors.

2 Likes

In your first post you mention a “specific state”. However, in the template you posted, there’s no check for a specific state. The template simply tests if the entity has been in any one state for a set period of time or longer.

So what is the actual requirement: specific or any state?


FWIW, Troon’s solution can handle either way but it’s helpful to know what are the actual requirements for your application.

Apologies that was only the beginning of the template, until now ive been doing a template with the check above for each of the sensors and then (in same template) check for state of each sensor.

The solution proposed by @Troon seems to be genius and simple.

If your goal is to trigger when three motion detectors have continuous reported on for at least 20 minutes, here’s a way to do it with a Template Trigger.

alias: example 
trigger_variables:
  sensors:
    - binary_sensor.one
    - binary_sensor.two
    - binary_sensor.three
trigger:
  - platform: template
    value_template: "{{ set(sensors | map('states')) == set(['on']) }}"
    for:
      minutes: 20
condition: []
action:
  ... etc ..
mode: single

I don’t have a suitable environment to test it (i.e. three motion detectors that are constantly on for 20 continuous minutes) so I tested it with two sensors that report light level.

Two rooms are normally dark. They report 0 shortly after a light is turned off. The test was configured to trigger when both changed to 0 and remained 0 for at least 1 minute. The test was performed four times and all were successful (triggered and sent a notification).

alias: "Template Tester"
trigger_variables:
  sensors:
    - sensor.basement_lux
    - sensor.garage_lux
trigger:
  - platform: template
    value_template: "{{ set(sensors | map('states')) == set(['0']) }}"
    for:
      minutes: 1
condition: []
action:
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: >-
        Template tester triggered. {{ states('sensor.garage_lux') }}, {{ states('sensor.basement_lux') }}
mode: single

1 Like