Sensor template syntax

I’ve been trying to do what is mentioned here: Getting difference between value of two sensors
but I cant seem to get a successful config check. I’m using the visual studio code editor to check indents etc. and this appears valid in there:

  - platform: template
    sensors:
      value_template: >-
          {% set masterbed = states.sensor.master_ensuite_sensor_2.state|float %}
          {% set ensuite = states.sensor.master_bedroom_sensor_2.state| float %}
          {{ (masterbed - ensuite)|round(1) }}

But I get this:

Invalid config for [sensor.template]: expected dictionary for dictionary value @ data[‘sensors’][‘value_template’]. Got ‘{% set masterbed = states.sensor.master_ensuite_sensor_2.state|float %} {% set ensuite = states.sensor.master_bedroom_sensor_2.state| float %} {{ (masterbed - ensuite)|round(1) }}’. (See ?, line ?).

What am I missing? I tried pasting the example in the top link and updating the entities etc but it wouldn’t validate either.

I think you need some_name.

  - platform: template
    sensors:
      some_name:
        value_template: >-
            {% set masterbed = states.sensor.master_ensuite_sensor_2.state|float %}
            {% set ensuite = states.sensor.master_bedroom_sensor_2.state| float %}
            {{ (masterbed - ensuite)|round(1) }}

1 Like

Also to prevent errors occurring if the sensors are uninitialised you should use this format to get the states:

  - platform: template
    sensors:
      some_name:
        value_template: >
            {% set masterbed = states('sensor.master_ensuite_sensor_2') | float %}
            {% set ensuite = states('sensor.master_bedroom_sensor_2') | float %}
            {{ (masterbed - ensuite) | round(1) }}

See the warning here: https://www.home-assistant.io/docs/configuration/templating/#states

This is the example that worked for another user:

    sensors:
      indoor_outdoor_temp_diff:
        entity_id:
          - sensor.living_room_temperature
          - sensor.dark_sky_apparent_temperature
        value_template: >-
          {% set esphome = states.sensor.living_room_temperature.state|float %}
          {% set outdoor = states.sensor.dark_sky_apparent_temperature.state| float %}
          {{ (outdoor - esphome)|round(1) }}

What I don’t understand is what goes above the -platform line, do I need to list the entities the template is reading states from, and if I put Sensor: above - platform it errors. So I call it sensor ensuite_diff_humidity: but then when i set some_name: lower as suggested which one does HA see?

If I apply the above mentioned fixes Check config times out.

You don’t have a - platform: line in that example.

If you have the template sensor definition in your configuration.yaml file the only thing above it should be sensor: and that should only appear once.

If you have split your configuration by putting sensor: !include sensors.yaml once in your configuration.yaml file and your template sensor definition is in the sensors.yaml file then nothing goes above -platform:.

No. That is no longer supported.

Because you either have sensor: more than once in your configuration.yaml file or you put it in the !include file (which actually resolves to having declared sensor: more than once).

This is valid:

  - platform: template
    sensors:
      some_name:
        value_template: >
          {% set masterbed = states('sensor.master_ensuite_sensor_2') | float %}
          {% set ensuite = states('sensor.master_bedroom_sensor_2') | float %}
          {{ (masterbed - ensuite) | round(1) }}

See the example here:

Note how the very first comment (light grey) says it is an example for including in configuration.yaml. If it is going in your sensors.yaml file, drop the sensor: line.

It should be sensor: !include sensors.yaml and not sensors: !include sensors.yaml.

Also in all your other examples you say sensors: instead of sensor: :slight_smile:

1 Like

Fixed. Thanks.

Also fixed, thanks.

One follow up question: I didn’t split my files with an include, so I have items like light one: etc to make each entry unique. Is that label for them completely irrelevant? As in if I move to an include file will it change anything?

If you have something like this:

light:
  - platform: mqtt
    command_topic: "office/rgb1/light/switch"

  - platform: group
    name: Kitchen Lights
    entities:
      - light.kitchen_ceiling_lights
      - light.kitchen_under_cabinet_lights
      - light.kitchen_spot_lights
      - light.pendant_lights

You can split it up by moving everything under light: to the new file (e.g. light.yaml) and adding this to the light: line:

light: !include light.yaml

Contents of light.yaml:

- platform: mqtt
  command_topic: "office/rgb1/light/switch"

- platform: group
  name: Kitchen Lights
  entities:
    - light.kitchen_ceiling_lights
    - light.kitchen_under_cabinet_lights
    - light.kitchen_spot_lights
    - light.pendant_lights

If however you have something like this:

light:
  - platform: mqtt
    command_topic: "office/rgb1/light/switch"

light one:
  - platform: group
    name: Kitchen Lights
    entities:
      - light.kitchen_ceiling_lights
      - light.kitchen_under_cabinet_lights
      - light.kitchen_spot_lights
      - light.pendant_lights

You can split it the same way for each light x: definition and have multiple files, or paste them all into the one file as in the first example above and remove all the light x: definitions.

I assumed as much but are you saying the word “one” isn’t really used so if I drop all the iterations of light one: light two: etc and put them all in the light: !include lights.yaml there will be no change to the entities names?

Hello! I’ve been trying to create my first sensor, following your examples, and now it works.
I have created a split configuration.
My sensors.yaml file is:

- platform: template
  sensors:
    corrente_netta:
      value_template: "{{ ((states('sensor.module_din_connesso_1_power')|float) -
        (states('sensor.solaredge_current_power')|float)) |round(3, default=0)
        }}"

It computes the difference between my grid energy and the solar panels.
How could I add the following information:

  • icon
  • a friendly name
  • unity of measurement

If i browse the entities from the interface, it says that i cannot modify it because it is not unique: is that a normal behavior or is it an error?
Thank you,
Fabio

That is a two year old post. You should be using the template integration for new template sensors. It supports unique id.