Unable to add "state_class" into my template sensor

I’ve added a sensor into my configuration.yaml as such:

sensor:
  - platform: template
    sensors:
      gas_meter_counter_m3:
        value_template: "{{ ((states.counter.gas_meter_counter.state | float) / 100) }}"
        unit_of_measurement: "m³"
        device_class: "gas"

but as soon as I add state_class: "total_increasing" beneath the device_class line, the sensor disappears from Home Assistant (doesn’t show up in developer tools or anything either)

What am I doing wrong?

Legacy templates do not support state_class and will not be getting support for it. You should be using the new template integration for new sensors as this is the only one that will receive new features.

configuration.yaml

template:
  - sensor:
      - name: Gas Meter
        state: "{{ states('counter.gas_meter_counter') | float(0) / 100 }}"
        availability: "{{ has_value('counter.gas_meter_counter') }}"
        unit_of_measurement: "m³"
        device_class: "gas"
        state_class: "total_increasing"

The availability template is important for setting the the sensor unavailable when the source sensor is unavailable to prevent weird readings in the energy dashboard.

Also:

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

1 Like

Thanks for this. This fixed my issue.

I do also have two other template sensors set up in a similar fashion. Should I also move these to the new template integration?

sensor:
  - platform: template
    sensors:
      #Flick power price correction
      flick_power_price_dollars:
        value_template: "{{ (states.sensor.flick_power_price.state | float) / 100 }}"
        unit_of_measurement: "$/kWh"
      #Flick power price correction, including GST
      flick_power_price_dollars_gst:
        value_template: "{{ ((states.sensor.flick_power_price.state | float) / 100) * 1.15 }}"
        unit_of_measurement: "$/kWh"

Generally there is no need to move them unless you are missing a feature you need. There is no indication that support for legacy templates will be dropped. Just makes sure to use the new integration for new sensors.

However, if you only have two you might as well move them to the new integration. It’s up to you.

Thanks. Lastly I’m also creating a similar entity to convert the gas m³ consumption into kWh. On my gas bills there’s a conversion factor of 10.99508 to convert it to kWh (and then I’m billed in kWh units), so that’s what I’m trying to add into the Energy dashboard.

I’ve just tried to make a template for this

template:
  - sensor:
      - name: Gas Meter kWh
        state: "{{ states(('counter.gas_meter_counter') | float(0) / 100 ) * 10.99508 }}"
        availability: "{{ has_value('counter.gas_meter_counter') }}"
        unit_of_measurement: "kWh"
        device_class: "gas"
        state_class: "total_increasing"

in Home Assistant frontend, the value shows as “Unknown”

Never mind, I’ve fixed this myself by grabbing the state of the sensor.gas_meter then multiplying it by 10.99508 like so:

template:
  - sensor:
      - name: Gas Meter kWh
        state: "{{ states('sensor.gas_meter') | float(0) * 10.99508 }}"
        availability: "{{ has_value('sensor.gas_meter') }}"
        unit_of_measurement: "kWh"
        device_class: "gas"
        state_class: "total_increasing"

I’m guessing there’s some sort of syntax I’m missing when carrying out multiple equations, but this seems to be fine anyway!

Thanks again for your help.

1 Like

If you ever want to just play around with templates to figure out how stuff works, try doing it in the dev tools section of your home assistant! Instant feedback as to why something is or isnt working!

Open your Home Assistant instance and show your template developer tools.

2 Likes

In addition to what Max said you should have a read of this page:

1 Like