Riemann integration for discharge and loading behave different

Element 4, 6kwh, 5 years old: 75%. Which battery type do you have and how old?

Varta Element (don’t know version, but it was installed 2020): ~ 73% calculating with the new values from the json file.

@mxwi That someone was actually me. @KNXBroker and I went on a little debugging excursion in the webinterface of the battery and discovered this “Endpoint”. At least this way we have the possibility to get the values from the battery and do not have to use the Riemann integration.

But to answer your question, my battery is brand new (Element S5) and I got around ~82% for now.

I hope hat this get’s added to the integration, it would be awesome if this would just work out of the box. But in case you’re interested I wrote a litte integration configuration to make it work in HA Energy (however I’m pretty sure my code could be improved upon, I’m really no expert in converting JS to JSON / readable format with jinja2)

If you are using node red, here some code to get the energy.js data:

[{"id":"4cbf71c03cf0e4c9","type":"function","z":"42a5d63b.36e0c","name":"VARTA Netzbezug","func":"/*\n\nPattern codes:\n--------------\nEGrid_AC_DC == Grid -> Home (Wh)\nEGrid_DC_AC == Home -> Grid (Wh)\nEWr_AC_DC == Inverter AC -> DC == Total Charged (Wh)\nEWr_DC_AC == Inverter DC -> AC == Total Discharged (Wh)\nChrg_LoadCycles == Charge Cycle Counter\n\nExample query:\n--------------\nEnergy Data: curl -X GET http://Varta-IP/cgi/energy.js\n\nExample answer of battery:\n---------------------------\nEGrid_AC_DC = 6135021;\nEGrid_DC_AC = 7717033;\nEWr_AC_DC = 2558221;\nEWr_DC_AC = 1878959;\nChrg_LoadCycles = [386];\n\n*/\n\nvar code = \"EGrid_AC_DC\";\nconst pattern = new RegExp(code + \" = (.*?);\");\nvar found = msg.payload.match(pattern);\nreturn { payload: parseInt(found[1].replace('[','').replace(']','')) };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1540,"wires":[["9fa61834a2d6604c"]]},{"id":"62d6bf0c4e40611f","type":"function","z":"42a5d63b.36e0c","name":"VARTA Netzlieferung","func":"/*\n\nPattern codes:\n--------------\nEGrid_AC_DC == Grid -> Home (Wh)\nEGrid_DC_AC == Home -> Grid (Wh)\nEWr_AC_DC == Inverter AC -> DC == Total Charged (Wh)\nEWr_DC_AC == Inverter DC -> AC == Total Discharged (Wh)\nChrg_LoadCycles == Charge Cycle Counter\n\nExample query:\n--------------\nEnergy Data: curl -X GET http://Varta-IP/cgi/energy.js\n\nExample answer of battery:\n---------------------------\nEGrid_AC_DC = 6135021;\nEGrid_DC_AC = 7717033;\nEWr_AC_DC = 2558221;\nEWr_DC_AC = 1878959;\nChrg_LoadCycles = [386];\n\n*/\n\nvar code = \"EGrid_DC_AC\";\nconst pattern = new RegExp(code + \" = (.*?);\");\nvar found = msg.payload.match(pattern);\nreturn { payload: parseInt(found[1].replace('[','').replace(']','')) };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1600,"wires":[["dbad85148a751892"]]},{"id":"5c9afd39e7dbdfdf","type":"function","z":"42a5d63b.36e0c","name":"VARTA Ladezähler","func":"/*\n\nPattern codes:\n--------------\nEGrid_AC_DC == Grid -> Home (Wh)\nEGrid_DC_AC == Home -> Grid (Wh)\nEWr_AC_DC == Inverter AC -> DC == Total Charged (Wh)\nEWr_DC_AC == Inverter DC -> AC == Total Discharged (Wh)\nChrg_LoadCycles == Charge Cycle Counter\n\nExample query:\n--------------\nEnergy Data: curl -X GET http://Varta-IP/cgi/energy.js\n\nExample answer of battery:\n---------------------------\nEGrid_AC_DC = 6135021;\nEGrid_DC_AC = 7717033;\nEWr_AC_DC = 2558221;\nEWr_DC_AC = 1878959;\nChrg_LoadCycles = [386];\n\n*/\n\nvar code = \"EWr_AC_DC\";\nconst pattern = new RegExp(code + \" = (.*?);\");\nvar found = msg.payload.match(pattern);\nreturn { payload: parseInt(found[1].replace('[','').replace(']','')) };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1660,"wires":[["1c0e43a464e67da9"]]},{"id":"31576e8ae1c19e14","type":"function","z":"42a5d63b.36e0c","name":"VARTA Entladezähler","func":"/*\n\nPattern codes:\n--------------\nEGrid_AC_DC == Grid -> Home (Wh)\nEGrid_DC_AC == Home -> Grid (Wh)\nEWr_AC_DC == Inverter AC -> DC == Total Charged (Wh)\nEWr_DC_AC == Inverter DC -> AC == Total Discharged (Wh)\nChrg_LoadCycles == Charge Cycle Counter\n\nExample query:\n--------------\nEnergy Data: curl -X GET http://Varta-IP/cgi/energy.js\n\nExample answer of battery:\n---------------------------\nEGrid_AC_DC = 6135021;\nEGrid_DC_AC = 7717033;\nEWr_AC_DC = 2558221;\nEWr_DC_AC = 1878959;\nChrg_LoadCycles = [386];\n\n*/\n\nvar code = \"EWr_DC_AC\";\nconst pattern = new RegExp(code + \" = (.*?);\");\nvar found = msg.payload.match(pattern);\nreturn { payload: parseInt(found[1].replace('[','').replace(']','')) };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1720,"wires":[["0c19a49d3c81d049"]]},{"id":"b177fee147ec29e4","type":"function","z":"42a5d63b.36e0c","name":"VARTA Ladezyklen","func":"/*\n\nPattern codes:\n--------------\nEGrid_AC_DC == Grid -> Home (Wh)\nEGrid_DC_AC == Home -> Grid (Wh)\nEWr_AC_DC == Inverter AC -> DC == Total Charged (Wh)\nEWr_DC_AC == Inverter DC -> AC == Total Discharged (Wh)\nChrg_LoadCycles == Charge Cycle Counter\n\nExample query:\n--------------\nEnergy Data: curl -X GET http://Varta-IP/cgi/energy.js\n\nExample answer of battery:\n---------------------------\nEGrid_AC_DC = 6135021;\nEGrid_DC_AC = 7717033;\nEWr_AC_DC = 2558221;\nEWr_DC_AC = 1878959;\nChrg_LoadCycles = [386];\n\n*/\n\nvar code = \"Chrg_LoadCycles\";\nconst pattern = new RegExp(code + \" = (.*?);\");\nvar found = msg.payload.match(pattern);\nreturn { payload: parseInt(found[1].replace('[','').replace(']','')) };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1780,"wires":[["0b6e6539e43e04a5"]]},{"id":"ddb0038ae62e2b81","type":"inject","z":"42a5d63b.36e0c","name":"Abfrage VARTA 30s","props":[],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":240,"y":1520,"wires":[["3e0232260f56ed4c"]]},{"id":"0b6e6539e43e04a5","type":"rbe","z":"42a5d63b.36e0c","name":"Größer Gleich","func":"deadbandEq","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":760,"y":1780,"wires":[["9e5f9a6ce0963d81"]]},{"id":"1c0e43a464e67da9","type":"rbe","z":"42a5d63b.36e0c","name":"Größer Gleich","func":"deadbandEq","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":760,"y":1660,"wires":[["2c994af343981e5f"]]},{"id":"0c19a49d3c81d049","type":"rbe","z":"42a5d63b.36e0c","name":"Größer Gleich","func":"deadbandEq","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":760,"y":1720,"wires":[["32c290a1bd5d52b3"]]},{"id":"dbad85148a751892","type":"rbe","z":"42a5d63b.36e0c","name":"Größer Gleich","func":"deadbandEq","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":760,"y":1600,"wires":[["16f7d3772f7d1f8f"]]},{"id":"9fa61834a2d6604c","type":"rbe","z":"42a5d63b.36e0c","name":"Größer Gleich","func":"deadbandEq","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":760,"y":1540,"wires":[["6f49af089abc484c"]]},{"id":"32c290a1bd5d52b3","type":"ha-entity","z":"42a5d63b.36e0c","name":"VARTA Entladezähler","server":"fbf6781b.5ad848","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"VARTA Entladezähler"},{"property":"device_class","value":"energy"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Wh"},{"property":"state_class","value":"total_increasing"},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":1720,"wires":[[]]},{"id":"2c994af343981e5f","type":"ha-entity","z":"42a5d63b.36e0c","name":"VARTA Ladezähler","server":"fbf6781b.5ad848","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"VARTA Ladezähler"},{"property":"device_class","value":"energy"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Wh"},{"property":"state_class","value":"total_increasing"},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1050,"y":1660,"wires":[[]]},{"id":"16f7d3772f7d1f8f","type":"ha-entity","z":"42a5d63b.36e0c","name":"VARTA Zähler Netzlieferung","server":"fbf6781b.5ad848","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"VARTA Zähler Netzlieferung"},{"property":"device_class","value":"energy"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Wh"},{"property":"state_class","value":"total_increasing"},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1080,"y":1600,"wires":[[]]},{"id":"9e5f9a6ce0963d81","type":"ha-entity","z":"42a5d63b.36e0c","name":"VARTA Ladezyklen","server":"fbf6781b.5ad848","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"VARTA Ladezyklen"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":"measurement"},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1050,"y":1780,"wires":[[]]},{"id":"6f49af089abc484c","type":"ha-entity","z":"42a5d63b.36e0c","name":"VARTA Zähler Netzbezug","server":"fbf6781b.5ad848","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"VARTA Zähler Netzbezug"},{"property":"device_class","value":"energy"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Wh"},{"property":"state_class","value":"total_increasing"},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1070,"y":1540,"wires":[[]]},{"id":"3e0232260f56ed4c","type":"http request","z":"42a5d63b.36e0c","name":"HTTP Abfrage","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://VARTA-IP/cgi/energy.js","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":280,"y":1580,"wires":[["4cbf71c03cf0e4c9","62d6bf0c4e40611f","5c9afd39e7dbdfdf","31576e8ae1c19e14","b177fee147ec29e4","fc0e24343bf93f19"]]},{"id":"fbf6781b.5ad848","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like

has someone a snippet to pull the data in sensors from the json?

I’m happy to share.

But as it’s not real json but more plain javascript syntax you can’t use the home assistant REST JSON parser (or at least I didn’t find out how), so I’ve built some jinja code and used templating to get the values. Works pretty well, but I’m sure the code could be improved :slight_smile:
Feedback is always welcome. Here we go:

Put everything in the configuration.yaml (you may need to merge with your own custom config)

First setup the “REST” call to pull the raw data and store it as string (don’t forget to replace VARTAIP with your actual IP)

sensor:
  - platform: rest
    name: varta_energy_totals_string
    scan_interval: 30
    resource: http://VARTAIP/cgi/energy.js
    value_template: "{{ value.replace(' ', '').replace('\n', '') }}"

Use templating to split this raw value up in four actual sensors:

template:
  - sensor:
      - name: "Varta Grid to Home Total"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
          {% set value = states('sensor.varta_energy_totals_string') | string %}
          {% for item in value.split(';') %}
            {% if item.startswith('EGrid_AC_DC') %}
              {{ item.split('=')[1] | int / 1000 }}
            {% endif %}
          {% endfor %}
      - name: "Varta Home to Grid Total"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
            {% set value = states('sensor.varta_energy_totals_string') | string %}
            {% for item in value.split(';') %}
            {% if item.startswith('EGrid_DC_AC') %}
                {{ item.split('=')[1] | int / 1000 }}
            {% endif %}
            {% endfor %}
      - name: "Varta Charged Total"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
          {% set value = states('sensor.varta_energy_totals_string') | string %}
          {% for item in value.split(';') %}
            {% if item.startswith('EWr_AC_DC') %}
              {{ item.split('=')[1] | int / 1000 }}
            {% endif %}
          {% endfor %}
      - name: "Varta Discharged Total"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
          {% set value = states('sensor.varta_energy_totals_string') | string %}
          {% for item in value.split(';') %}
            {% if item.startswith('EWr_DC_AC') %}
              {{ item.split('=')[1] | int / 1000 }}
            {% endif %}
          {% endfor %}

Sometimes HA complains about the “last_reset” not being set, in that case add:

        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'

to each template sensor. I’m not quite sure when you actually need this.

1 Like

Thank you very much!

aw man, my whole energy dashboard history is gone. what a mess!
Is it possible to just replace my old calculated battery value with the given one from the json?

If you change the name of the sensors all the history will be gone… That is true.

What you can try:
Name the new sensors exactly as you named the old ones. Then the history will probably stay intact.
However, as the values will probably differ you will have a completely messed-up day, because the values changed in “one hour” drasticly and HA thinks that you generated / lost a lot of energy in one day :wink:

I had a similar thing when I played around with the values of my heat pump.

I’ll probably try to “reset” my Energy Dashboard once I have everything up and running the way I like it.

Note: Be sure to make a backup before, just in case :wink:

Somehow my Chrg_LoadCycles changed to 24?? Any suggestions?

What does the official webinterface state?

Have you found a solution in the meantime?

Hi,

sorry I totaly forgot about this thread.
No, I did not find any solution. Sometimes my charge counter justs resets. Maybe because It is not connected to the internet, idk.
If, one day, I want to know the cycles, I will calculate them out of my influxdb with some other values.

Do you have the same problem?

You are not the only ones. I am also having the problem of differing charge and discharge amounts, and therefore I am watching this thread…

Thanks for your great work. Could you also add a code which integrates SOC in % ?
That would be great

Thank you very much

How do you bring this figure (Chrg_LoadCycles) into an sensor?

I think there is something missing…

sensor:
  - platform: command_line
    name: Varta
    command: "curl -X GET http://Varta-IP/cgi/energy.js"

Have a look at my post from above

It explains how to parse the energy.js into sensor values. It does not include the loadCycles yet, but it’s basically the same procedure to get that as a sensor into home assistant. If you can’t figure it out with that code block and you still need help, let me know and I’ll have a look when I have some time :slight_smile:

Sure, that is done via Modbus.

Hopefully the plugin will be available soon: https://github.com/home-assistant/core/pull/63736

But for now you can use something like this:

modbus:
  - name: varta
    type: tcp
    host: <YOUR_VARTA_IP>
    port: 502
    retry_on_empty: true
    sensors:
      - name: "Varta battery SOC"
        unit_of_measurement: "%"
        address: 1068
        data_type: uint16
        slave: 255
        scan_interval: 1
      - name: "Varta battery state"
        address: 1065
        data_type: uint16
        slave: 255
        scan_interval: 1
      - name: "Varta battery capacity"
        unit_of_measurement: "10 Wh"
        address: 1071
        data_type: uint16
        slave: 255
        scan_interval: 30
      - name: "Varta battery active power"
        unit_of_measurement: "W"
        address: 1066
        slave: 255
        data_type: int16
        scan_interval: 1
      - name: "Varta battery energy counter"
        unit_of_measurement: "Wh"
        address: 1069
        count: 2
        state_class: total_increasing
        data_type: uint32
        swap: word
        slave: 255
        scan_interval: 30
      - name: "Varta battery grid power"
        unit_of_measurement: "W"
        address: 1078
        slave: 255
        data_type: int16
        scan_interval: 1

I have then used those values, the values from the energy.js and the values from my SolarEdge inverter to calculate all the needed values for the energy tab and for one of the lovelace views, which now looks something like this:

1 Like

Unfortunately I don’t get anything. By the way, I’ve got a Varta Pulse 6


The IP needs to be given without http:// as it is using the modbus protocol and not http :slight_smile:

for example

modbus:
  - name: varta
    type: tcp
    host: 192.168.0.54
    port: 502
    retry_on_empty: true

Update:
Note, that for the energy.js you need http://, as that one is served by a webserver on http.