Sensor Question. Template: vs platform: template

Was hoping someone could school me on the right way to implement this.

Today, in my configuration.yaml, i have a

sensor: 
  !include sensor.yaml

In that sensor.yaml file, I have a bunch of sensors organized by platform type.

- platform: feedparser
    name: Politico
    feed_url: https://www.politico.com/rss/politicopicks.xml
    date_format: '%a, %d %b %Y %H:%M:%S %Z'
    scan_interval:
      hours: 3
 - platform: sonarr_upcoming_media
    api_key:xxxxxxxxxx
    host: 192.168.7.95
    port: 8989
    days: 7
    max: 10 
  - platform: template
    sensors:
      cleaner_days:
        friendly_name: "Days till Cleaners"
        value_template: >-
              {{ state_attr('sensor.cleaners','days') }}
      hvac_action:
        friendly_name: "hvac_action"
        value_template: "{{ states.climate.home.attributes.hvac_action }}"

This template sensor is where i have a problem.

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 don’t.

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:

sensor: !include sensor.yaml

template: 
  - sensor:
      friendly_name: "Echo Show Timer"
      icon: mdi:timer
      state:  etc...

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.

e.g.

template:
  - sensor:
      - name: "lounge_dehumidifier_time_remaining" # Customized as, friendly_name: Time Remaining
      icon: "mdi:timelapse"
      state: >
        {% if is_state('switch.lounge_dehumidifier_power', 'on') and (as_timestamp(states('input_datetime.lounge_dehumidifier_stop_time')) - as_timestamp(now())) > 0 %}
          {{ (as_timestamp(states('input_datetime.lounge_dehumidifier_stop_time')) - as_timestamp(now()))|timestamp_custom('%H:%M',false) }}
        {% else %}
          00:00
        {% endif %}

This part:

name: "lounge_dehumidifier_time_remaining"

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.

2 Likes

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 :slight_smile:

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.