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.
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 ?)."
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.
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.
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.
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.
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).
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.
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.