RESTful sensor - how to integrate properly?

Hi!

I use vzlogger to grab my energy-meter readings from a german “smart”-meter. This works very well. vzlogger provides a simple json output (shortened, there are more uuids in data):

curl -X GET http://192.168.178.202:8084
{
"version": "0.8.0", 
"generator": "vzlogger",
"data": 
  [ { 
    { "uuid": "198c1340-3014-11eb-84e0-6f5816942e51", "last": 1634466608156, "interval": -1, "protocol": "sml", "tuples": [ [ 1634466608156, 32.369999999999997 ] ] 
  } ]
}

In my configuration.yaml I grab those readings:

  - platform: rest
    resource: http://192.168.178.202:8084/
    value_template: >
      {% for i in value_json.data %}
        {% if i.uuid == "198c1340-3014-11eb-84e0-6f5816942e51" %}
           {{ '%.2f'%(i.tuples[0][1]) | float }}
        {% endif %}
      {% endfor %}
    method: GET
    name: "P_L3"
    unit_of_measurement: W
    device_class: power

Wich works nice:

grafik

But I would really like to integrate one of these values into the “Energy” area, so I can see my distribution. But as it happens, I am not shure how to set up my consumption, as the entity does not show. I do of cause also monitor the increasing total in kWh (not shown in picture).

Could you please help me to understand, how I can add my grid-consumption?

Best regards

Pascal

Let us know how the energy entity is created and defined ? This is the most important thing to be shown in the energy monitor dashboard. Entities expressed in Watts are “consumption” values, you only display energy in the dashboard (expressed in kWh).

This is how I declared the energy entity:

#Zählerstand Verbrauch
sensor:
  - platform: rest
    resource: http://192.168.178.202:8084/
    value_template: >
      {% for i in value_json.data %}
        {% if i.uuid == "75b1bc80-272b-11eb-8698-9344f05d7094" %}
           {{ '%.2f'%(i.tuples[0][1]) | float }}
        {% endif %}
      {% endfor %}
    method: GET
    name: "Zählerstand Verbrauch"
    unit_of_measurement: Wh
    device_class: energy

Does nobody has a suggestion? Do I need to create a full custom integration to be able to add my data to the Energy-area and if this is so, why is a simple sensor not enougth?

Is it power (W) or energy (Wh) that is being reported, actually? You first and second version differ.

Adding state_class: total_increasing is impossible:

Invalid config for [sensor.rest]: [state_class] is an invalid option for [sensor.rest]. Check: sensor.rest->state_class.

(When checking the configuaration)

My /config/configuration.yaml :

#Zählerstand Einspeisung
  - platform: rest
    resource: http://192.168.178.202:8084/
    value_template: >
      {% for i in value_json.data %}
        {% if i.uuid == "d5940e30-0cbc-11ec-aa10-5bd2872b2c55" %}
           {{ '%.2f'%(i.tuples[0][1]) | float }}
        {% endif %}
      {% endfor %}
    method: GET
    name: "Zählerstand Einspeisung"
    unit_of_measurement: Wh
    device_class: energy
    state_class: total_increasing

So it is energy.


But I do also have power available. So I tried:

   - platform: integration
     name: energy_spent
     source: sensor.p

Wich does at least turn up so I can add it. But I still don’t get, why my first sensor with rest is unavailable?! I would really like to use the data provided by my energy meter directly instead of re-calculating it.

you add state_class: total_increasing through customization, not in the rest platform

Arf. Deserves a PR imo.
If you can set the device_class on something, you should be able to set the state_class as well nowadays.

Thats the solution. I really tried to figure this out by reading the documentation, but I did not get it… So my new config looks like this:

homeassistant:
  customize:
    #grid consumption
    sensor.zahlerstand_verbrauch:
      state_class: total_increasing
    #grid return
    sensor.zahlerstand_einspeisung:
      state_class: total_increasing

sensor:
  #grid consumption
  - platform: rest
    resource: http://192.168.178.202:8084/
    value_template: >
      {% for i in value_json.data %}
        {% if i.uuid == "75b1bc80-272b-11eb-8698-9344f05d7094" %}
           {{ '%.2f'%(i.tuples[0][1]) | float }}
        {% endif %}
      {% endfor %}
    method: GET
    name: "Zählerstand Verbrauch"
    unit_of_measurement: Wh
    device_class: energy
  #grid return
  - platform: rest
    resource: http://192.168.178.202:8084/
    value_template: >
      {% for i in value_json.data %}
        {% if i.uuid == "d5940e30-0cbc-11ec-aa10-5bd2872b2c55" %}
           {{ '%.2f'%(i.tuples[0][1]) | float }}
        {% endif %}
      {% endfor %}
    method: GET
    name: "Zählerstand Einspeisung"
    unit_of_measurement: Wh
    device_class: energy

Thanks for helping me out!

I’m wondering if state class needs to be extended to the base sensor platform vol validation and the base sensor class. Worth a chat with the dev team

Then we wouldn’t need prs on every integration

Or it could be as simple as 2 imports