Setting up motion sensors as binary sensors in Home Assistant

This post explains how to set up binary_sensors in Home Assistant that track the state of RF motion sensors. Our RF gateway converts radio messages to MQTT messages with the RF code in its payload and sent on its own topic. (e.g. /rf/).We can set up <code>binary_sensors in our Home Assistant configuration that track the state of RF devices and get updated according to RF codes sent on the relevant MQTT topic.

We can use the binary_sensor.mqtt component for this task as it supports a few properties and behaviours that come in handy.


Advertisement Begins

Advertisement End


Types of RF Devices

First of all, we need to distinguish between two different kinds of RF devices: * those that send RF signals for both the “on” and “off” state (“garage door open” and “garage door close”) * those that only send “trigger” signals, that are not followed by an “off” signal (such as “motion detected”)

The following sections explain how to configure them in Home Assistant.

On/Off Type RF devices

The first kind we can handle as shown in the door_sensor example below.

  1. The RF device sends a signal indicating the door has been opened.
  2. Our RF gateway converts this to a MQTT message containing the 10179082 payload and a message send on /rf/10179082.
  3. This is picked up by the binary_sensor component in Home Assistant and;
  4. Our sensors state is changed to "on".

Similarly, when the door is shut, the device emits a radio signal that is converted to a MQTT message containing the payload_off code. Again, this results in the binary_sensor component updating the state of the sensor inside Home Assistants state machine. This is easy enough becuase the RF device emits signals for both its on and off state, i.e. it is completely stateful. The following YAML shows how to ste up this type of RF device as a binary_sensor.

- platform: mqtt
  name: "door_sensor"
  device_class: 'door'
  state_topic: "/rf/all"
  payload_on: '10179082'
  payload_off: '10179086'

Affiliate Content Start

Affiliate Content End


Trigger type RF devices

The second kind of RF device does not send off signals because it is either not supported or it doesn’t make sense to send such a signal.

This poses a problem because our binary_sensor gets stuck in the on position indefinitely. Any subsequent signals sent out are therefore not registered in Home Assistant because they do not trigger a state change in the binary sensor component (on -> on does not represent a change in state).

- platform: mqtt
  name: "living_room_motion"
  device_class: 'motion'
  state_topic: "/rf/10179082"
  off_delay: 5 # make sure you update HA to v0.81 for this feature

Note that the binary_sensor.mqtt component does not support the expire_after property as used in other types of sensors. This property would come in very handy and I hope they add this functionality soon. (They have!!!)

How to automatically reset binary_sensor state back to off?

Kitchen Multi-Timer Pro

Now you’re cooking

Multi Timer Pro is your ultimate meal prep companion, keeping track of multiple cooking times and making adjustments on the fly. Give it a try today and become a better home cook!


Since v0.81, Home Assistant does support the off_delay parameter, which we can use here to turn off the MQTT binary sensor automatically after some delay. This means that the rest of this post is obsolete as the functionality has been added into Home Assistant.

We can turn off the binary sensor by creating an automation. This will fake a sensor update by sending an MQTT message on the sensor topic. Home Assistant will interpret this as a message originating from the MQTT sensor.

To achieve this we can create an automation that resets the binary_sensor to off after some time (say 5 seconds). Consider the living_room_motion sensor above. It’s payload_off RF code is set to 0. This is not a real RF code. It’s a fake code we are going to use to reset its state back to off using the following automation.

- alias: Motion Sensor Reset Off
  trigger:
    platform: state
    entity_id: binary_sensor.living_room_motion
    to: 'on'
    for:
      seconds: 5 # Trigger once the sensor has been in the to state for 5sec
  action:
    - service: mqtt.publish ## Why MQTT? See notice below
      data:
        topic: '/rf/all'
        payload: '0'

I will use the '0' payload in all trigger-type RF devices. However, this means that different RF devices can interfere with each other when the intent was to reset a particular device. Instead all devices with payload_off: '0' are switched off. The reason I did this is to avoid defining unique fake codes for each RF device. It’s not perfect. Keep in mind that this is only really a problem if your reset window is much longer than 5 seconds. Any device that is reset prematurely (because of another device triggering the automation) would have gotten reset a few seconds later anyway by its own reset trigger.

Conclusion

We have successfully configured Home Assistant to maintain the state of our motion sensor and door sensor. This means that Home Assistant now contains an accurate representation of the true states of these RF devices. The next step is to create motion based automation

MY MISSION

This blog started nearly 10 years ago to help me document my technical adventures in home automation and various side projects. Since then, my audience has grown significantly thanks to readers like you.

While blog content can be incredibly valuable to visitors, it’s difficult for bloggers to capture any of that value – and we still have to work for a living too. There are many ways to support my efforts should you choose to do so:

Consider joining my newsletter or shouting a coffee to help with research, drafting, crafting and publishing of new content or the costs of web hosting.

It would mean the world if gave my Android App a go or left a 5-star review on Google Play. You may also participate in feature voting to shape the apps future.

Alternatively, leave the gift of feedback, visit my Etsy Store or share a post you liked with someone who may be interested. All helps spread the word.

BTC network: 32jWFfkMQQ6o4dJMpiWVdZzSwjRsSUMCk6

Related posts

Cheap Wifi MQTT Controlled LED strip using Tasmota and Home Assistant

Integrating Mi/QingPing BLE sensors into Home Assistant without a Gateway

How to set up Tasmota to control LED strips individually on a single H801 controller

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More