Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtl4332mqtt support for multiple devices? #9

Open
ianfiske opened this issue Dec 8, 2017 · 7 comments
Open

rtl4332mqtt support for multiple devices? #9

ianfiske opened this issue Dec 8, 2017 · 7 comments

Comments

@ianfiske
Copy link

ianfiske commented Dec 8, 2017

First, thank you for the awesome rtl4332mqtt addon! I'm using it with an Ambient Weather F007TH Thermo-Hygrometer outdoors and it works great on our farm. I'm thinking of adding more sensors, but wanted to check if you know how easy it will be to adapt the rtl4332mqtt.sh script to handle multiple sensors before I go and makes the purchase. I suppose the mqtt sensor would need to somehow parse the single event steam into multiple sensors... or, rtl4332mqtt.sh could be modified to push events to multiple mqtt topics. I'm curious about your thoughts on this.

@james-fry
Copy link
Owner

Hi Ian.
Very pleased it's working for you.
You ask a very good question, and and raised what is a clear use case for this add-on.
Unfortunately it's not a questions that I can categorically answer since I only currently have a single sensor to test with.

However, it looks like one of the repos I based the add-on on does use multiple sensors (albeit very similar binary door/window sensors).
https://github.com/chriskacerguis/honeywell2mqtt

Chris mentions that his solution is working with HA.
I can see that his mqtt payload contains both a sensor type string (model) and what looks like a unique ID (id), so I assume that he's doing the "clever stuff" in HA.

{
"time" : "2017-08-17 13:18:58",
"model" : "Honeywell Door/Window Sensor",
"id" : 547651,
"channel" : 8,
"event" : 4,
"state" : "closed",
"heartbeat" : "yes"
}

Good luck with any changes you make.
Feel free to submit a PR if you get it working - either to the main script if the change is generic, or as an alternative script if not.
Also maybe reach out on the HA community - I think both the repo owners who I forked are lurking there + maybe someone else already did what you need.

@ianfiske
Copy link
Author

ianfiske commented Dec 8, 2017

Thank you for the pointers, James. I'll give it a try and keep you posted and also reach out to HA community if I get stuck. Not a big investment for one more of those sensors anyhow. If I find something useful, I'll definitely submit a PR.

@jchasey
Copy link

jchasey commented Jan 28, 2018

I've been looking into this and have a working (albeit non-elegant) solution by editing the shell script. There are a few challenges when dealing with multiple sensors.

The HA options assumes just one sensor, so would need to be updated to support multiples sensor protocols being passed. However different protocols have different json payloads and therefore you want to publish each sensor to a different mqtt topic, so really an array of protocols and mqtt topics need to be set.

It gets a bit more difficult however as the json output of rtl_433 weirdly does not include the protocol number. Therefore you have to parse the model string in the json to determine which device the payload refers to and therefore which mqtt topic to publish to.

The end result of this is that I have a working solution with some hardcoded values but which is not general purpose. The key changes are as follows:

PROTOCOL="-R 42 -R 43"	# Hardcode as we want multiple protocols 
/usr/local/bin/rtl_433 -F json $PROTOCOL | while read line
do
	# We don't know from the output which protocol number was received
	# However we can extract the device name from json output
	DEVICE="$(echo $line | jq --raw-output '.model')"
	
	if [ "$DEVICE" = "HIDEKI TS04 sensor" ]; then
	  MQTT_TOPIC="home/rtl433/weather"
	fi
	if [ "$DEVICE" = "Oil watchman" ]; then
	  MQTT_TOPIC="home/rtl433/oil"
	fi

	# Publish the json to the appropriate topic
	echo $line | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_TOPIC
done

Then in HA you simply have a number of mqtt sensors as appropriate triggered from the relevant topics.

@james-fry
Copy link
Owner

Thanks for the detailed response. Hope this helps @ianfiske
I'd be happy to roll it into the add-on (or at least add to the readme)

@jchasey
Copy link

jchasey commented Jan 29, 2018

Yes please feel free to use

@messismore
Copy link
Contributor

I have several devices of the same model so I tried a more general approach. If there is a model or id in the message, it will be appended to the message's path, replacing spaces with underscores.

So

{
  "model": "Some device",
  "id": 42,
  …
}

will send the message to $MQTT_PATH/model/id.

Setting

logger:
 default: warning
 logs:
   homeassistant.components.mqtt: debug

in configuration.yaml will let you see the exact path of each device in the log.

I know very little bash, so you might want to have a good look at the changes first, but it works for me: #11

@asifma
Copy link

asifma commented Jan 22, 2019

Here is an alternative if you have for example PIR sensors that sends like 50 messages in a row when there is motion. This do loop will make the mqtt pub lag as it needs to loop each line.
Just check if the line was not same as previous line.

PREV_LINE=""

/usr/local/bin/rtl_433 -F json -G | while read LINE
do
  # Skip duplicate lines
  if [ "$LINE" != "$PREV_LINE" ]; then

  DEVICE="$(echo $LINE | jq --raw-output '.model' | tr -s ' ' '_')" # replace ' ' with '_'
  DEVICEID="$(echo $LINE | jq --raw-output '.id' | tr -s ' ' '_')"

  MQTT_PATH=$MQTT_TOPIC

  if [ ${#DEVICE} > 0 ]; then
    MQTT_PATH=$MQTT_PATH/"$DEVICE"
  fi
  if [ ${#DEVICEID} > 0 ]; then
    MQTT_PATH=$MQTT_PATH/"$DEVICEID"
  fi

  # Create file with touch /tmp/rtl_433.log if logging is needed
  #[ -w /tmp/rtl_433.log ] && echo $line >> rtl_433.log
  echo $LINE | /usr/bin/mosquitto_pub -h $MQTT_HOST -i RTL_433 -l -t $MQTT_PATH
  fi
  
  # Set the previous line
  PREV_LINE="$LINE"
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants