"modern" style template sensor confusion

I have working sensors now in sensors.yaml:

 - platform: template
    sensors:
      tmobile_mbps_receive:
        friendly_name: "Tmobile 5G Mbps Receive"
        value_template: >-
          {% if is_state('sensor.pfsense_interface_tmo_5g_inbytes_kilobytes_per_second', 'unknown') %}
            unknown
          {% else %}
            {{ (((float(states('sensor.pfsense_interface_tmo_5g_inbytes_kilobytes_per_second')) * 8192) / 1000000) | round(2)) }}
          {% endif %}
        unit_of_measurement: "M"

      tmobile_mbps_send:
        friendly_name: "Tmobile 5G Mbps Send"
        value_template: >-
          {% if is_state('sensor.pfsense_interface_tmo_5g_outbytes_kilobytes_per_second', 'unknown') %}
            unknown
          {% else %}
            {{ (((float(states('sensor.pfsense_interface_tmo_5g_outbytes_kilobytes_per_second')) * 8192) / 1000000) | round(2)) }}
          {% endif %}
        unit_of_measurement: "M"

However when I try to reformat the sensors.yaml file for “modern” style
I get an error:
Invalid config for ‘sensor’ at sensors.yaml, line 1: required key ‘platform’ not provided
I have not idea what to enter for “platform”

Here is my attempted modified sensors.yaml file:

- sensor:
  - unit_of_measurement: M
    default_entity_id: sensor.tmobile_mbps_receive
    name: Tmobile 5G Mbps Receive
    state: "{% if is_state('sensor.pfsense_interface_tmo_5g_inbytes_kilobytes_per_second',
      'unknown') %}\n  unknown\n{% else %}\n  {{ (((float(states('sensor.pfsense_interface_tmo_5g_inbytes_kilobytes_per_second'))
      * 8192) / 1000000) | round(2)) }}\n{% endif %}"

- sensor:
  - unit_of_measurement: M
    default_entity_id: sensor.tmobile_mbps_send
    name: Tmobile 5G Mbps Send
    state: "{% if is_state('sensor.pfsense_interface_tmo_5g_outbytes_kilobytes_per_second',
      'unknown') %}\n  unknown\n{% else %}\n  {{ (((float(states('sensor.pfsense_interface_tmo_5g_outbytes_kilobytes_per_second'))
      * 8192) / 1000000) | round(2)) }}\n{% endif %}"

Please read the deprecation notice and check that you have not committed one of the common errors listed in the top post:

Your error:

The instructions in the repair say to put the new configuration in configuration.yaml, not sensors.yaml. That file is most likely included under the sensor integration, not the template integration. The current syntax needs to be under the template integration.

Legacy

Here’s a brief review of the “legacy” way of defining a Template Sensor in configuration.yaml.

sensor:
  - platform: template
      sensors:
        my_kitchen_temperature_sensor:
          friendly_name: "Kitchen Temperature"
          unique_id: abc123xyz
          value_template: "{{ <whatever> }}"
        my_bedroom_temperature_sensor:
          friendly_name: "Bedroom Temperature"
          unique_id: def456uvw
          value_template: "{{ <whatever> }}"

You can optionally move the Template Sensor definitions out of configuration.yaml and into a separate file. In that case, you would create a file called sensors.yaml (or whatever name you prefer) and enter the following into configuration.yaml.

sensor: !include sensors.yaml

Here is what you enter into sensors.yaml. The first line no longer includes sensor: as it did when it was defined within configuration.yaml.

  - platform: template
      sensors:
        my_kitchen_temperature_sensor:
          friendly_name: "Kitchen Temperature"
          unique_id: abc123xyz
          value_template: "{{ <whatever> }}"
        my_bedroom_temperature_sensor:
          friendly_name: "Bedroom Temperature"
          unique_id: def456uvw
          value_template: "{{ <whatever> }}"

Modern

Let’s compare it to the “modern” way of defining a Template Sensor in configuration.yaml.

template:
  - sensor:
      - default_entity_id: sensor.my_kitchen_temperature_sensor
        name: "Kitchen Temperature"
        unique_id: abc123xyz
        state: "{{ <whatever> }}"
      - default_entity_id: sensor.my_bedroom_temperature_sensor
        name: "Bedroom Temperature"
        unique_id: def456uvw
        state: "{{ <whatever> }}"

Modern uses different key names but most important of all is that the primary key is now template: and not sensor: like in the legacy method. That’s because in “modern” format you define all kinds of Template entities (sensors, binary_sensors, trigger-based, select, covers, etc) under the template: key whereas in “legacy” you can only define sensors under the sensor: key.

You can optionally move the Template Sensor definitions out of configuration.yaml and into a separate file. In that case, you would create a file called templates.yaml (or whatever name you prefer) and enter the following into configuration.yaml.

template: !include templates.yaml

Here is what you enter into templates.yaml. Note that the first line no longer includes template: as it did when it was defined within configuration.yaml.

  - sensor:
      - default_entity_id: sensor.my_kitchen_temperature_sensor
        name: "Kitchen Temperature"
        unique_id: abc123xyz
        state: "{{ <whatever> }}"
      - default_entity_id: sensor.my_bedroom_temperature_sensor
        name: "Bedroom Temperature"
        unique_id: def456uvw
        state: "{{ <whatever> }}"

Now that you know the structural differences between the two formats, it explains why you are getting an error message. You are attempting to define Template Sensors using modern format but under the sensor: primary key as opposed to the template: primary key. Your sensors.yaml file is currently included under the sensor: key. Your modern sensor definitions belong under the template: key.

I suggest you rename your sensors.yaml file to templates.yaml. Ensure it only contains definitions in modern format. In configuration.yaml, replace sensor: !include sensors.yaml with template: !include templates.yaml and restart Home Assistant. Check the log for errors.

5 Likes

This is still confusing to me.

In my configuration.yaml, I have tried this:

template: 
  - binary_sensor: !include includes/templates/binary_sensor.yaml
  - sensor: !include includes/templates/sensor.yaml
  - trigger: !include includes/templates/trigger.yaml

Because I would like to keep each type of template in separate files. binary_sensor and sensor work, and are formatted like this within the include files:

- default_entity_id: sensor.my_jam
  state: '{{ 1==1 }}'
  name: my_jam

But I cannot get trigger to work.

Ideas?

trigger is not an entity type.

I do it like this:

template: !include_dir_merge_list templates/

templates is a directory I created below the config directory and contains multiple template configuration files.

It’s easier to maintain because you don’t need to explicitly list each file name plus any new file you create in the templates folder is automatically included after an explicit restart or reload (and it doesn’t have the problem you encountered for trigger-based template entities).

3 Likes

This is working. Thanks so much!