Rest binary sensor config

I’ve added several sensors using rest and they work fine but now I’m trying to add a simple leak detector binary sensor to the same rest section . While it works with this:

binary_sensor: >-
    - name: "Leak Sensor"
      value_template: {{ value_json.digitalInput1 }}

The binary sensor shows “Off” so I’m trying to get it to display “Dry” and “Wet”. When I try adding any logic for the binary sensor, rest fails after restarting HA even though it passes in DT.

binary_sensor: >-
    - name: "Leak Sensor"
      value_template: {{ value_json.digitalInput1 }}
          {% if is_state('leak_sensor_2', '0') %}
          {% else %}
          {% endif %} 
      icon: mdi:PipeLeak

The returned json file does show “0” for the state and I’ve tried changing the “is_state” to “digitalInput1” but doesn’t help.

Can anyone point out where I’m going wrong?

Thanks for any help.

One way to do this is to amend your template binary sensor to add a unique_id

Docs are on this page:

When your sensor has a unique_id Then you can edit the device from the UI to make it type: moisture which probably has the icons colors and states you’re looking for without a lot of work.

I’ve tried using unique_id’s but it always errors and fails to load anything in the rest section until I take it out. I’ve used unique_id’s with other entities like in modbus and it works fine but never had luck with rest.

In reading through all the posts I can’t really tell if unique id is supported in rest or not. It also says in the documentation that you can use any of the sensor configurations in rest like device class etc. but I’ve never been able to without errors.

Appreciate you help

1 Like

Eir yes you are using rest but didn’t include the top level label. Standby… Reading about rest unique_id

Try this solution instead

You still create your Inital sensor as you did but instead of the UI try customize

Thanks…I’ll read through it and give it a try.

1 Like

So it looks like that won’t work either as it comes up with the error that the entity doesn’t have a unique_id.

This implemenation of a restful binary sensor will allow both device_class (moisture) and unique id

It will allow device_class and unique_id

Notes on device_class es supported by binary sensors are here:

I was using this documentation on restful sensors…

So after lots more playing with different configuration I came up with what worked and what didn’t for me.

What I found was that using RESTful rest: configuration to create multiple sensors using a single resource endpoint is very limited in what can be configured for the sensors. It won’t accept unique_id’s, device_class or any of the sensor configurations without breaking rest so it doesn’t load. What was throwing me was that I was following the documentation in the RESTful section which says:

“sensor list (optional)
A list of sensors to create from the shared data. All configuration settings that are supported by RESTful Sensor not listed above can be used here.”

The only way I could get things to work was creating individual sensors using the RESTful sensors option and this allowed me to use all the configuration variables that I needed including unique_id’s:

  - platform: rest
    resource: http://IP_ADDRESS/ENDPOINT

It took a long trip down the rabbit hole :slight_smile: but it’s all working and no complaints…love HA!