Last active
March 23, 2019 22:53
-
-
Save andreasgruener/9935ffecbc59404375e7a9cb4a9a8812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Remove Homie Device from MQTT | |
# | |
# Simple Script to remove all reatined messages for a homie device, effectively removing the device from MQTT. | |
# The script get all topics "below" a device e.g homie/a020a615f72c/$mac and | |
# send a NULL message with mosquitto_pub to the topic. This removes the retained | |
# message and the topic will not show up anymore. | |
# | |
# How? | |
# mosquitto_pub -r -n --> sends an empty retained message, which clears the retained message and therefor the topic | |
# | |
# Example: | |
# $ ./clear-retained-messags.sh a020a615f72c | |
# | |
# Clearing for device a020a615f72c all messages, last clean will block kill with Ctrl+C when nothing happens anymore. | |
# | |
# STARTING :: Checking for valid topics one after the other.... | |
# 0 Checking for Topic .... | |
# homie/a020a615f72c/$homie >> sending empty payload to topic with retained flag | |
# 1 Checking for Topic .... | |
# homie/a020a615f72c/$mac >> sending empty payload to topic with retained flag | |
# 2 Checking for Topic .... | |
# homie/a020a615f72c/$name >> sending empty payload to topic with retained flag | |
# 3 Checking for Topic .... | |
# homie/a020a615f72c/$localip >> sending empty payload to topic with retained flag | |
# | |
echo | |
echo "Clearing for device $1 all messages, last clean will block kill with Ctrl+C when nothing happens anymore." | |
echo | |
#mosquitto_sub -v -h 10.0.2.2 -t "homie/$1/#" -C $2 | |
echo "STARTING :: Checking for valid topics one after the other...." | |
# will stop after 100 topics | |
MAX_TOPICS_TO_CHECK=100 | |
for c in `seq 0 $MAX_TOPICS_TO_CHECK` | |
do | |
echo " $c Checking for Topic ...." | |
for i in `mosquitto_sub -v -h 10.0.2.2 -t "homie/$1/#" -C 1|cut -d' ' -f1` | |
do | |
# echo "mosquitto_pub -h 10.0.2.2 -r -n -t \"`echo $i | sed s#'\\$'#'\\\\\\\$'#g`\"" | |
echo " $i >> sending empty payload to topic with retained flag" | |
mosquitto_pub -h 10.0.2.2 -r -n -t "`echo $i | sed s#'\\$'#'\\\$'#g`" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove Homie Device from MQTT
Simple Script to remove all reatined messages for a homie device, effectively removing the Homie device from MQTT.
The script get all topics "below" a device e.g homie/a020a615f72c/$mac and send a NULL message with mosquitto_pub to the topic. This removes the retained message and the topic will not show up anymore.
Also works for MQTT Topic Tree not related to Homie.
How?
Subscribing with mosquitto_sub '#' to the sub topics and iterating over all sub topics up to MAX_TOPICS_TO_CHECK
mosquitto_pub -r -n
--> sends an empty retained message, which clears the retained message and therefor the topicExample: