I have a number of Lorawan sensors connected to HA via MQTT. Some of these report perfectly and always show a value. Others have gaps in reporting, as per the image below. I would like these ‘gappy’ sensors always to show the last-received value.
I think what is happening is that I have multiple sensors using the same MQTT state topic, and so when a second sensor reports via MQTT and that message doesn’t contain a json value that the first sensor is looking for, the first sensor switches to an ‘unknown’ value. Unfortunately, I think the act that they share a topic means that simply retaining MQTT messages may not be the answer.
Switching sensors to binary_sensor has solved the problem in some cases. But for sensors such as the soil moisture probe shown here, that’s clearly not an option.
What’s a simple way to force the dashboard to show the last value?
Wrong question: the dashboard should show the current value, but you can either change the entity it’s showing, or change how the current entity is configured.
Are these auto-discovered sensors or have you configured them?
Easiest way might be to configure a template sensor helper for each troublesome sensor, with a state template like:
That tries to convert the state of the soilmoisture sensor to a number, and returns the existing value of this sensor if it fails. Then show that “retained soil moisture” template sensor in the dashboard.
Here’s my entry for the soil moisture sensor above. The reason for looking for the dev_eui was in order to distinguish this from another which has the same mqtt topic. (I realise that’s probably daft and that I should filter the topics differently, but for a few reasons it’s less straightforward to change the mqtt broker).
Brilliant - seems to be working now and presents as a continuous variable in the dashboard. Will change the other misbehaving sensors similarly. Much appreciate your assistance!