Extrating json value from attribute with "#" and "@"

hi
need some help figuring out how to extract value from the following response from a http sensor:
http request:

http://192.168.1.30:8688/controller/rest/devices/Zooz%20dimmer%20zen22/status?name=zooz%20dimmer%20a%20switch%20status

Raw response:

<sensors><sensor name="zooz dimmer a switch status">off</sensor></sensors>

I have this setup as a rest sensor in HA

sensor:
  - platform: rest
    #resource: http://192.168.1.30:8688/controller/rest/devices/Zooz%20dimmer%20zen22/status?name=zooz%20dimmer%20a%20switch%20status&name=zooz%20dimmer%20a%20switch%20status
    resource: http://192.168.1.30:8688/controller/rest/devices/Zooz%20dimmer%20zen22/status?name=zooz%20dimmer%20a%20switch%20status
    name: Dinning Light Status 
    #json_attributes: '{{sensor.["#text"]}}'
    value_template: '{{sensor."#text" }}'

without the value_template, on the UI it is displaying status as

{"sensors": {"sensor": {"@name": "zooz dimmer a switch status", "#text": "off"}}}

trying to figue out how to extract just the status and display β€œoff” and not the entire raw response

value:template does not seem to like β€œ#”

error message is



    Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['sensors']['dinning_light_status']['value_template']. Got '{{sensor.["@name"] }}'. (See ?, line ?).
    Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['value_template']. Got '{{sensor.["#text"] }}'. (See ?, line ?).
    Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['value_template']. Got '{{sensor."#text" }}'. (See ?, line ?).

any ideas how to extract and display just the on / off value?

i understand the @ and # are being added as per:


https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html

Try: value_template: '{{ value_json.sensors.sensor["#text"] }}'

Thanks for the quick response. That works well!

1 Like

I have a follow up question regarding assigning custom value for display on UI. For my door lock, the value retured in β€œon” for door locked and β€œoff” for unlocked. currently on UI it displays on or off. How can i change this to display locked and unlocked. below is what i have tried so far (amongst many other things but this seems to get me closet but not quite:

 platform: rest
    name: Main Door Lock
    resource: http://192.168.1.30:8688/controller/rest/devices/doorlock/status?name=doorlock_status
    value_template: >
                    '{{value_json.sensors.sensor["#text"]}}'
                    '{% if is_state("sensor.main_door_lock", "on") %} locked {% else %} Unlocked {% endif %}'

output on the UI:

**Main Door Lock     'on' ' Unlocked ' `** 

if i create an additional template sensor, the template sensor seems to display the values locked and unlocked correctly corresponding to on / off for the rest sensor. I am wondering if there is a way to do this without creating an additional template sensor

 - platform: rest
    name: Main Door Lock
    resource: http://192.168.1.30:8688/controller/rest/devices/doorlock/status?name=doorlock_status
    value_template: '{{value_json.sensors.sensor["#text"]}}'
                    
    
  - platform: template
    sensors:
      
      door_lock:         
        value_template: '{% if is_state("sensor.main_door_lock", "on") %} locked {% else %} Unlocked {% endif %}'
        friendly_name: 'Main Door Lock'

Output on the UI for the 2 seperate sensors (Rest and template):


Main Door Lock    On
Main Door Lock    locked

if it is a binary sensor, just add

    device_class: 'door'

doesnt look like binary_sensor supports REST platform.

i tried but it gives an error

binary_sensor:   
  - platform: rest
    name: Main Door Lock
    resource: http://192.168.1.30:8688/controller/rest/devices/doorlock/status?name=doorlock_status
    value_template: '{{value_json.sensors.sensor["#text"]}}'

error:

 Logger: homeassistant.components.hassio
Source: components/hassio/__init__.py:269
Integration: Hass.io (documentation, issues)
Platform error binary_sensor.version - No module named 'homeassistant.components.version.binary_sensor'