I saw another example of working with templates that looks like this:
template:
- sensor:
friendly_name: "Echo Show Timer"
icon: mdi:timer
state: >-
{% set entity = 'sensor.family_room_echo_next_timer' %}
{% if state_attr(entity,'status') == 'ON' %} active
{% else %} inactive
{% endif %}
attribute:
remaining: >-
{% set entity = 'sensor.family_room_echo_next_timer' %}
{% if state_attr(entity,'status') == 'ON' %}
{% set duration = ((state_attr(entity, 'sorted_active')|from_json)[0][1].originalDurationInMillis/ 1000)| round(0) %}
{{duration//3600}}:{{(duration%3600)//60}}:{{((duration%3600)%60)}}
{% endif %}
end_time: >-
{% set entity = 'sensor.family_room_echo_next_timer' %}
{% if state_attr(entity,'status') == 'ON' %}
{{ states(entity) }}
{% endif %}
The problem i have is that in my sensor.yaml file, I get the “sensor.state is not supported error.” how do i fold this latter example into my sensor.yaml file. Do i need to rewrite all my template sensors (there are lots!)
You have two separate ways to create template sensors, the legacy sensor template platform, or the new template integration.
template: is an integration (like sensor:) it is not a sensor platform (like platform: template found under the sensor: integration). Therefore template: has to be put at the top level of your configuration.yaml file. Like this:
No you do not have to. The legacy template sensor platform continues to be supported and there is no sign of it going away. It is recommended that new sensors be created with the template: integration though (as it supports new features and can be reloaded without restarting home assistant).
Having said that, I did change all my sensor template platform entries over to the new template integration. It took most of one evening and there are some issues, like no longer being able to specify a friendly name that is different from the entity id object id. these can be dealt with using customize.
Ensured the new entity id matched the old one (sensor.lounge_dehumidifier_time_remaining) so I did not have to change it in all my automations and scripts, and I used Customize to change the friendly name to what I wanted (“Time Remaining”).
If I had used
name: "Time Remaining"
I would have had to change the entity id everywhere to sensor.time_remaining. And that is also a very generic entity id that could get confusing as I have other sensors like this.
Wow what a great answer! Thanks Tom, you definitely answered my question and now that i’ve placed that template: under my sensor: section, it’s all working great.
I probably will be neurotic and have to update all my sensors now
legacy template sensors can be reloaded without restarting HA as well. And they are reloaded using exactly the same reload service as the new template sensors use.
I’m not sure why.
Unless you have a sensor that absolutely needs some “new feature” then there really is no benefit to switching them over. And there are downsides as tom described above with entity_id’s vs freindly_names requiring additional effort in customization.