Getting list of all sensor entities that belong to devices of a given model

I am building an automation that handles click, hold and release events from a switch of a specific hardware model. For the action part, the automation just fires a new click or hold events, attaching the entity_id of the sensor entity that clicked the button as event data. This approach gives me an abstraction layer between the buttons and the lights that they will ultimately control.

To make the automation more flexible, I would like a piece of template Jinja code that will get a list of all sensor entities that belong to devices of a given model. This will allow me to add new switches to the system by just assigning an area to them.

In this post I found a clever little piece of code by @Troon that finds devices of a given model. I’m kinda new to Jinja so I haven’t been able to figure out how to modify it to get the sensor entities that belong to these devices.

Any help would be greately appreciated. Thanks :slight_smile:

This will return a list of lists:

{{ set(states
       |map(attribute='entity_id')
       |map('device_id')
       |reject('none'))
   |select('is_device_attr','model','YOUR_MODEL_NAME')
   |map('device_attr','id')
   |map('device_entities')
   |list }}
1 Like

You can use sum(start=[]) to collapse a list

2 Likes

Fantastic, thanks! In that case, including a filter to only show sensor entities:

{{ set(states
       |map(attribute='entity_id')
       |map('device_id')
       |reject('none'))
   |select('is_device_attr','model','YOUR_MODEL_NAME')
   |map('device_attr','id')
   |map('device_entities')
   |sum(start=[])
   |select('match','sensor')
   |list }}
1 Like

This is so cool. Thanks a lot, guys!