Problem parsing RESTful XML from Mbus (meter-bus)

Hello everyone,

I am rather new to Home Assistant, and have a problem configuring one sensor (all others work fine). I have a RESTful server running on a raspi, which returns XML data for an MBus device. I can see that Home Assistant can retrieve the XML by setting

  # Grundwasserzaehler
  - platform: rest
    resource: http://raspberrypi.home:8080/mbus/get/ttyAMA0/2400/0
    method: POST
    name: "Grundwasserzaehler"
    value_template: "{{ value|truncate(50) }}"
    scan_interval: 600

Then the sensor contains the first 50 characters of the XML response and they look correct. If instead I try to extract something from the parsed json with

  # Grundwasserzaehler
  - platform: rest
    resource: same as above
    method: POST
    name: "Grundwasserzaehler"
    value_template: "{{ value_json.MBusData.SlaveInformation.Id }}"
    scan_interval: 600

I get an “unkown” value and the following log entry:
“Template variable error: ‘value_json’ is undefined when rendering '{{
value_json.MBusData.SlaveInformation.Id }}”.
If, instead, I try to access attributes using

  # Grundwasserzaehler
  - platform: rest
    resource: same as above
    method: POST
    name: "Grundwasserzaehler"
    json_attributes_path: $.MBusData.SlaveInformation
    value_template: "OK"
    json_attributes:
      - Id
      - Version
    scan_interval: 600

I get a log entry “REST result could not be parsed as JSON”
In a python script the XML gets parsed completely fine, see below. If I try the recommended online tool at xmltodict-debugger.glitch.me it looks a bit different. I’d be happy to post the XML and JSON samples but the community forum won’t let new users post more than two links. I think it is confused with the XML, even when using code backticks.

I am really at a loss. Short of writing my own RESTful server that delivers JSON instead of XML I really don’t know what to do. So any help would be appreciated. I am not sure what Home Assistant does differently. Sorry for the very long post, any response is really appreciated.

Best,

Gregor

The first thing you want to check is if your server is actually producing a response that can be converted to JSON. Please note the following hint in the doco:

If the endpoint returns XML with the “text/xml”, “application/xml” or “application/xhtml+xml” content type, it will automatically be converted to JSON

Check with your web browser’s developer tools what the Content-type response header contains. If it’s not one of the above then the result won’t be available as value_json.

Thank you so much, you were spot on! Here are headers and the first line of the “body”. Clearly, with text/plain this is not getting parsed. Unfortunately this doesn’t solve my problem for now, but perhaps the RESTful server response can be changed. It is written in rust, though, not my fortĂ©.

Best, Gregor

x-span-id: 646232ae-653b-46c2-a84e-234f0171f4af
content-type: text/plain
content-length: 2372
date: Tue, 04 May 2021 20:44:53 GMT
 
<?xml version="1.0" encoding="ISO-8859-1"?>
1 Like