Template to return all devices in state

Like the title says, I am trying to make a sensor with a template for pulling the names of all devices in a specific state at a specific time.
I can do this by specifying a ton of “if” statements for each device I’d like to look for, but I am hoping it is possible to template out easier and more dynamically.

I used HX711s, and a 16 in by 3 foot board to make a sensor to tell when a cat is possibly eating. I am also using Tiles and ESPresense at the moment for room tracking. I want to make a sensor that basically says "if the weight sensor is triggered, then the state is all devices in the state of “Laundry Room”. So that I know who is in there. I have 3 cats and sometimes they’ll eat together, sometimes they won’t, they’re weirdos.

Can this be done? Or am I writing out an annoying, lengthy “if”?

I’m not sure I get this part:

"if the weight sensor is triggered, then the state is all devices in the state of “Laundry Room”

So perhaps I’ll attempt to rephrase the request:

A list of all of the cat location sensors whose state is “Laundry Room” when one of the weight sensors there is triggered? For that, I would do this:

template:
  - trigger:
      platform: state
      entity_id:
        - binary_sensor.weight1
        - binary_sensor.weight2
        - binary_sensor.weight3
    sensor:
    - name: Cats in laundry room
      state: >-
        {% set sensors = ['sensor.cat_one', 'sensor.cat_two', 'sensor.cat_three'] %}
        {{ expand(sensors) | selectattr('state', 'eq', 'Laundry Room') | map(attribute='name') | list | join(', ') }}

That will update the list of cats in the laundry room whenever one of the weight sensors changes state (assuming they’re binary_sensors and just report ‘on’/triggered or ‘off’/not triggered).

1 Like

Hmm… still requires me to input the variables on what sensor can give the reading, but OMG SO MUCH EASIER THAN WHAT I WAS ABOUT TO DO! Thank you.

EDIT: Just read a little more on template sensors… I didn’t realize I could put triggers in. This opens a whole new world. I think I got it now. Will update with solution thank you for leading me down this path

Glad it helps. If you have a common naming scheme for your sensors, you can use a template to generate them as well. It gets more complicated in a trigger, but the sensor state could start with something like this:

{% set sensors = states.sensor|selectattr('object_id', 'match', 'cat')|map(attribute='entity_id')|list %}

That will generate a list of all entity_ids for sensors whose name starts with “cat”, such as sensor.cat_one.

You could do something similar with the template using a template trigger like this:

{{ states.sensor|selectattr('object_id', 'match', 'weight')|selectattr('state', 'eq', 'on')|list|length }}

Which will trigger whenever the number of “active” sensors that match that naming scheme changes.

1 Like

You are pretty rad. Thank you for your help. Now I need to figure out one more piece to the puzzle, and it’s a little more complicated…
How do I get it to read “Clear” or “Empty” after they jump off? As of this moment this sensor retains the state afterwards.

- trigger:
    - platform: numeric_state
      entity_id:
        - sensor.bed_weight_lbs
      above: '1'
      for: '00:00:10'
        
  sensor:
    - name: Cat Eating
      state: >-
        {% set sensors = ['sensor.hathor_room', 'sensor.pants_room', 'sensor.osiris_room'] %}
        {{ expand(sensors) | selectattr('state', 'eq', 'Laundry Room') | map(attribute='name') | list | join(', ') }}

I would add a second trigger when it goes back to 0, given it a trigger ID, and then use that in the logic.

- trigger:
    - platform: numeric_state
      entity_id: sensor.bed_weight_lbs
      above: '1'
      for: '00:00:10'
      id: eating
    - platform: numeric_state
      entity_id: sensor.bed_weight_lbs
      below: 1
      for: '00:00:10'
      id: left
  sensor:
    - name: Cat Eating
      state: >-
        {% set sensors = ['sensor.hathor_room', 'sensor.pants_room', 'sensor.osiris_room'] %}
        {{ "" if trigger.id == 'left' else expand(sensors) | selectattr('state', 'eq', 'Laundry Room') | map(attribute='name') | list | join(', ') }}
1 Like

Thank you so much, this is exactly what I was looking for!