Simple RESTful binary_sensor

Hi everyone,

I am new to HA and migrating my stuff from openhab to HA.
The RESTful binary_sensor help on this forum is unfortunately above my head.
So I could need some help on this pretty simple example (I suppose).

I would like to create a RESTful binary sensor and would like to trigger this from my alarmsystem (Lupus XT1).
More precisely I would like to set the binary_sensor to ON from the Alarmsystem (Motion sensor) and use this in HA.

I have created the stuff in configuration.yaml

# RESTful binary sensor for input by Lupus (Motion detector)
binary_sensor:
  - platform: rest
    name: binary_sensor.bewegung_wozi
    device_class: motion
    resource: http://192.168.68.70/bewegung_wohnzimmer
    method: POST

I am unclear about what to do next.

  1. Do I need to create the sensor itself in the “Helpers” area?
    If so, how to hook it up to the RESTful binary_sensor?
  2. How to adress / set the binary_sensor to ON?

From my Alarmsystem I can trigger something like (from my memory on openhab):

http://admin:[email protected]:8080/httplistener/LupusAlarmListener?motion=ON

You help is greatly appreciated!

Look at template sensor for binary_sensor:
https://www.home-assistant.io/integrations/template/

It’s going to depend a great deal on the Lupus XT1. First, why are you doing POST method if you aren’t posting something? Second, you have to do something with the returned information and use it to create the value for the sensor. If it’s a JSON, you can add stuff from the response to the sensor attributes which you can use in the value template to get the right sensor status.

This is for a regular sensor of mine, not a binary one, but the principle should be the same:

  - platform: rest
    resource: http://172.16.1.2/discover.json
    name: HDHomerun 2 Info
    json_attributes:
      - FriendlyName
      - ModelNumber
      - FirmwareName
      - FirmwareVersion
      - DeviceID
      - DeviceAuth
      - UpgradeAvailable
      - BaseURL
      - LineupURL
      - TunerCount
      - ConditionalAccess
    value_template:  >-
      {% if value_json.UpgradeAvailable is defined %}
        Upgrade Available
      {% else %}
        OK
      {% endif %}

You’ll have to figure out the value template so that it always gives back on or off.

Follow up note. The restful binary sensor can’t have a json_attribute entry, so this example is of limited value in this thread. I’m leaving it just for context.

No, you have created the sensor in YAML. You only need to do a configuration check (Developer Tools → YAML) and restart Home Assistant if the check passes. However…

It is a sensor. You do not change the state of sensors. Sensors are inputs to Home Assistant that change state outside Home Assistant. Home Assistant just monitors this state with the sensor. There is no control of sensors.

Perhaps you wanted the restful switch instead?

A switch is an output you can use to control things. It has the ability to monitor the current state of the external switch with a template if something outside Home Assistant changes the switch state.

Thank you, Tom.

Unfortunately I don’t see the sensor in the entities section of the UI.

Yes, that’s my idea behind it:
I expose a sensor from HA to the outer world which is monitored by HA.
The Alarmsystem changes the state of the sensor with a http command and HA recognizes the change (motion detected).

Thank you, Glenn.
Will do and try my best :wink:

I just used what worked for me in the past from openhab.
I actually don’t care, but just want to get the “ON” signal to HA.

So how does the http call look like if I want to change the exposed binary_sensor to ON?

My config now looks like this:

# RESTful binary sensor for input by Lupus (Motion detector)
binary_sensor:
  - platform: rest
    name: binary_sensor.bewegung_wozi
    device_class: motion
    resource: http://192.168.68.70/bewegung_wohnzimmer
    method: POST

# Binary_sensor template for Lupus Motion detector
template:
  - binary_sensor:
      - name: "Bewegung - WoZi"
        delay_off:
          minutes: 5
        state: >
          {{ states('binary_sensor.bewegung_wozi') }}

The restful sensor in Home Assistant polls a resource (your alarm) to determine the state of the sensor.

If you want to push a state from the alarm system to Home Assistant you would have to use a different sensor.

I googled “Home Assistant Lupus XT1” and this popped up:

I assume you tried this already? If not consider trying this first. If so and it didn’t work for you then consider making a #feature-requests for what you found it is lacking.

Assuming you did try it and found it lacking and need to go a more custom route, from what you’re saying I don’t think you want a REST integration at all. RESTful sensors and switches use a polling model. They periodically call a URL to ask some device/service what state it’s in and (if its a switch) call another URL to turn it on or off.

You’re describing a push model. Your alarm system is able to send a command when it updates with that state. In that case you want a webhook. Using a webhook trigger anywhere in HA makes an endpoint your alarm system can post its state to.

You can use this type of trigger in an automation. Or if you want to make a sensor out of it, you can use a trigger-based template sensor. Your use case is basically identical to the linked example.

1 Like

Hi Mike,
yes, that’s already in place, but the Motion sensor is not supported yet.
But I will reach out to request this feature - thank you.

Thanks also for the webhook suggestion.
I will look into this as well.

Thanks, guys for the great support here :slight_smile:

EDIT:
@CentralCommand
That was the right hint.
I can trigger a switch using curl now:
curl -X POST https://My_URL/api/webhook/WebhookID

The Alarmsystem just accepts http action urls, so how can I transform this curl request into an action URL?

1 Like

Just a quick update.
I managed to connect the external Motion Sensor to HA.

I just created a new automation with a webhook trigger.
This created a new webhook (to be found under > Settings > Cloud at the bottom.

This webhook switches a binary_sensor helper which will be reset after a few seconds.

In the alarm system I just need to use the https:// request to address the exposed webhook.