Integrating a motion detector with Home Assistant or Hassio

If we want to place motion sensors and perform actions based on detecting movements, we will be able to use some cheap Chinese IR sensors that are powered by a couple of batteries, they are small and disguised… In this document we will see how to add them in Home Assistant and how to configure some actions, For example, If we're not at home, Well, send us a Telegram message notifying us of a movement, or at a certain time turn on some LEDs to illuminate.

 

Thanks to the Sonoff Bridge RF we have, we can connect any sensor that communicates via radio frequency to our Hassio, No need for anything else. As we saw in a previous post, we were able to change the firmware to the Sonoff Bridge and thus make it communicate with Home Assistant via MQTT, In that post we saw how to add a door or window sensor. This post will help us to continue adding devices, a motion sensor that we can visualize and configure actions in Hassio.

 

Add the Motion Detector to Hassio

 

Turn on our motion sensor, we open our Sonoff Bridge RF in a browser, in our case using Tasmota, in the Console we will be able to see everything that happens, That's where we will see the code that our motion detector sends every time it does its job, that is, detect any movement. We copy the value of “Date”. And we must add in some rule that we have the following, which will be nothing more than the Data you will receive and the topic you will publish:

[SourceCode]on rfreceived#data=85DB26 do publish2 RFBridge/sensor_pir_hall Movimiento endon[/SourceCode]

 

Now, We register our motion sensor, within the Hassio configuration, We add in the section of the “binary_sensor” This new sensor. We must give it a name, A topic, the payload it will send when there is or is not movement and the type of device, Leaving something like this:

[SourceCode]binary_sensor:
– Platform: MQTT
Name: "Hall Sensor"
state_topic: "RFBridge/sensor_pir_hall"
availability_topic: "tele/Sonoff-Bridge-01/LWT"
Qos: 1
payload_on: "Movement"
payload_off: "No movement"
payload_available: "Online"
payload_not_available: "Offline"
device_class: motion[/SourceCode]

 

 

If we save the configuration file and restart Home Assistant as usual so that it reloads the config, we will see our sensor and we can add it to the Summary view.

 

Automations

We'll use automations to make sense of the motion sensor, A first example that of light if it detects a movement, then we will make another one to turn off the light if after 30 seconds there was no movement. And last and perhaps the most important, an automation that if after 5 seconds no more motion detected, then send Home Assistant this information and say that there is no one. Since these sensors communicate when there is a movement, Nothing else, it does not inform when something 🙂 has finished moving

 

We start with the first automation, Forced, since we will have to tell Home Assistant that if it does not detect movement after 5 seconds will understand that there is no more movement and therefore no one out there swarming. So in the Trigger we select our motion sensor, Indicate 5 seconds with the state “on” and that executes the Action that interests us, in this case it will be that he sends his new status by MQTT, that there is no movement. I leave you the code of this automation that you can also manually add in automations.yaml:

[SourceCode]- Id: ‘1563905146870’
alias: Hall Reset PIR Sensor
Trigger:
– entity_id: binary_sensor.sensor_hall
Platform: state
To: 'on’
for:
seconds: 5
Condition: []
action:
– service: mqtt.publish
date:
topic: RFBridge/sensor_pir_hall
Payload: No movement
[/SourceCode]

 

I leave you with an example of automation that we can use when it detects a movement from 8 pm to 6 am to turn on a strip of LEDs, As we always give it a name, in the Trigger we will select our Motion Sensor and indicate that when it goes from off to on, complying with the Hourly Condition (For example), and in Actions I will indicate what interests us, in this case I run a shell script that lights up an LED strip controlled by a remote Raspberry Pi. I also leave you the code that this example generates for me in “automations.yaml”:

 

[SourceCode]- Id: ‘1563924390485’
alias: Hall – Movement – Giving the Light
Trigger:
– entity_id: binary_sensor.sensor_hall
from: 'off’
Platform: state
To: 'on’
Condition:
– after: ’20:00:00′
before: 06:00:00
Condition: Time
action:
– service: shell_command.ledsa100[/SourceCode]

 

What I said, We will need another automation to turn off the light, We won't leave her forever, No? In my particular case, If after 30 seconds there is no movement.

[SourceCode]- Id: ‘1563999476366’
alias: Hall – NO Movement – Turn off the light
Trigger:
– entity_id: binary_sensor.sensor_hall
for:
seconds: 30
Platform: state
To: 'off’
Condition:
– after: ’20:00:00′
before: 06:00:00
Condition: Time
action:
– service: shell_command.ledsa10[/SourceCode]

 

And well, although we already saw it in another post on how to integrate Telegram with Home Assistant, But you might be interested to see what this automation would look like, we will use it to send us a Telegram message when it detects a movement in the Hall when in principle there are no people at home. I leave you the code that generates this automation in case you need it:

[SourceCode]- Id: ‘1563907587744’
alias: Telegram Notification – Sensor Pir Hall
Trigger:
– entity_id: binary_sensor.sensor_hall
from: 'off’
Platform: state
To: 'on’
Condition:
– Condition: state
entity_id: device_tracker.hector
state: not_home
– Condition: state
entity_id: device_tracker.seila
state: not_home
action:
– date:
Message: There is movement in the Hall
service: notify.telegram[/SourceCode]

 

Well! I hope you found it interesting, We already have another clunker more fully controlled and we will make our home a safer place, we already have the openings of the windows and doors under control, In this post we will know any movement!

 

Recommended Posts

Author

nheobug@bujarra.com
Autor del blog Bujarra.com Cualquier necesidad que tengas, Do not hesitate to contact me, I will try to help you whenever I can, Sharing is living ;) . Enjoy documents!!!