Use XML result from rest API


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


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.


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:


  - platform: command_line
    name: VLC player
    command: '/usr/bin/curl --user {{ user }}:{{ password }} "" 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 }}>)[^<]+"'

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.


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 "" 2>&1 | grep -oPm1 "(?<=<val>)[^<]+"'
    scan_interval: 60

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

Use this tool to check the result of XML 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 :

the sensor to get the rate :
- platform: command_line
name: Dollar(USD)
command: "curl get 2>&1 | grep RATE | grep -o ‘[0-9].*[0-9]’ "
scan_interval: 43200 #12 Hours