Read RF with Raspberry PI GPIO Binary Sensor

My device is cheap motion sensor - there is listed as [87] Generic wireless motion sensor. So I don´t know how to check if it will be OK.

Using python sniffer I can see that the motion sensor broadcast this:
[INFO] rfrx: 7621980 [pulselength 482, protocol 1]

I have no problem to buy some RTL2832 tuner.

You can try looking in the source code for the protocol - e.g. https://github.com/merbanan/rtl_433/blob/master/src/devices/generic_motion.c

But best way is to get a tuner and run rtl_433 with no protocol specified or with the test mode on.

I knew in advance that my currentcost sensor would work… but my front door bell does not (although the event is captured it cannot be decoded)

@keithh666 or other Please could you recommend how to read from GPIO using rpi_gpio?

RXB6 433MHz receiver is connected to raspberry´s GPIO. RF sniffing python script is able to read codes from rf devices. How to get it into home assistant using rpi_gpio?

You read from the gpio using the folowing …

binary_sensor:
  - platform: rpi_gpio
    ports:
      11: PIR Office
      12: PIR Bedroom

However as I assume you have figured out your pir is not directly connected it’s using RF433 so you need a program to decode the RF and send a appropriate message to HA, this is usually done with either some other HW such as RF Link, Telldus, RFXTX or some RF receiver and transmitter directly connect to the PI with either pilight or some other SW decoding the RF to send a message to HA. You could use some other software to decode the RF and use it to toggle the GPIO pins directly but that would not be efficient and you only have so many GPIO’s. If you have the Rx’er and Tx’er attached to the PI then pilight is the best option that I know. If you have some other software sniffing the code from your Rx’er then just modify it to send a MQTT message to HA.

Hope that helps.

Thanks Keith. I´m right now on the way to try both MQTT and Pilight. I would like to have all on single board (raspberry) so we will see what is better solution. I will need MQTT for other switches so I hope this will be better way for me but seems like there is no direct solution for RF2MQTT on Pi. All I checked till now was based on arduino :frowning:

Did you have a look at this …

Yes and thank you. But this is about another piece of HW. I try to find solution based on raspberry. This shouldn´t t be so hard to do.

if you publish your Python script you use to get the values from RF, I should be able to add the few lines necessary to get it to send an mqtt message.

1 Like

Oh it would be very nice from you. I use this script. And please, if there is any difference,I would like to use Mosquitto. Thank you very much.

I cloned that repository and created a script here

If you have a username and password defined in mosquitto, you can change that in line 16. If you don’t (like me), it seems to work just fine as it is.

At the moment, it should just send the code received. If you need the pulselength and proto as well, I can fix that.

Obviously, I can’t test it completely, because I don’t have a receiver, but if it works, let me know and I’ll send a pull request back to the author so he can add it to his scripts.

Edit:
Also, you need to add the mqtt library to your environment

pip install paho-mqtt
1 Like

Oh this is really fast :smiley: Many thanks! I´l ltry it and give you feedback. paho-mqtt is neccesarry even if I have mosquitto? Isn´t it some kind of alternative of mosquitto? (i try to have system as clean as possible).

Yes, its the python interface into mqtt.

This may be of use to you, depending on what else you have to control…

Does anybody knows hot to correct setup configuration.yaml if mosquitto is running on the same device (raspy3)? In this case home assistant is a broker or a client? I really don= t know hot to get mqtt received codes from rf into HA. Could anyone help?

HA is always a client, the broker is mosquitto… I have my broker mosquitto on a chip (mini linux computer), but the setup in HA is exactly the same as HA is a client so … in HA configuration.yaml

mqtt:
  broker: !secret mqtt_broker  <---- ip addr of broker i.e. addr of pi that HA is on if broker on same pi
  port: !secret mqtt_port  <----- 1883
  client_id: home-assistant
  username: !secret mqtt_usern <------ mosquitto username
  password: !secret mqtt_pw <-------- mosquitto pw
  keepalive: 60
  protocol: 3.1.1

Then to subscribe to a topic in switch.yaml

  - platform: mqtt
    name: "Bedroom Switch"
    state_topic: "home/bedroom/switch1"
    command_topic: "home/bedroom/switch1/set"
    availability_topic: "home/bedroom/switch1/available"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

For a check you can use the command line tools mosquitto_pub shipped with mosquitto to send MQTT messages. This allows you to operate your switch manually:

$ mosquitto_pub -h 127.0.0.1 -t home/bedroom/switch1 -m “ON” <-------- you may need to add pw and username see --help

1 Like

Thanks @keithh666 you are lightning fast as usually :slight_smile: You are really helpful. But how do you recommmend to set the “switch”? I used rflink on one of my raspberrys. There were 433 motion sensors connected to rflink and while rflink sent only single code it was OK for switch which I returned back to OFF state via automation.

But how about a 433 ring button which I use now with your rpi-rf_receive-mqtt script? This device makes this with rpi.rf:receive script: [INFO] rpi-rf_receive: 14110972 [pulselength 198, protocol 1]

So many lines how long I hold the button. I think using only rpi-rf_receive-mqtt there will be many lines of the same code. Do you recommend to use switch component - and how?

Isn´t this small bug?

mqttauth = {‘username’:“username”, ‘password’ : “” }

there is space between password and : and “” - should it be?

I don’t think it will matter to HA if you get lots of on messages, but you could modify the python code to only send 1 MQTT message per press of the doorbell. I would have thought the firmware in the doorbell would only send 1 on RF message and 1 off RF message? Does RF Link not work with the Doorbell?

RFlink should work. But rflink is connected on my other device (not in home). At home I have only cheap RXB6 heterodyne RF receiver. Using your script I can receive signals and read them in command line. But please read my post below. I can´t get them into HA using MQTT.

The code that @gpbenton - Graham did for you publishes to the topic of ‘test’, so taking my example…

- platform: mqtt
    name: "Doorbell"
    state_topic: "test"
    command_topic: "test"
    payload_on: "your rx on code for Doorbell"
    payload_off: "your rx off code for Doorbell"
    optimistic: false
    qos: 0
    retain: true