I’m trying to set up some MQTT sensors to take values from a json string sent from a Python script on another device (a Pi running DietPi) and I can’t get them to show a state other than “unknown” in developer tools.
I’m wondering if there’s something wrong at the source end with something not being persistent or somesuch, as when I came back to it, there was no entry in MQTT explorer for them. But it looks like at least the HA end seems to be working now.
So I’ll call it closed from HA side, looks like I maybe couldn’t see anything wrong at that end as there perhaps wasn’t anything…
Does the python script publish the payload as a retained message?
If it doesn’t, restarting Home Assistant will cause all DietPi sensors to report unknown on startup. They will acquire values only at the moment the script publishes a payload. I assume the script runs according to some polling interval. So if the interval is 5 minutes, then the worst case scenario is the sensors report unknown for 5 minutes before next payload is published.
To avoid this momentary lapse of data, modify the python script to publish the payload as a retained message (i.e. retain = true).
@123 I thought I had, but now I go back and check it looks like in the editing that got lost. That would indeed explain it perhaps…
Anyway I also note that with the original sensors, the update one has no state. It looks like that’s hitting a keyword or something, as if I change “update” to “available” then it works fine.
I’m also going to slim them down to one, with that one as the main and the others in as attributes of it. Already got that running and it works fine now.
Yes, it reports unknown until it updates. You’re on the money as normal
Anyway I’ve added the retain in where it should be, and that’s resolved it.
The script for testing was updating once a minute, but it’s now stepped back to once an hour as the notifications don’t need to be that fast.
Just need to play with the Lovelace set-up now, as it looks like attributes can’t be shown on multiple entities cards. Might have to restore the individual sensors after all, but no big issue.
Glad to hear it. Typically when an MQTT Sensor reports unknown occasionally and seemingly inexplicably, it’s often due to disconnects/reconnects with the broker and the previous payload wasn’t stored.
Please consider marking my post above with the Solution tag so that users experiencing a similar problem are directed to the appropriate post.