Force XML to JSON conversion

Hi,

I’m attempting to add a sensor that gets its value from a get request.

The returned information is in XML, which shouldn’t be an issue as according to the docs it should be converted to JSON. But, the content-type header is being set incorrectly, to “application/javascript” rather than xml.

How can I force the conversion to JSON? Or is there perhaps a different route I can take to get the sensor value?

For reference, this is the returned body:

<?xml version="1.0" encoding="utf-8"?>
<SpeakerPreset>1</SpeakerPreset>

with this being my config:

- platform: rest
  resource: https://10.0.10.146:10443/ajax/speakers/get_config?type=11
  verify_ssl: false
  name: Speaker Preset
  value_template: "{{ value_json.SpeakerPreset }}"

With this config, I get the error: Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.SpeakerPreset }}’
That doesn’t make the most sense to me.

Assuming the value is always digits, you can bodge it thus:

value_template: '{{ (value.replace("\n","")|regex_findall("<SpeakerPreset>(\d+)<"))[0] }}'

That works great, thank you!

I see you’ve already got a workaround, but have you tried setting an Accept header in your GET call with a value of application/xml (or, for that matter, application/json if the endpoint can be convinced to return JSON)?