RESTful and XML Integration

Hi,

I´m trying to use the RESTful integration to get out data from a boiler. The result is in XML format, but as I understood it should convert to JSON according to this page https://xmltodict-debugger.glitch.me/

My config is:

- platform: rest
  resource: http://192.168.xxx.xxx:8080/user/var/120/10251/0/0/12242
  name: eta_tanktopp_temp
  device_class: temperature
  json_attributes:
    - strValue

The XML result is as following:

<?xml version="1.0" encoding="utf-8"?>
<eta version="1.0" xmlns="http://www.eta.co.at/rest/v1">
  <value uri="/user/var/120/10251/0/0/12242" strValue="79" unit="°C" decPlaces="0" scaleFactor="10" advTextOffset="0">792</value>
</eta>

And when using the converter I get the following:

{
  "eta": {
    "@version": "1.0",
    "@xmlns": "http://www.eta.co.at/rest/v1",
    "value": {
      "#text": "792",
      "@advTextOffset": "0",
      "@decPlaces": "0",
      "@scaleFactor": "10",
      "@strValue": "79",
      "@unit": "°C",
      "@uri": "/user/var/120/10251/0/0/12242"
    }
  }
}

I would like the sensor to get the @strValue, what is it I do wrong and do not understand?

  • When using json_attributes the value would only be added as an attribute to the sensor, not as its state.
  • The JSON you posted does not have a top-level attribute “strValue”, but only an attribute “eta”. You could configure json_attributes_path to extract nested values, but this would still not set the sensor’s state.
  • What you want is to add value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'. This follows the path through the JSON structure to the value you want to extract.

ok, I tried it a bit but I only get Unkown as state, if I don´t do anything

- platform: rest
  resource: http://192.168.xxx.xxx:8080/user/var/120/10251/0/0/12242
  name: eta_tank_topptemp

I only get

<?xml version="1.0" encoding="utf-8"?> <eta version="1.0" xmlns="http://www.eta.co.at/rest/v1"> <value uri="/user/var/24/10301/0/0/12161" strValue="77" unit="°C" decPlaces="0" scaleFactor="10" advTextOffset="0">770</value> </eta>

as result when I look at the sensor, should´t the result be automatically converted to JSON?

It should. The integration does that automatically if the webserver providing the XML sends content type text/xml or application/xml as part of the response.
You could for example use Chrome and its developer tools and look for Content-Type in the response header.

Ok,
The header contains the following:

Status Code: 200 OK
cache-control: no-cache
content-length: 236
content-type: application/xml;charset=UTF-8
date: Tue, 17 Mar 2020 14:08:32 GMT

So it should work as I understand it, is there something else I´m missing?

Try

  json_attributes:
    - eta

(can’t remember if the value should be quoted or not).

[edit]
I should have said that this should load all the data in as an attribute. If that works it is possible to work out the data structure and extract the value(s) required.

That did´t work either I tried booth with or with out quot, in booth example I get the whole XML string as a result/value

That’s the one. That should trigger the conversion from XML to JSON.

One more thing you could try is to increase the log level to debug for this sensor which would give you either “JSON converted from XML…” or “Erroneous XML…”.

logger:
  logs:
    homeassistant.components.rest: debug

Do you get the XML or is it converted to a data attribute of the entity? I would expect for you to get it all. Then create a template sensor to extract it from the attribute.

Can you post a screen shot of the entity state?

The result is

2020-03-18 10:12:32 DEBUG (SyncWorker_14) [homeassistant.components.rest.sensor] Updating from http://192.168.xxx.xxx:8080/user/var/120/10251/0/0/12244

2020-03-18 10:12:32 WARNING (SyncWorker_14) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON

2020-03-18 10:12:32 DEBUG (SyncWorker_14) [homeassistant.components.rest.sensor] Erroneous JSON: <?xml version="1.0" encoding="utf-8"?>
<eta version="1.0" xmlns="http://www.eta.co.at/rest/v1">
  <value uri="/user/var/120/10251/0/0/12244" strValue="61" unit="°C" decPlaces="0" scaleFactor="10" advTextOffset="0">613</value>
</eta>

So I suppose it don´t covert to JSON, teh question is why then

What configuration are you using now?

Look on the states page in HA. Does this entity appear?

[edit]
Looking at the docs, I think you need a

json_attributes_path: $.eta.value.@strValue

to trigger the XML conversion - it may only pull XML data as attributes of an entity. This then needs a template sensor to pull it out of an attribute into a sensor state.

That path just pulls your value into an attribute of the REST sensor.

Ït seems to be the same result, I´m running 0.106.6

The entity appears and gets the XML string as state, so in some way it don´t convert the XML into JSON

What is your configuration?

I tried some different:

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
  name: eta_panna_temp
  json_attributes_path: $.eta.value.@strValue

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12204
  name: eta_panna_begardeffekt  


- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12242
  name: eta_tank_topptemp
  json_attributes:
    - "eta"  

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12244
  name: eta_tank_bottentemp
  json_attributes:
    - eta

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12207
  name: eta_tank_status
  json_attributes_path: $.eta.value.@strValue
  value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'

All with the same result except if I use the value templet then the state becomes Unknown

As I said before, I think the json_attributes_path tells the sensor to try XML conversion. You then need a json_attributes entry (I believe)

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
  name: eta_panna_temp
  json_attributes_path: "$.eta"
  value_template: 'OK'
  json_attributes:
    - "value"

Unfortunately it didn´t work.

I get the following error message

2020-03-18 15:46:28 DEBUG (SyncWorker_13) [homeassistant.components.rest.sensor] Updating from http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
2020-03-18 15:46:28 WARNING (SyncWorker_13) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2020-03-18 15:46:28 DEBUG (SyncWorker_13) [homeassistant.components.rest.sensor] Erroneous JSON: <?xml version="1.0" encoding="utf-8"?>

660

when i tried:

- platform: rest
  resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
  name: eta_panna_temp
  json_attributes_path: "$.eta.value"
  value_template: 'OK'
  json_attributes:
    - "@strValue"

The value for the sensor is then OK but i dont´t get any attributes to the sensor

I edited my suggestion as the OK should be single quoted.

I’ve played around with a similar XML source - it is fussy, but I cannot see why my suggestion won’t work although I wonder if the use of value is the problem.

Sorry I’m out of ideas. Raise an Issue on GitHub?

Alright, thanks for that.

  • “Updating from …”: That indicates that the update from that URL is about to happen
  • Then there is missing a DEBUG message “Data fetched from resource…”.
  • Then it checks for presence of HTTP response headers.
  • Next, because there is neither a message “JSON converted from XML…” nor “Erroneous XML…” I assume that either no HTTP response header has been found at all, or no “content-type” header was found, or the content type header does not start with text/xml or application/xml.
  • The next message “REST result could not be parsed as JSON” occurs because you still have json_attributes configured. But because the conversion from XML to JSON has not actually happened as seen above, this will then of course fail.

A quick test that I have just done is:

  • Create test.xml in my Apache webserver’s root folder and pasted the XML content you posted in your first post.
  • Use the following configuration:
sensor:
  - platform: rest
    resource: http://localhost/test.xml
    name: eta_tanktopp_temp
    device_class: temperature
    value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'
  • This works as expected. Sensor value is set to 79.

So, this all may indicate that your webserver (http://192.168.xxx.xxx:8080/...) does not quite work as expected, i.e. it appears as - despite you getting a correct content-type header in a web browser - the rest integration somehow does not.

Next step that I would take if this was my webserver is to configure the sensor in a developer instance of HA and add more and more DEBUG statements into the code (homeassistant/components/rest/sensor.py) to find out where the HTTP response is not as expected.

2 Likes

I was just reading through the 0.107 release notes and saw that application/xml has only been added to this release (Add application/xml as an XML to JSON auto converted mime types for rest sensors by bdraco · Pull Request #32289 · home-assistant/core · GitHub). That explains why it was working for me (I was running a pre-release of 0.107), but not for you.
Sorry for not checking this earlier. If you upgrade to 0.107, the solution I proposed using value_template: '{{ value_json["eta"]["value"]["@strValue"] }}' should work fine.

1 Like

Ok great, then I wait and try with the 0.107 version

Thanks!