Can't get value_template to work

I’m struggling to change the value of a sensor through value_template. This is my configuration.yaml:

sensor:
  - platform: template
    sensors:
      sdc30_temperature:
        friendly_name: SDC30 temperature
        unit_of_measurement: "°C"
        value_template: "{{ states.sensor.sdc30_temperature.state | float - 3.0 }}"

The id of sensor is right as the value_template value gets formatted correctly in the Developer tools → Template editor. The yaml is valid and reloads fine. Is there something wrong I don’t see?

I’m by no mean an expert, but that looks like ‘the old way’ of doing template sensors.
The current documentation suggest something like this:

template:
  - sensor:
      - name: "SDC30 temperature"
        unit_of_measurement: "°C"
        state: "{{ states.sensor.sdc30_temperature.state | float - 3.0 }}"

Btw. The template editor is a really nice tool :smiley:

1 Like

You’re trying to create a Template Sensor that has exactly the same entity_id as another sensor. You can’t do that.

Change the Template Sensor to something other than sdc30_temperature. For example, sdc30_temperature_adjusted.

1 Like

Nice catch! :blush:

To prevent errors please heed this warning:

https://www.home-assistant.io/docs/configuration/templating/#states

Also you should supply a default value for your float filter, an availability template, and a device class (for icon and unit adjustment) and possibly a state class (for long term statistics).

template:
  - sensor:
      - name: "SDC30 Adjusted Temperature"
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement # only include this if you want long term statistics
        state: "{{ states('sensor.sdc30_temperature') | float(0) - 3.0 }}"
        availability: "{{ states('sensor.sdc30_temperature') | is_number }}"
1 Like