Restful binary sensor not working, but restful sensor works

Hi,

I have trouble with the configuration of a restful binary sensor.

I have created this restful sensor that work fine:

sensor:
  - platform: rest
    name: luce_studio_stato
    resource: "http://<valid address that returns an xml>"
    value_template: "{{ value_json['root']['parametri']['parametro1'] }}"

the sensor above yields either 0 or 1, so I would like to use a binary sensor instead. I tried the following:

binary_sensor:
  - platform: rest
    name: luce_studio_stato
    resource: "http://<valid address that returns an xml>"
    value_template: "{{ value_json['root']['parametri']['parametro1'] }}"

but this does not work, the binary sensor is always OFF and never changing.
According to the documentation it should not matter where the value is either 1 or "1", but nonethelesse I tried to evaluate "{{ value_json['root']['parametri']['parametro1'] == 1}}" and "{{ value_json['root']['parametri']['parametro1'] == '1'}}"

do you have any idea what is not working, or how to understand what value_template is evaluating?

1 Like

Resource returns XML and you parse it as JSON? Have you ever checked the actual return value?

Try converting the returned value into a float before comparisson:

"{{ value_json['root']['parametri']['parametro1'] | float == 1 }}"

according to REST documentation, xml results from resource are automatically parsed as json, indeed my sensor example works as intended and the value_template returns either "1" or "0" as it should.

It’s the binary_sensor that doesn’t work and I don’t know what is evaluating since it returns always off

1 Like