Binary sensor template - if any entity with an ID part is ON

I have several Reolink cameras around the house and it can report different motion detection types. I’d like to make a simple binary sensor which goes ON when any of these cameras detects person movement. Each cmaeras has a binary sensor like “binary_sensor.xxx_person detected” where “xxx” is different based on the camera’s name. Later on I’ll add more cameras but I don’t want to forget these cameras be included, so I need a binary sensor which listens automatically for all binary sensors which contains “person_detected” in its entity ID.
I have similar template sensor which counts all the lights with ON state (value_template: “{{ states[‘light’] | selectattr(‘state’,‘eq’,‘on’) | list | count }}”), but currently I don’t want to count anything just get an ON state when any binary_sensor with person_detected is ON, and shows OFF when all of them are OFF (like a group).

You can use the filter “search” in a selectattr() to include only sensors with “person_detected” in the entity ID.

{{ states.binary_sensor 
| selectattr('entity_id', 'search', 'person_detected')
| selectattr('state', 'eq', 'on') | list | count > 0 }}
1 Like

Perfect! Thanks a lot! Somehow can I add a filter to collect entities just in given area(s)? E.g. when the entity is in area: Garden

You can accomplish that by using the expand() and area_entities() functions then filtering for the specific attributes you want.

{{ expand(area_entities('Garden'))
| selectattr('domain', 'eq', 'binary_sensor')
| selectattr('entity_id', 'search', 'person_detected')
| selectattr('state', 'eq', 'on')
| list | count > 0 }}
2 Likes

May I have one more? :slight_smile:
I need another sensor which counts the amont of the devices with low battery (code below). I’d ike to replace the fixed threshold (currently 20 in the example below) to an input number value, let’s call it: input_number.battery_threshold
How can I do this?

      eszkozok_lemerult_elemmel_szama:
        unique_id: lemerult-elemek-szama
        friendly_name: "Eszközök lemerült elemmel száma"
        value_template: "{{ states['sensor'] | selectattr('attributes.device_class', 'defined') 
          | selectattr('attributes.state_class', 'defined') 
          | selectattr('attributes.device_class', '==', 'battery') 
          | selectattr('attributes.state_class', '==', 'measurement') 
          | selectattr('state', 'is_number') | selectattr('state', 'lessthan', '20') 
          | rejectattr('state', 'equalto', '100') 
          | rejectattr('state', 'equalto', '100.0') | list | count }}"

There are many posts covering this issue with a variety of methods… here is one. @smarthomejunkie has a video about a similar sensor on his youtube channel if you decide you want something a little more informational than just a count.

The key to this method is using the map() function to apply another filter (in this case int) to the state of all the selected objects before performing the mathematical comparison. If you do not do this you will be performing a string comparison, which will give you strange results.

      eszkozok_lemerult_elemmel_szama:
        unique_id: lemerult-elemek-szama
        friendly_name: "Eszközök lemerült elemmel száma"
        value_template: |-
         {% set batts = states.sensor | selectattr('attributes.device_class', 'defined') 
         | selectattr('attributes.state_class', 'defined') 
         | selectattr('attributes.device_class', '==', 'battery') 
         | selectattr('attributes.state_class', '==', 'measurement') 
         | selectattr('state', 'is_number') | list %}

         {% set threshold = states('input_number.battery_threshold')|int(0) %}

         {{ batts | map(attribute = 'state')
         | map('int', 0) | select('lt', threshold ) 
         | list | count }}
1 Like

Thank you so much. I saw that video and it is really helpful.