2-way REST binary_sensor

I am trying to automate my garage door and have a Raspberry Pi monitoring the door status and am not sure if I am doing it the right way. From what I can tell so far in Home Assistant, the best way to do this would be for me to create a REST binary_sensor in Home Assistant. The problem I have with that configuration is the REST binary_sensor is a polling service that polls every minute and not real-time. I would like for the Pi to push the door status on change. I am thinking using the Home Assistant REST API is the best way to keep the status real-time, but seem to be having some issues getting the correct syntax.

Here is my configuration:

configuration.yaml

    binary_sensor:
      - platform: rest
        resource: http://192.168.1.6:5000/api/status/leftdoor
        method: POST
        name: Left Garage Door
        device_class: garage_door
        value_template: '{{ value_json.status }}'

I can call the endpoint using CURL and get the following:

    curl --request GET 192.168.1.6:5000/api/status/leftdoor
returns
    {"status":"Closed"}

I can call my home assistant API and get the following:

    curl --request GET 192.168.1.216:8123/api/states/binary_sensor.left_garage_door
returns
    {{
        "attributes": {
            "device_class": "garage_door",
            "friendly_name": "Left Garage Door"
        },
        "context": {
            "id": "a7a139b80bc14bee8e27ff24c5fe02a2",
            "parent_id": null,
            "user_id": null
        },
        "entity_id": "binary_sensor.left_garage_door",
        "last_changed": "2020-03-20T16:46:47.789213+00:00",
        "last_updated": "2020-03-20T16:46:47.789213+00:00",
        "state": "off"
    }

Here is where I have an issue. I am not sure what to put in the Home Assistant REST API to properly show a changed status

When I post:
curl --request POST 192.168.1.216:8123/api/states/binary_sensor.left_garage_door

What do I send in the body?

{"state": "Closed"}
or
{"state": "off"}

neither seems to reflect the same status as the true closed status of the garage door.

You don’t want to try and change the state of that binary sensor. All you’ll be doing is changing its representation in the Home Assistant State Machine. You won’t be changing the actual binary sensor. And the next time it updates it will just overwrite what you wrote to the State Machine.

What you want to do is to write the state of a made up binary_sensor. You can even use the same entity_id, just remove the RESTful binary sensor from your configuration. In effect, rather than HA polling your Pi, the Pi will push the status to HA.

EDIT: So basically what you’re doing, just not with the binary_sensor pre-configured in HA.

And to answer your question, the state of a binary_sensor is always on or off. You can also set the device_class attribute at the same time if you want it to look like a door.

Interesting, so basically what I need is to simply create a dynamic binary sensor with my POST command that sets the state. My previous HA solution had a similar idea where my PI would always push its status, but you had to build a virtual sensor first. This is interesting and I will play around with it. Thanks!

1 Like

If you don’t mind me suggesting a change of direction, this is the sort of thing MQTT is designed for.

I am open for anything. I have custom-written the PI code for my garage, but am worried MQTT would be a bit complex and overkill for this usecase. The PI only controls 2 garage door push buttons, 2 garage door sensors, and one regular door sensor. Everything else in my home automation is z-wave.

My real point I suppose is that mqtt is local push, once your pi detects the garage state change, it pushes to mqtt which ha picks up. No need to invent that wheel.

There is a python mqtt library, and even a mqtt garage integration. https://www.home-assistant.io/integrations/cover.mqtt/

Someone has undoubtedly written the whole thing for you already.

eg https://github.com/Jerrkawz/GarageQTPi