Use XML result from rest API

Hi,

I have a service that exposes a RESTful API and returns an XML result that looks something like this:

<result>
<type>boolean</type>
<value>true</value>
</result>

My question is how would I go about grabbing the <value> from that result?

I get how to use a JSON result but not straight XML.

3 Likes

I have the same question. Was this ever answered for you?

No, I ended up writing a python script and using a command line sensor to grab the result.

1 Like

That’s the same approach I took. JIC it helps anyone, here’s how I’m recording a remote VLC player’s state:

sensor:

  - platform: command_line
    name: VLC player
    command: '/usr/bin/curl --user {{ user }}:{{ password }} "http://192.168.0.109:8080/requests/status.xml" 2>&1 | grep -oPm1 "(?<=<state>)[^<]+"'
    scan_interval: 1

Could you elaborate a little bit on how you constructed this command? I don’t really understand the syntax.

Also, is this really the only way for HA to extract and XML value from a REST api that only returns XML?

Yes, I think so. I’ve since written a python script to interface with VLC remotely. But since this is a more general purpose tool, I’ll walk you through it.

Syntax is complex but it works a charm.

'/usr/bin/curl --user {{ user }}:{{ password }} "{{ XML URL }}" 2>&1 | grep -oPm1 "(?<=<{{ Opening bracket }}>)[^<]+"'

Where:
User / password: the HTTP authentication
XML URL: location of the XML file.
Opening bracket: tag title of the opening bracket e.g. <body>

First part of the command, curl, pulls the XML response and pipes the response into the second part… Grep. Grep searches for the contents of the XML tag you’re after.

3 Likes

Working perfectly, thank you!

For anyone else looking to do a similar thing, my application was extracting a variable value from my ISY-994i. I couldn’t think of any other way for HA to read the temperature from my Insteon thermostat, so I wrote an ISY program to assign an integer value to a variable based on the temperature. The command above then allows me to extract that integer variable via the ISY REST api.

I’m now realizing that I could bypass the whole variable thing altogether if I could modify the code to extract directly from the Insteon thermostat node. Any idea how I could grab the highlighted value below? Preferably without the two decimal places.

Thank you for this! I have been struggling and struggling to get all my components tied in and this awesome, simple little trick really did the trick.

I wanted to share my exact configuration in case it helped anyone else. I’ve got a couple Raspberry Pis running python scripts to gather data points and store them in my ISY variables that are then used for managing some programs. (Ex: I’ve got a sensor collecting pool temp and then programs running on the ISY to determine if I need to run the heat pump longer.)

The specific IP/port is my ISY and of course the rest url specific to my variable type and ID.

sensor 2:
  - platform: command_line
    name: PoolTemp
    command: 'curl -X get --user myusername:mypassword "http://192.168.1.10:13333/rest/vars/get/2/5" 2>&1 | grep -oPm1 "(?<=<val>)[^<]+"'
    scan_interval: 60
3 Likes

Thanks so much for posting this, you solved a problem I thought was almost impossible!

Use this tool to check the result of XML https://jsonformatter.org/xml-formatter and go to load url.

@James_Malvi It appears that this just converts XML to JSON. I’m not sure how this would help people with extracting data from an XML directly to a Home Assistant sensor. Can you elaborate?

FYI for anyone looking for Insteon thermostat support, I did end up figuring it out thanks to this thread. See my thread here.

hello , its may help someone.

i was try first this code , but i found it dont working good .

this is my sonsor:
the xml data is : http://www.boi.org.il/currency.xml

the sensor to get the rate :
- platform: command_line
name: Dollar(USD)
command: "curl get https://www.boi.org.il/currency.xml?curr=01 2>&1 | grep RATE | grep -o ‘[0-9].*[0-9]’ "
scan_interval: 43200 #12 Hours