Json template to pull a list of entities from a group into entity_id section

I am trying to make a json template that can pull all entities within a group and put them into a list under entity_id. Then the template sensor can update it’s state whenever there is a state change of those listed entities. I tried to use the group as an entity but it will not update it’s state every time a new light is on.

  - platform: template
    sensors:
      count_lights_on:  
        entity_id:
          "- {{ state_attr('group.all_lights', 'entity_id' )|join('\n- ')}}"
        value_template: "{{ states|selectattr('entity_id','in',state_attr('group.all_lights','entity_id'))|selectattr('state','eq','on')|list|count }}"

When I put this into my config.yaml I got this:

  • Invalid config for [sensor.template]: Entity ID {{ state_attr(‘group.all_lights’, ‘entity_id’ )|join(’ - ‘)}} is an invalid entity id for dictionary value @ data[‘sensors’][‘count_lights_on’][‘entity_id’]. Got ["{{ state_attr(‘group.all_lights’, ‘entity_id’ )|join(’\n- ')}}"]. (See ?, line ?).
  • Invalid config for [sensor.template]: Entity ID - {{ state_attr(‘group.all_lights’ is an invalid entity id for dictionary value @ data[‘sensors’][‘count_lights_on’][‘entity_id’]. Got “- {{ state_attr(‘group.all_lights’, ‘entity_id’ )|join(’\n- ')}}”. (See ?, line ?)."

Sorry for my bad json skills.

I think you mean jinja not json template. In any event, you can’t template an entity_id. As you are already aware listing only the group will not trigger a proper update for the sensor you will need to list all the lights.

Try:

{{ state_attr(‘group.all_lights’,‘entity_id’)|count }}

That will count the lights but not the lights on The provided error message points to the entity_id template.

Ah yes, missed the on part. can be tackled with an expansion like I posted and then a loop over the list to see if on.

{% set group_id = 'group.all_lights' %} 
{% set on_states = ['on'] %} 
{{ states | selectattr('entity_id','in', state_attr(group_id, 'entity_id')) | selectattr('state','in',on_states) | map(attribute='entity_id')|list|count }}

You can expand the group, select items whose state is on, convert to a list then count the items in it.

{{ expand('group.all_lights') | selectattr('state', 'eq', 'on') | list | count }}

However, this is of limited utility in a Template Sensor because it contains no identifiable entities. This is true for all the templates presented in this thread so far. Without identifiable entities, Home Assistant cannot assign listeners. That means the Template Sensor is only evaluated at startup and never again (until restart).

The way I mitigate it is by explicitly listing all the entities I want to be monitored by the Template Sensor.

{{ [ states.light.kitchen,
     states.light.bedroom,
     states.light.porch ]
   | selectattr('state', 'eq', 'on') | list | count }}

Home Assistant can identify the entities in the template and assign listeners to them. So when any one of them changes its state it will cause the Template Sensor to be refreshed.

Could you please explain a bit more on what you mean here?
I already have a template to count how many entity in this group is on, but need a easy way to update this sensor. I can put every entity in the group under the entity_id field one by one, but I am wondering if there is a easy way to also use a template in the entity_id field so in the future if I add more things to the group it will automatically be added into the list and cause an update once the state has changed.

no you have to list the lights as I wrote originally, sorry.

I am not sure exactly what you mean here. I already have a working template that counts how many light is on within the group and I managed to force the sensor template to update by manually putting every light within the group in a list under the entity_id field. Here I am trying to find a template to automatically change what’s under the entity_id field so when I add more light into the group in the future it will also be updated in the sensor template which will cause an update.

Ps. I can put sensor.date in the entity_id field to cause the sensor to update every minute but it’s not instant(once/min instead of when the state of an light is changed)

Even if entity_id allowed templating, there’s no value to do that. The entities have to be defined at startup when Home Assistant first examines the template and assigns listeners to all identifiable entities. Then it proceeds to evaluate the templates.

I just set it to update on sensor.time, so once a second. Not perfect, but good enough to display lights on in the gui.

Using sensor.time is the usual mitigation technique to compensate for a template with no identifiable entities. However, sensor.time updates every minute (not every second). For my purposes, that’s a bit slow.

The best solution is a PR that is currently in the works. When Home Assistant examines the template to identify entities, it will first evaluate the expand function and then assign listeners to the members of the group. Whenever that PR is implemented, it will allow one to use the template in my previous post (that expands the group).

1 Like

You sure it is minute? I get better resolution then that. And if I display sensor.time it is 1 second resolution.

Last time I checked, sensor.time displays HH:MM in its state. That state value changes every minute. It has no attributes that change state more frequently than its state.

You are correct. I just double checked and my HH:MM:SS is with another sensor which is my entity to watch.

But what you propose would be much cleaner as my method is a dirty hack!

I’m playing with expand but can’t get a list of the entities

There you go.

FWIW, the output of the expand function is a generator, not a list.

Which sensor is giving you HH:MM:SS? I want to do the same if it’s possible.

Thanks a lot! I will keep my eyes on the release notes.
By the way, when using your template after the PR is implemented. What should be under the entity_id? If I understand correctly the template should be under value_template, right?

Rather than use the word “when” let’s use “if” the PR is implemented. It was first implemented in version 0.110 but didn’t work correctly. It was discovered that, on startup, groups must be configured before Template Sensors … and that wasn’t guaranteed. So until it can be guaranteed, and we are now at version 0.113, the correction remains pending.

So if the PR is implemented, there will be no need to specify entity_id.

1 Like