In https://community.home-assistant.io/t/how-best-to-deal-with-mqtt-sensor-name-changing-after-battery-change/718079/3, I asked how do you handle the fact that every time you change the power (more generally disconnect power) to cheap 433MHz temperature sensor, the channel id changes.
If you are using OpenMQTT Gateway (OMG) RTL-433
to read the sensor data, OMG will understandably see this as a new sensor and publish a new homeassistant config
line to the MQTT broker resulting in a totally new device and set of entities being created in HA, each time.
This has several problems:
- It creates new devices & entities every time the batteries are removed & replaced
- This results in data history discontinuities as the old entities stop receiving data and are replaced by new entities
- If you have multiple sensors from the same brand, the device & entity names are hard to remember since they are differentiated only by their channel & id numbers (or you could each time go in and replace the friendly name for each of them)
- It messes up integrations and scripts, since the entity names changes and you need to edit each automation referencing the relevant sensors
In the previous topic, I showed one way to get around this.
But here is a much simpler and slicker method.
Once set up, it requires only a single line Bash script to be run from an SSH shell or terminal on HA.
-
Change the ‘discovery_prefix’ in OMG
(under: Configuration → Configure MQTT: MQTT Discovery Prefix)
[This will also have the nice side benefit of keeping random neighbor 433 MHz devices from creating sensors in your HA instance while still allowing you to keep MQTT discovery turned on in HA]
For example, change it tohomeassistant-staging
[Note: Alternatively, you could change the discovery_prefix in HA to something other thanhomeassistant
] -
Whenever you want to add a new sensor use
MQTT Explorer
to look in thehomeassistant_staging
tree list of discovery topics to determine which (new) ones have appeared – you will see a full collection of all sensors seen by OMG, including random neighbor devices so be careful to identify your new device. -
(a) If the device/entity has a fixed name that doesn’t change when the battery changes (this is true for example for Govee battery sensors), you can just republish the corresponding discovery topic under the
homeassistant
prefix used by HA as follows (for the Govee example) using an SSH shell or terminal on HA.
For the Govee exmple,
(TOPICGREP="Govee";NEW_PREFIX=homeassistant; OLD_PREFIX=homeassistant-staging; USER="MQTT"; PASSWD='<YOURMQTTPASSWD>'; TIMEOUT=2; IFS=$'\n'; timeout ${TIME\
OUT}s mosquitto_sub -h homeassistant -u MQTT -P "$PASSWD" -t $OLD_PREFIX/# -v | grep -i "$TOPICGREP" | sed -e 's/^ *//g' -e 's/ *$//g' | tr -d $'\n' | sed -e 's#\(^\|}\) *\([^{}]*\
/config\) \+{#\1\n\2\n{#g' -e '$a\\' | tail -n +2 | while { read -r TOPIC; read -r MESG; }; do echo mosquitto_pub -h homeassistant -u MQTT -P "$PASSWD" -r -t "${TOPIC/#$OLD_PREF\
IX/$NEW_PREFIX}" -m "$(echo "$MESG" | jq -M -c)"; done)
Adjust TOPICGREP
as needed to uniquely identify the entities you want to add (note typically multiple entities per device)
- (b) If the device/entity has a channel id-dependent name, then you can republish using a custom name without the channel id (which will change and be meaningless every time you remove the batteries)
For example, the following works for LaCrosse, AmbientWeather, and Nexus temperature sensors (for others you may need to tweak the find/replace logic)
(ORIG="M-NNN"; NEW=<YOURNEWDEVICENAME>; SRC=homeassistant-rtl433; DEST=homeassistant; PASSWD='<YOURMQTTPASSWORD>'; IFS=$'\n'; for line in $(timeout 1 mosquitto_sub\
-v -h homeassistant -u MQTT -P "$PASSWD" -t "$SRC/#" | sed -n "s/$ORIG/$NEW/pg"); do topic=${line%%/config *}; message=${line#*/config }; mosquitto_pub -h homeassistant -u MQTT -\
P "$PASSWD" -t "${topic/$SRC/$DEST}/config" -m "$message" -r; done)
where M is the ‘channel’ and N is the ‘id’
Whether you used method 3a or 3b, HA should create the appropriately name new devices & entities.
- Whenever you replace the battery for a device whose name & id change, again use MQTT Explorer to look in the ‘home’ tree of MQTT explorer to find the new ‘id’.
For me it’s under:
home -> OMG_lilygo_rtl_433_ESP -> RTL_433toMQTT -> <Device Model> -> <Channel> -> <id>
Then all you need to do is run the following Bash command from an ssh shell or terminal to overwrite* the old discovery topic with the old channel/id names with an updated discovery topic that has the correct new channel/id.
(NAME=LaCrosse-TX141THBv2"; ORIG="1/26"; NEW=1/73; PREFIX=homeassistant; PASSWD='<YOURMQTTPASSWORD>'; IFS=$'\n'; for line in $(timeout 1 mosquitto_sub -v -h\
homeassistant -u MQTT -P "$PASSWD" -t "$PREFIX/#" | sed -n "s#\(\"\(stat_t\|state_topic\)\":\".*\)\($NAME/$ORIG\)\"#\1$NAME/$NEW\"#p"); do topic=${line%%/config *}; message=${li\
ne#*/config }; mosquitto_pub -h homeassistant -u MQTT -P "$PASSWD" -t "${topic}/config" -m "$message" -r; done)
where NAME
is the identifier found in the config (and home) trees to identify the device model (typically in the config name just before the channel & id numbers.
Note: For other devices you may need to play with the find/replace logic to make the appropriate change to the id.
Note: The above doesn’t change the messages captured in the home
tree, nor does it change the device or entity naming elements that go in the relevant HA device and entity registries (and home-assistant_v2.db
database). The new published discovery topic simply tells the HA MQTT integration where to look in the home
tree to find new messages for the devices & entities previously set up.
Note: You don’t need to delete the old discovery topic, since the new one overwrites it (using the same topic name)