Trigger for multiple sensors with attribute

I have several binary_sensor.zone_XX, with bateria_baixa attribute.

I want to trigger an automation if any of them change the bateria_baixa attribute from 0 to 1.

trigger:
  - platform: template
    value_template: "{{ states.binary_sensor.zona_33.attributes.bateria_baixa | int == 1 }}"

How can I do this without declaring the sensors separately?

Maybe with “Group”.

trigger:
  - platform: template
    value_template: >
      {{ states.binary_sensor
      | selectattr('attributes.bateria_baixa', 'defined')
      | map(attribute='attributes.bateria_baixa') 
      | map('int', 0)|select('eq', 1) | list | count > 0 }}

I came close to your solution.

{{states 
| selectattr('attributes.bateria_baixa','defined') 
| selectattr('attributes.bateria_baixa', 'eq' , 1 )
|  list | count  }}

Where can I get more information about these map, selectattr options and ways to use them?

Thanks.

In this case the map() filter is used two different ways. The first | map(attribute='attributes.bateria_baixa'), is used to define which attribute is output. The second, | map('int', 0), is used to convert those values to integers. The second might not have been necessary, but you did not say what data type the attribute was, so I included it to cover the bases.

The two best sources for information on Home Assistant templating are:

https://jinja.palletsprojects.com/en/latest/templates/

However, even combined, they do not cover all the methods, functions, and “tricks” that are available. There isn’t a single source that covers all of those… they are sprinkled throughout this forum.

1 Like

I think this will only trigger on the first sensor but not if a second sensor is in matching state?

Correct, it triggers only on the change where the count go from 0 to any value above 0… if you need something that triggers with every additional change you could use something similar to for a template sensor then use a State trigger and template condition to trigger on every increasing state change.

template:
  - sensor:
      - name: Example
        state: >
          {{states | selectattr('attributes.example','defined') 
          | selectattr('attributes.example', 'eq' , 1 ) |  list | count  }}
alias: Increasing trigger value
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.example
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state | int > trigger.from_state.state | int }}"
action:
.....
mode: single
1 Like

Cool! How do I make it a blueprint?
It seemed one template sensor has to be created for each device class?

Not my area of knowledge… I find blueprints kind of pointless.

Open a new topic with a specific question.

1 Like