Sensor with multiple entities, condition trigger with numeric_state

Hello,

I have 3 sensors
I want to trigger an action whenever one of them gets above a fixed value.
I have used the following blueprint but it only triggers when all three are above the set value:

blueprint:
  name: test
  domain: automation
  input:
    humidity_sensor:
      name: humidity sensor
      selector:
        entity:
          domain: sensor
          multiple: True

trigger:
  platform: time_pattern
  seconds: "/10"

condition:
  condition: or
  conditions:
    - condition: numeric_state
      entity_id: !input humidity_sensor
      above: 50

action:
  - service: persistent_notification.create
    data:
      message: >
        value is above 50

edit:
I don’t want to use a numeric_state trigger since this only triggers when changing from below to above and doesn’t keep triggering afterwards

Time pattern triggers have their uses, but they are rarely the best choice.

trigger:
  - platform: numeric_state
    entity_id: 
      - sensor.humidity_1 
      - sensor.humidity_2 
      - sensor.humidity_3
    above: 50
condition: []
action:
  - service: persistent_notification.create
    data:
      message: >
        value is above 50

One thing to note is that the automation will not fire when you set it up just because the humidity is above 50. The numeric state trigger needs the value to change from below 50 to above 50.

1 Like

That’s why I didn’t use it, I want to trigger the actions whenever the value is above 50, no matter what the value or state used to be

Then remove the ‘above: 50’ in the trigger and add a condition template that checks that any of them are above 50.
This will cause it to trigger on any change of temp on any of the sensors, but will only run the automation if one or more of them are above 50.

That could work, how can I use a value_template with a multiple: true template? I didn’t find any example

There are ways to template it. Multiples are presented as a list. you can look in the list for any that meet your criteria.
I don’t generally write code for people, but there are lots of examples out there to iterate over a list and select things that match, count them, and if the count >0 send true.

I tried using the following code but the variable isn’t changed successfully, would you have any pointers?

condition:
  - condition: template
    alias: "Any sensor is above the set value"
    value_template: >
      {%- set condition_was_set = False -%}
      {%- for sensor in (states | selectattr('entity_id','search','^sensor.lumi_humidity')) -%}
        {%- if int(sensor.state) > 2 -%}
          {%- set condition_was_set = True -%}
        {%- endif -%}
      {%- endfor -%}
      {{condition_was_set}}

Are you trying to create a Blueprint or a one-off Automation, because your template doesn’t really make sense for a Blueprint.

In your original post the list of humidity sensor’s would already be known and stored in the humidity_sensor input variable, there’s no need to search through every sensor entity. Assign the value from the input variable to a script variable, using either the trigger_variables or variables key. Then the list can be use in templates.

blueprint:
  name: test
  domain: automation
  input:
    humidity_sensor:
      name: humidity sensor
      selector:
        entity:
          device_class: humidity
          domain: sensor
          multiple: True

variables:
  h_sensors: !input humidity_sensor
trigger: ...

condition:
  - condition: template
    value_template: |
      {{ expand(h_sensors) | map(attribute='state')
      | map('int') | select('gt', 50) | list | count > 0 }}

action: ...




As shown above you don’t need to use a for loop to get the desired information. But, in cases where a loop is necessary, you need to use a namespace to extract the variable value that is set inside the loop after the loop is complete. Since the purpose of the template is just to see if any of them are above the desired value you can also make it a bit more efficient by pre-filtering with an inline if and breaking out of the loop after the first run.

{% set sensors = states.sensor | selectattr('entity_id', 'search', 'sensor.lumi_humidity') %}
{% set ns = namespace(condition_was_set = false) -%}
{%- for sensor in sensors if int(sensor.state) > 50 -%}
  {%- set ns.condition_was_set = true %}{% break %}
{%- endfor -%}
{{ ns.condition_was_set }}
1 Like

Thanks for that. I didn’t know how to force integer, didn’t consider trying to double map.
I knew someone would be able to help them, though.