Integrate values XML-endpoint into HomeAssistant

Hi everyone,

I got a smart meter which provides a XML-formatted endpoint to read all interesting values (used energy, produced by solar, energy returned to grid, …) and I am trying to make use of these values by integrating them in my HomeAssistant using the RESTful integration. Unfortunately this XML-format is not easily compatible with this json_path + json_attributes approach and I am not experienced enough with templating to figure out how to properly bind these values to sensor entities.

The XML looks something like this:

<values>
<value id="toGridValue">0.31 kW</value>
<value id="dateValue">2021-08-18</value>
<value id="timeValue">23:05:52 Uhr</value>
...
</values>

which will result in a JSON like this:

{
    "values": {
      "value": [
        {
          "#text": "0.37 kW",
          "@id": "toGridValue"
        },
        {
          "#text": "2021-08-18",
          "@id": "dateValue"
        },
        {
          "#text": "21:42:25 Uhr",
          "@id": "timeValue"
        },
        ...
      ]
   }
}

I could manually select a single metric using a json path like this $.values.value[?(@['@id'] == 'toGridValue')]['#text'] but this would mean I would have to fetch/query this endpoint for each and every metric I want to use instead of querying it just once and make use of all values. I just have no clue how to do this since the examples I found all have a resource that is structured in a way that can be used easier (e.g.: https://michilehr.de/how-to-integrate-an-external-api-into-home-assistant-an-example-with-the-ambee-pollen-api)

Could someone help me out and/or point me in the right direction?
Thanks in advance!

Sometimes, just checking the doc does miracles :slight_smile:
See there for how to define multiple sensor from a single payload:

and if I would have a JSON like:

{
    "values": {
        "toGridValue": "0.37 kW",
        "dateValue": "2021-08-18",
        "timeValue": "21:42:25 Uhr",
        ...
   }
}

I could use it like the docs do by using the json_attributes field.
But I do have a weird input format where I cant (or at least I didnt figure out how to) use the json_attributes field to select all metrics I would like to have. Since I have an array of these json objects:

{
          "#text": "0.37 kW",
          "@id": "toGridValue"
}

where the metric name is in a JSON attribute and the metric value is in another attribute on the same tree level.
So it’s not that I didnt read the docs - there is just no example that uses similar input to mine. Or is there something that I overlooked in the docs on how to handle such data? If so could you please point me to it.
Thanks.

Hi, I have the same issue with parsing the smartfox xml file. Have you found a solution and would you please share how you managed to solve it?

Thank you!

The XML tags are not stable through releases, the ModBus registers are.
They publish the registers and their meaning on website Allgemein ( smartfox dot at slash allgemein dot html, (the XLS sheet says DE, yet content is EN)
good luck

Addendum : SMartFox Integration works with below modbus.yaml

# Modbus for SmartMart  18 Dec 21
 - name: "smartmart"
   close_comm_on_error: true
   delay: 5
   timeout: 5
   type: tcp
   host: 192.168.0.16
   port: 502
   sensors:
    - {name: solar.boiler       ,  unit_of_measurement: pct ,  data_type: int   ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41046  ,  scan_interval: 20}
    - {name: solar.power_pv1    ,  unit_of_measurement: W   ,  data_type: uint32,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41399  ,  scan_interval: 20}
    - {name: solar.power_net    ,  unit_of_measurement: W   ,  data_type: int32 ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41017  ,  scan_interval: 20}
    - {name: solar.power_l1     ,  unit_of_measurement: W   ,  data_type: int32 ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41019  ,  scan_interval: 20}
    - {name: solar.power_l2     ,  unit_of_measurement: W   ,  data_type: int32 ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41021  ,  scan_interval: 20}
    - {name: solar.power_l3     ,  unit_of_measurement: W   ,  data_type: int32 ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 41023  ,  scan_interval: 20}
    - {name: solar.relay_1      ,                              data_type: int   ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 42249  ,  scan_interval: 20}
    - {name: solar.relay_2      ,                              data_type: int   ,  input_type: holding  ,  swap: none  ,  slave: 1 ,  address: 42279  ,  scan_interval: 20}

Thanks for the config!

Energy from and to grid can be red with

    - {
        name: energy_from_grid,
        unit_of_measurement: Wh,
        data_type: uint64,
        input_type: holding,
        swap: none,
        slave: 1,
        address: 40999,
        scan_interval: 20,
      }
    - {
        name: energy_to_grid,
        unit_of_measurement: Wh,
        data_type: uint64,
        input_type: holding,
        swap: none,
        slave: 1,
        address: 41003,
        scan_interval: 20,
      }
    - {
        name: energy_pv_yield,
        unit_of_measurement: Wh,
        data_type: uint64,
        input_type: holding,
        swap: none,
        slave: 1,
        address: 41401,
        scan_interval: 20,
      }

The Adresses in registers in documenation (https://www.smartfox.at/assets/smartfox-pro-modbus-v22e-em2-00.01.03.10.xlsx) are 1 based, not 0 based

All of that looks very promising, yet i don’t know where to start. Could someone point me in the right direction? Thanks heaps

Thank you for the code and address! Amazing how much this post helped me.

@W6Es3QEa How did you get the addresses? I’m trying to get the ones for the daily Wh but they are not mentioned in the Modbus docu of smartfox.

Thank you in advance for your resopnse

Probalby a little late, but if you check the Config sheet in the excel file (latest at the moment: https://smartfox.at/wp-content/uploads/2022/12/Modbus-Register-SMARTFOX-Pro-SMARTFOX-Pro-2-v22e-00.01.03.10.xlsx) you see that there is an offset of -1.
So if you go back to the rgister, you take the Start value and substract 1. E.g. Power total has the start value 4018, so the adress in the config yaml is 4017.

I team… sorry to bother… but i can solve the problem…

I do have a smartfox installation (charger and pro) - now, i do want to read the values from the xml http://192.168.XXX.127/values.xml and make a specific value available in home assistant (hidCcMode1) - but i can not solve the problem with rest - played around but nothing worked… the value is always unknown

Hello everyone,
unfortunately I have no idea about this topic and I’m just setting up my home assistant. Therefore, please excuse my very simple questions:
where do I have to enter the Modbus or XML query in homeassistant and how can I then visualize the values?
Greetings Frank

Paste the XML and explain which value you want.

For what? What device are you working with, what data does it produce and what do you want HA to do?

Thanks for the feedback. I also have a Smartfox installation. First, I would like to integrate the mode data in the HA energy dashboard. I currently don’t have any display of solar production, the charging station, my own consumption, etc. Later I would like to use the surplus to control the WiFi socket of the washing machine, etc.
Greetings Frank

I think I found the problem. The Modbus configuration above is no longer completely possible in the 2024 version of the HA. the entry “swap” had to be removed.