Linking entities with integration in template?

I was wondering if there is anyway within a template to get the integration the provides an entity?

I’m trying to write a template that provides information on low batteries from the my Zigbee/Zwave devices. I can get a list of device_class battery entities, but this also includes the battery entities for my phone, provided by the companion app.

It would be cool if I could further filter the list of entities based on the integration that provides them. I see that this is done in the front-end, as each entity has it’s providing integration listed there. But, I don’t seem to find anything like this in the template functionality.

An entity’s integration is not included as one of its attributes so that means a template has nothing available to determine the entity’s integration.

One way to meet your requirements is to add a custom attribute to each one of your Zigbee/Zwave devices that indicates its integration. Your template can easily use this new attribute to select entities based on their integration.

Someone else had a similar request. They weren’t interested in the entity’s integration but did want custom battery thresholds for each entity. The solution I proposed relies on a custom battery_threshold attribute for each battery-operated sensor.

You can add as many custom attributes as you need. For example, your customize.yaml file would contain something like this:

sensor.dining_room_dimmer_switch_battery:
  integration: zigbee
  battery_type: 'CR2450'
  battery_threshold: 25

sensor.living_room_dimmer_switch_battery:
  integration: zigbee
  battery_type: 'CR2450'
  battery_threshold: 25

sensor.garage_motion:
  integration: zwave
  battery_type: 'CR123A'
  battery_threshold: 15

Simple template to get all sensors based on zigbee or zwave integration:

{{ states.sensor | selectattr('attributes.integration', 'in', ['zigbee', 'zwave']) | list }}

Thanks for the info.

The only issue I have with the customize.yaml approach is the need to enumerate through every battery entity and maintainability. I seem to have issues with some of my Aqara sensors becoming unavailable and then needing to re-add them through the ZHA integration. My concern is that I go through the effort to setup the customized attribute, but then need to reconnect a device and it somehow gets a different entity id, which I don’t notice, and then the customization won’t work.

Plus, adding a new device means I need to edit a config file…which I’m trying to avoid if possible.

I’m thinking about looking into zigbee2mqtt to see if I have a bit more control about how zigbee entities are presented to HA.

If you don’t want to add a custom attribute then there is no other means of using a template to select entities by their integration.

The alternative is what someone people do but I don’t recommend. They append meta-data to the entity’s name (i.e. add “zigbee” to the entity’s name). Then they create templates that extract entities based on matching sub-strings in the entity’s name. The resulting template isn’t nearly as straightforward as the one I posted above (and even more involved when attempting to match multiple sub-strings). Plus it involves all the editing and maintenance of creating a custom attribute (which you said you didn’t want to do). So all the same effort but with less convenience and an entity with a long-winded name.

Okay, thanks for the ideas. I’ll try implementing them and see what works better for me.

For future reference, integration_entities would work now

@SamJongenelen: Thank you for the tip. For all those following along, here is the documentation for integration_entities.