RESTful API with JSON Parsing

Hi Folks,

I have an Audio Matrix switch which I can interrogate with a single GET request. Running the curl command gives me the following XML output.

<?xml version="1.0" encoding="UTF-8"?>

50,50,50,50,50,50,50,99,50,50,50,50,50,50,50,5075,95,75,75,75,75,75,75,75,75,75,75,75,75,95,950,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,095,75,95,95,95,95,95,95,95,95,95,95,95,95,75,7520,60,21,0,0,40,0,1,20,0,0,10,20,20,60,60

I have managed to use the RESTful sensor to get the data, store the β€œResult” portion in a state attribute field. But I can’t get to just the single values out of that state_attribute.

Here is the state_attribute:
Result:

  • β€˜@Name’: Balance
    β€˜#text’: β€˜50,50,50,50,50,50,50,99,50,50,50,50,50,50,50,50’
  • β€˜@Name’: Bass
    β€˜#text’: β€˜75,95,75,75,75,75,75,75,75,75,75,75,75,75,95,95’
  • β€˜@Name’: LipSync
    β€˜#text’: β€˜0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0’
  • β€˜@Name’: Mute
    β€˜#text’: >-
    false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false
  • β€˜@Name’: StereoConnections
    β€˜#text’: β€˜0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0’
  • β€˜@Name’: Treble
    β€˜#text’: β€˜95,75,95,95,95,95,95,95,95,95,95,95,95,95,75,75’
  • β€˜@Name’: Volume
    β€˜#text’: β€˜20,60,21,0,0,40,0,9,20,0,0,10,20,20,60,60’

Here is the value_template for one of the sensors, I can’t more out of this data, I would just want a single number, eg, 5 below.

{{states.sensor.audio_switch.attributes.Result[4]}}

yields:

{’@Name’: β€˜StereoConnections’, β€˜#text’: β€˜0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0’}

Alternatively, if someone can suggest a better way to extract all of these individual values cleanly, I am all ears.

Thanks in advance.

Welcome! Please format your post correctly: use the </> button to format code, otherwise spacing and quote style are screwed up.

code and XML etc should look like this with "straight quotes"
not like this with β€œsmart quotes”

Paste in the full XML from the matrix switch, and your current sensor configuration.

Then we have something to work with… :slight_smile:

It’s looking very similar to my answer here, though.

My apologies Troon. First time I have posted here and didn’t pick up on that formatting issue.

You are right, the answer is likely similar to that other post, except I would like to retrieve all of the β€œItems” for later use and hence need to store them as state attributes as it is greater than 255 chars, whereas they only wanted the last one.

Re-posting the XML and sensor configuration for you here. Hopefully this format is better.

<Query Status="Success" Reason="">
<Results>
<Result Name="Balance">50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50</Result>
<Result Name="Bass">75,95,75,75,75,75,75,75,75,75,75,75,75,75,95,95</Result>
<Result Name="LipSync">0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</Result>
<Result Name="Mute">false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false</Result>
<Result Name="StereoConnections">0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0</Result>
<Result Name="Treble">95,75,95,95,95,95,95,95,95,95,95,95,95,95,75,75</Result>
<Result Name="Volume">20,60,21,0,0,40,0,18,20,0,0,10,20,20,60,60</Result>
</Results>
</Query>

and here is the configuration of the sensor.

   platform: rest
   name: Audio Switch
   scan_interval: 1
   resource: http://10.100.100.41/DeviceQuery?Id=2
   value_template: "OK" 
   json_attributes_path: "$.Query.Results"
   json_attributes:
     - Result

Thanks Troon, your suggested other topic proved the key the [’#text’] piece . I managed to get this working as follows:

  platform: rest
  name: Audio Switch
  scan_interval: 1
  resource: URL
  value_template: "OK" 
  json_attributes_path: "$.Query.Results"
  json_attributes:
    - Result
  platform: template
  sensors:
    stereoconnections:
      friendly_name: "Audio Zone Stereo Connections"
      value_template: "{{state_attr('sensor.audio_switch', 'Result')[4]['#text']}}"
    audio_1:
      friendly_name: "Audio Output 1"
      value_template: "{{states('sensor.stereoconnections').split(',')[0]}}"
    audio_2:
      friendly_name: "Audio Output 2"
      value_template: "{{states('sensor.stereoconnections').split(',')[1]}}"

Thanks for the tip.

1 Like

Well done! That will work fine provided the XML is always in that order.