State_class: “measurement” for platform: rest

Hi all,

I want to add Long-term Statistics to my temperature Rest sensors

in configuration.yaml i have this:

sensor: !include_dir_merge_list myconfig/sensor/

and in sensors file

- platform: rest
  name: LAB Temperature
  device_class: temperature
  state_class: measurement      # without this it works ok
  json_attributes:
    - temperature
    - battery
  resource: http://192.168.90.10/api/devices/11497/state
  value_template: '{{ value_json.temperature }}'
  verify_ssl: false
  unit_of_measurement: '°C'
  force_update: true

But I have following errors:

Invalid config for [sensor.rest]: [state_class] is an invalid option for [sensor.rest]. 
Check: sensor.rest->state_class. (See ?, line ?).

Any idea what is wrong?

Have you tried adding state_class as a customization either through the UI or in configuration.yaml? I had a couple sensors that did not automatically have the state_class added, but I added it through the UI and it is working.

1 Like

Hi,
I have the same problem. I managed to read out my solar panel production via REST, but now I am surprised to find that REST sensors can’t be configured with state_class, if I try, I get this:

Invalid config for [rest]: [state_class] is an invalid option for [rest].

I see a bunch of other people having encountered the same issue here: Energy: Not showing the expected entities for consumption - #8 by jmad

So I cross my fingers it will be added soon.

I’m also trying to add a REST sensor to the energy page. Looks like it isn’t implemented yet for REST.

I have opened a feature request. Energy: Support for REST sensors
Please vote for it! :pray:

I managed to add my rest sensor to the energy dashboard using a template sensor:

Here’s my rest sensor that reports daily kWh generated by the solar panels:

This is my setup on the file sensors.yaml, but i think it could also be used on configuration.yaml

  - platform: rest
    name: Solis Today Generation 
    resource: http://192.168.1.53:8000
    value_template:  '{{ value_json.energy.today }}'
    unit_of_measurement: 'kWh'
    device_class: energy

Then on my configuration.yaml I created a template sensor as follows:

template:
  - sensor:
      - name: "Energy Solar Generation Daily"
        unit_of_measurement: 'kWh'
        state: >
          {{ (states("sensor.solis_today_generation") | float) | round(2) }}       
        device_class: energy
        state_class: measurement      
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

What you get from that template sensor is a copy of the rest sensor but it will show up on the energy dashboard because this “new” template sensor will have the required attributes to be detected by the energy dashboard:

device_class: energy
        state_class: measurement      
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

Hope this helps!

My prayers have been heard - the 2021.11 release of HA adds support for device_class attribute for REST sensors! I have been able to successfully apply it to my solar panel REST sensors.