RESTful Energy sensor

Hi all,

I’m trying to create an energy sensor, to be used in energy dashboard, which gets data via REST from another application running on my raspberry.
The sensor is defined as following:

  - platform: rest
    resource: http://192.168.1.165/metern/programs/programtotal.php    
    name: solar_immissione_giornaliera
    value_template: '{{ value_json.Dailycounter4 }}'
    unit_of_measurement: Wh
    state_class: total_increasing
    device_class: energy
    unique_id: "immissione_giornaliera"

It works, but I realized that the unit of measurement provided by the “programtotal.php” provides values as a raw value up to 999 Wh, and then it returns valued like “1 k”.
So the sensor raises up to 999 Wh, and then falls to 1, since apparently HA remove the “k” after the figure.
See picture:
image

How can I configure the sensor for getting both the value and the unit (k), and when the k is present it should multiply the value by 1000.

Thanks!

What is exactly in your json? You will need a value_template with conditions, but is there any indication of W and kW what you can read?

Can you share the json outputs with Wh and kWh? Otherwise hard to guess how to template it…

Right now the json is this:


{"Totalcounter1":"18.650,140 k","Dailycounter1":"11,6 k","Totalcounter2":"16.327,305 k","Dailycounter2":"15,0 k","Totalcounter3":"406,305 k","Dailycounter3":"7,9 k","Totalcounter4":"607,312 k","Dailycounter4":"4,4 k","Totalcounter5":"306,688 k","Dailycounter5":"7,2 k","Totalcounter6":"113,102 k","Dailycounter6":"9,0 k","Totalcounter7":"--","Dailycounter7":"--","Totalcounter8":"165.610.587 ","Dailycounter8":"127.513 ","Totalcounter9":"307.642 ","Dailycounter9":"347 "}

So basically when we pass 999, 1000 is written in the json as 1 k, and then 1,1 k.
Right now the value in HA is this:

image

I tried to modifiy the template as follow:

 - platform: rest
    resource: http://192.168.1.165/metern/programs/programtotal.php    
    name: solar_immissione_giornaliera
    #value_template: '{{ value_json.Dailycounter4 }}'
    value_template: >-
        {% if 'k' in states('value_json.Dailycounter4.split(' ')[1:2]') %}
          '{{ value_json.Dailycounter4[0:4] | float * 1000 }}'
        {% else %}
          '{{ value_json.Dailycounter4.split(' ')[0:1] | float}}'
        {% endif %}     
    unit_of_measurement: Wh
    state_class: total_increasing
    device_class: energy
    unique_id: "immissione_giornaliera"

It works in template “tester” in developer options, but apparently it doesn’t actually:

Instead of that, try this:

{% if value_json.Dailycounter4.split() | last == "k" %}
  {{ value_json.Dailycounter4.split() | first | float * 1000 }}
{% else %}
  {{ value_json.Dailycounter4.split() | first | float}}
{% endif %}

It seems to work, the issue now is that the value is 4,4 and not 4.4 (comma instead of dot), and it’s giving me a conversion to float error:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.10/site-packages/homeassistant/helpers/template.py", line 1701, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: '4,4'

Strange is that if I just create the sensor with the full value of the json (i.e. 4,4 k), the sensor properly works, getting just the first figure.

I tested the code by changing the , to . as I assumed you have a different localisation settings, where , is the decimal point.

So replace the . with nothing and the , with .

It should work with any value, where you have originally . for separating every thousand, million and with the decimal point ,


{% if value_json.Dailycounter4.split() | last == "k" %}
  {{ value_json.Dailycounter4.split() | first | replace('.','') | replace(',','.') | float * 1000 }}
{% else %}
  {{ value_json.Dailycounter4.split() | first | replace('.','') | replace(',','.') | float}}
{% endif %}

Thanks! It works like a charm now.
Yes, we always have this localization issue in Italy with . and ,

1 Like