#selecting entities based on the presence of other entities on the same device

1 messages · Page 1 of 1 (latest)

trim sand
#

I'm trying to select the battery entities of certain device types only, e.g. switches. The only way I can think of to do that is to look at all entities of the same device and see if there is an entity of a certain device type, e.g. switch, present on the same device, but I have no idea how to do even that.

Is this even possible? Or am I going about this in entirely the wrong way?

gentle glen
#

Something like this?

{{ states.switch | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') | list }}
trim sand
#

That unfortunately won't work. You see, what I have is a device with two entities one with the device class battery and one with the device class temperature. I want to select e.g. all the sensor entities with certain characteristics (e.g. a certain device class) that belong to a device which also has another entity with certain defined characteristics.

#

afaik, the battery is always in the sensor domain.

#

Maybe I should give you a bit of background here: I've got a dashboard which displays all battery states, and for ease of reading I'd like to group same ones together.

wintry dragon
#

I think you need to refine what "switch device type" actually means. AFAIK that's not a thing. Lots of devices might have switch entities that are not switches. Like any kind of configuration setting would present as a switch entity.

trim sand
#

Yeah, let's forget about the switch. That was just an example that popped into my mind because I have a lot of battery-operated switch modules. It might as well be a thermometer or a plant sensor.

#

selecting entities based on the presence of other entities on the same device

#

I could generalize my requirement to "selecting entites based on the presence of other entities on the same device", and I've renamed the post accordingly.

#

... I'm digging under the fence again, ain't I?

gentle glen
#

I mean ... yes, it's possible but you're likely not going to get a general solution since the criteria are going to make up the majority of the logic.

You're likely start the the set of "presence of other entities" and resolve that to a set of devices, and then expand that to the set of all entities across those devices and then filter down to the ones you care about.

#

Obviously an example... but something along the lines of your initial request

{% set devices = states | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') | map(attribute='entity_id') | map('device_id') | select('!=', None) | unique | list %}
{{ devices | map('device_entities') | sum(start=[]) | expand | selectattr('domain','eq','switch') | list }}