Using a Sensor with state_class as "meassurement" in Energy Dashboard

Hi

I have a API Rest “sensor” that reads the Watt produced by my solar panels each 1 sec. as configured in the code below.

At the moment it is configured with state_class: total and I have also tried with “meassurement” and “total_increase”. None of the configurations sums the data correct so it’s displayed correctly in the Energy Dashboard.

The data look like this in the “history” when it’s configured as “total”.

I have a feeling that I need the sensor to be configured as “measurement” and in some way create a new sensor that used this data that sums it up correctly.

So the question is: does someone have an idea how to create a sensor, based on a API rest call for every 1 second that can be used to sum the total amount of Watt in the Energy Dashboard?

As you can see in the graph below the data on the sensor goes up and down each second according to the amount produced watt.

When compared to the summarized data in the Energy Dashboard the data looks totally off. Produced power goes up and down which is not possible and the total sum for this example is only 3,87 kWh and should be around 34 kWh .

sensor:
  - platform: rest
  name: SonnenAPIV2_Solar_production
  scan_interval: 1
  resource: http://myIP/api/v2/status
  headers:
    Auth-Token: !secret sonnen_api_token
  json_attributes:
    - Production_W
  value_template: '{{ value_json.SonnenAPIV2_Solar_production | default }}'

template:
  - sensor:
      - name: 'Sonnen_Solarpanel_Production_v1'
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
          {{ states.sensor.SonnenAPIV2_Solar_production.attributes["Production_W"] / 1000 | round(1) }}  
 


That is a power sensor, not an energy sensor so you have configured the template sensor incorrectly. However you don’t need a template sensor.

Configure a rest sensor to get the power as its state:

configuration.yaml (not sensors.yaml)

rest:
  - authentication: basic
    resource: http://myIP/api/v2/status
    headers:
      Auth-Token: !secret sonnen_api_token
    sensor:
      - name: SonnenAPIV2_Solar_production
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement 
        value_template: "{{ value_json.response.system.Production_W / 1000 }}"

Then feed this rest power sensor to the Riemann Sum helper to integrate power with respect to time to get energy. Be sure to use method: left. Use this energy sensor in your energy dashboard.

1 Like

Thanks a lot @tom_l this was the trick. I had never found that solution.

Now the data looks great

I ended up though doing a configuration with template still, I tried you simple solution but couldn’t get HA to accept, I had json entity error it said.

So the code that did the trick ended up like (and I converted to W instead of kW) :

sensor:
# Sonnenbatterie APIV2 - Solar Production
  - platform: rest
    name: SonnenAPIV2_Solar_production
    scan_interval: 1
    resource: http://192.168.1.34:80/api/v2/status
    headers:
      Auth-Token: !secret sonnen_api_token
    json_attributes:
      - Production_W
    value_template: '{{ value_json.SonnenAPIV2_Solar_production | default }}'

template:
  - sensor:
      - name: 'Sonnen_Solarpanel_Production_v2'
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement 
        state: >
          {{ state_attr('sensor.SonnenAPIV2_Solar_production','Production_W')|float(0) }} 
        availability: >
          {{ state_attr('sensor.SonnenAPIV2_Solar_production','Production_W')|is_number }}