Problem with creating restful binary_sensor - Template variable error: 'value_json' is undefined when rendering

Hi all,

I have a problem with a binary sensor which I can’t get to work.

My heatpump has some digital outputs where it reports the status of its pumps.
It is either 1 or 0.

If I set it up as rest sensor it works perfectly, but I don’t get the benefits of binary_sensor.

sensor
  - platform: rest                                                   
    name: Zusatzumwälzpumpe                                          
    resource_template: http://192.168.0.27/usr-cgi/xml.cgi?D|49|49   
    scan_interval: 60                                                
    value_template: "{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}"

This is the debug log for the rest data:

2022-09-21 11:02:26.683 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
2022-09-21 11:02:27.241 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: <PCOWEB>
<PCO>
<DIGITAL>
<VARIABLE>
<INDEX>49</INDEX>
<VALUE>0</VALUE>
</VARIABLE>
</DIGITAL>
</PCO>
</PCOWEB>
2022-09-21 11:02:27.242 DEBUG (MainThread) [homeassistant.components.rest.sensor] JSON converted from XML: {"PCOWEB":{"PCO":{"DIGITAL":{"VARIABLE":{"INDEX":"49","VALUE":"0"}}}}}

But when I set it up as restful binary_sensor, like described here:

binary_sensor:
  - platform: rest
    name: Zusatzumwälzpumpe 
    resource_template: http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
    scan_interval: 60
    value_template: "{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}"

I get the following error and I don’t understand why.

2022-09-21 11:12:12.143 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.rest
2022-09-21 11:12:12.183 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
2022-09-21 11:12:12.872 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}'

Why is it not working?
Has anyone an idea what I’m doing wrong?

What do you get with just:

"{{ value_json }}"

?

Or:

"{{ value_json['PCOWEB']['PCO']['DIGITAL']['VARIABLE']['VALUE'] }}"

with only “value_json” i get this message:

2022-09-21 13:07:49.779 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
2022-09-21 13:07:50.359 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'value_json' is undefined when rendering '{{ value_json }}'

with "value_json[‘PCOWEB’][‘PCO’][‘DIGITAL’][‘VARIABLE’][‘VALUE’] "

I get only this message:

2022-09-21 13:12:32.925 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49

but its not working.
The value is 1, so binary sensor should be ON, but its showing OFF

The error message is implying that the received data is not valid JSON that’s why value_json fails to work.

When you test the URL in a browser, what is the result?

The URL is working correctly
There is no difference between these two configs!

sensor
  - platform: rest                                                   
    name: Zusatzumwälzpumpe                                          
    resource_template: http://192.168.0.27/usr-cgi/xml.cgi?D|49|49   
    scan_interval: 60                                                
    value_template: "{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}"
binary_sensor:
  - platform: rest
    name: Zusatzumwälzpumpe 
    resource_template: http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
    scan_interval: 60
    value_template: "{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}"

Only difference is sensor and binary_sensor.

And sensor is working, binary_sensor is not working.
But I don’t understand why… :thinking:

Post the result of the URL (i.e. the information it produces).

The value produced by your template must be one of the values listed in the documentation for RESTful Binary Sensor

The JSON messages can contain different values like 1, “1”, TRUE, true, on, or open. If the value is nested then use a template.

It appears that it’s having difficulty interpreting the data it received. That’s why it would be helpful if you post the data so we can examine it.

Here are the results the URL:

<PCOWEB>
<PCO>
<DIGITAL>
<VARIABLE>
<INDEX>49</INDEX>
<VALUE>1</VALUE>
</VARIABLE>
</DIGITAL>
</PCO>
</PCOWEB>

As an experiment, try this:

    value_template: "{{ 'E>1<' in value }}"
1 Like

i think I found the problem.

i removed the line:

value_template: "{{ value_json.PCOWEB.PCO.DIGITAL.VARIABLE.VALUE }}"

and tried again with sensor and binary_sensor:

here is the logfile.
So it seems that with binary_sensor happens no XML to JSON conversion!

binary_sensor

2022-09-21 15:21:08.895 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.rest
2022-09-21 15:21:08.897 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49

sensor:

2022-09-21 15:21:57.906 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.rest
2022-09-21 15:21:57.908 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
2022-09-21 15:21:58.100 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: <PCOWEB>
<PCO>
<DIGITAL>
<VARIABLE>
<INDEX>49</INDEX>
<VALUE>1</VALUE>
</VARIABLE>
</DIGITAL>
</PCO>
</PCOWEB>
2022-09-21 15:21:58.101 DEBUG (MainThread) [homeassistant.components.rest.sensor] JSON converted from XML: {"PCOWEB":{"PCO":{"DIGITAL":{"VARIABLE":{"INDEX":"49","VALUE":"1"}}}}}

That’s why I said in my first reply:

Try what I suggested above. It simply searches for a matching string within the received value.

But why is it working with sensor and not with binary_sensor?
What is the difference?

This the response in log file:

2022-09-21 15:30:05.714 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.rest
2022-09-21 15:30:05.721 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49
2022-09-21 15:30:05.942 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'VALUE' is undefined when rendering '{{ 'E>1<' in VALUE }}'

Don’t change the template I suggested. You changed value to VALUE. It’s not the same thing.

I’m sorry, I thought that was a typo.

here is the code:

binary_sensor:
  - platform: rest                                                   
    name: Heizungspumpe                                              
    resource_template: http://192.168.0.27/usr-cgi/xml.cgi?D|49|49   
    scan_interval: 60                                                
    value_template: "{{ 'E>0<' in value }}"

and here the log response:

2022-09-22 09:06:29.318 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.rest
2022-09-22 09:06:29.324 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://192.168.0.27/usr-cgi/xml.cgi?D|49|49

and its the same as above, as soon as I use binary_sensor, the XML to JSON conversion doesn’t happen anymore.

The log doesn’t contain any warnings or errors.

The template I suggested doesn’t rely on XML to JSON conversion. It uses value, which is the raw, unconverted data that is received.

What is the state value of binary_sensor.heizungspumpe when you view it in Developer Tools > States?

it seems to be working. :slight_smile:

Could you please explain what 'E>0<' in value exactly does?

It looks at this line here:

<VALUE>1</VALUE>
     ^^^^

And looks for the string E>0< in it.
If it is

1 Like

understood, thank you :slight_smile:

I suggested you use 'E>1<' not 'E>0<'.

It’s looking for the string E>1< in the received data. If it finds it then it means it matched the E>1< in <VALUE>1</VALUE> and the template reports true. A Template Binary Sensor interprets true as on.

If you change it to E>0< it will cause the Template Binary Sensor to work opposite to the way you want (it will report on when it receives 0 and off when it receives 1).

1 Like

yes, was a typo on my side, thank you for the info

Glad to hear it’s working now.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.