I am using several ESP devices and a lot of Shelly devices, and wanted to have a clear overview of the areas where these are located.
For each device I did set the Area in Home Assistant.
Initially I made a template sensor that is looking at an entity of each device like this:
But then I found out that an entity does not always have to be in the same area as the device itself.
So I decided to get the Area from the device ID like this:
This does work, but it appeared that the sensors are not automatically updated in this case when the Area of the device is changed: I had to go through “Developer tools → YAML → Template Entities” to reload the template yaml code to update the sensors.
So I changed the template sensors in trigger based sensors like this:
This is working fine now, but I wonder why the templates without the trigger updates are not updating automatically?
The HA Template documentation states:
Template entities will by default update as soon as any of the referenced data in the template updates.
So why isn’t this the case here?
Is this due to the nested functions device_id and area_name?
Or is this a bug?
My current version of Home Assistant is 2024.3.0
Who can shed some light on this?
However, even then it would only update the template when the entity’s state value changed and not at the moment when the entity was reassigned to another area.
I don’t know if the behavior for this particular template (monitor no entities) is by design or a bug. It seems like it might be a bug.
Thanks, that’s helpful.
I did test the templates in the Template Editor, but didn’t look for this specific info.
Yes, so as I understand it now, the templates are only automatically updated when the state of an entity is changed (creating an event), and the Area and Device ID properties are not seen as a state of the entity.
So the area_name and device_id functions are not calling the state of an entity and therefor are not automatically updating the template.
Could this be a bug or still missing feature, or is this intentional?
For reference: the area_name and device_id in the template are updated when a Trigger-based Template is used. The documentation states:
Whenever the trigger fires, all related entities will re-render and it will have access to the trigger data in the templates.
So in the case of a Trigger-based template the template is not only looking for state changing events but is re-rendering the entities, finding the changed area name as well.
In a Trigger-based Template Sensor, the entities in the state template aren’t monitored and do not determine when the template is evaluated. The state template is evaluated only when one of the sensor’s triggers is triggered.
In a Template Sensor, the entities in the state template are monitored and determine when the template is evaluated. The state template is evaluated only when one of entities within the template changes state or if the template contains now() or refers to all State objects states or an entire domain of State objects (states.light).