[SOLVED: layer 8 issue] MQTT sensor just won't update!

Hi all,

The tldr; is that I have an MQTT sensor that simply isn’t updating, but I can’t for the life of me figure out why, or what to look at next.

THE SCENARIO
I have a Python script that I run on a particular host that, among other things, records the time it last ran in a specific topic. I have a corresponding sensor in HA that picks this up and displays it for me.

Until recently my MQTT server was running in a mosquitto docker image on a Raspberry Pi. A couple of weeks ago, I migrated a bunch of docker containers (including mosquitto) over to a beefier machine, in a different subnet (I have a few VLANs for various purposes).

Since then, this particular sensor has steadfastly refused to update. All other sensors that are subscribed to adjacent topics are still working just fine.

THE DETAILS
I have a sensor with the entity ID of sensor.internet_last_tested, configured thusly:

- platform: mqtt
  state_topic: "internet/lasttest"
  force_update: true
  name: 'Internet last tested'

NOTE: I only recently turned on force_update to see if it would help. It didn’t.

MQTT is configured thusly:

mqtt:
  broker: 192.168.254.15
  port: 1883

Like I said, I have other topics adjacent to this. These topics are updated by the very same Python script (unchanged, except to point to the new MQTT server), and all sensors that depend on these other topics are updating just fine, but this particular sensor stubbornly refuses.

So, I turned on debug logging and ran some mosquitto_pub commands from the host that runs the Python script (another Raspberry Pi). This is where my confusion kicks into high gear.

I can see the updated topic is picked up by HA. I can see that it claims to update the sensor. But the state of the sensor is still false:

Screen Shot 2020-08-19 at 18.30.56
HA’s debug log tells me the following:

hass    | 2020-08-19T07:15:14.002406553Z 2020-08-19 17:15:13 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on internet/lasttest: b'11:38 18-Aug-2021'
hass    | 2020-08-19T07:15:14.005202747Z 2020-08-19 17:15:13 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.internet_last_tested, old_state=<state sensor.internet_last_tested=17:09 19-Aug-2020; friendly_name=Internet last tested @ 2020-08-19T17:14:26.756810+10:00>, new_state=<state sensor.internet_last_tested=11:38 18-Aug-2021; friendly_name=Internet last tested @ 2020-08-19T17:15:13.988074+10:00>>
hass    | 2020-08-19T07:15:14.018599338Z 2020-08-19 17:15:14 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1872557008] Sending {'id': 14, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.internet_last_tested, old_state=<state sensor.internet_last_tested=17:09 19-Aug-2020; friendly_name=Internet last tested @ 2020-08-19T17:14:26.756810+10:00>, new_state=<state sensor.internet_last_tested=11:38 18-Aug-2021; friendly_name=Internet last tested @ 2020-08-19T17:15:13.988074+10:00>>}

(NOTE: topic message is arbitrary text I sent in the command to make sure the value is actually changing

I’m all out of ideas on where to look next.

PLEASE HELP!

bump

Can anyone suggest additional troubleshooting I can undertake, please?

I can’t help but feel I’m hitting a weird HA/mqtt bug of some sort, but what mystifies me is that I have heaps of other sensors that aren’t displaying this issue.

First: Since the state in your screenshot shows ‘false’, are you sure you created it under sensor: and not under binary_sensor ?

Second: an easy way to troubleshoot mqtt is installing mqtt explorer, and look what happens there.

Cold comfort, but it works for me.

Screenshot from 2020-08-24 23-33-01

I used the same MQTT sensor config you posted to create the sensor. After restarting, I used MQTT Explorer to publish the string 17:09 19-Aug-2020 to the topic internet/lasttest and the string immediately appeared as the sensor’s state.

Hey - thanks for weighing in. I’ve been using MQTT Explorer and I can absolutely verify the topic is updated. And, the debug logs seemed to validate that, as HA is definitely seeing the update come through. It’s just that, for whatever reason, the sensor itself isn’t updating.

And, yep - it’s definitely a sensor, not a binary_sensor. I use stub yaml files, referenced via includes from the main configuration.yaml file.

It’s almost like the sensor is somehow going stale, and not updating, even though it’s seeing the topic update on the MQTT server.

I don’t normally struggle with issues like this - the path is usually clear, but this one has me stumped…

Thanks for the reply. Yeah - I’ve been using MQTT Explorer to both validate the changes via my Python script, and publish changes manually for testing.

Per the debug logs, HA definitely sees an update to the topic, but the sensor itself Just. Won’t Update.

What’s frustrating more than anything is that stuff like MQTT and sensors are really linear things - there shouldn’t be a lot of nuance to how they work, or what breaks them.

But this one just me really scratching my head…

Just for fun, create a new sensor that’s subscribed to the same topic.

OK - colour me embarrassed.

Creating the new sensor helped me track the issue - I had a piece of Javascript code that was trying to check IF the state for my problem sensor was false, but somehow was only using a single ‘=’ sign. So, effectively, the sensor kept getting changed to false.

I suspect the issue came from a bug I’ve been experiencing with the Lovelace editor in Firefox for Mac (may be present in other browser combos).

Essentially, when I open the code editor for a given card, and that code contains long-ish lines (eg. Javascript code), the typing cursor is misaligned within the window, and the first keystroke isn’t where you think it should be.

So, in short, I think that bug meant I accidentally deleted one of ‘=’ in the above-mentioned line of Javascript unintentionally, without realising.

I’ll see myself out…

1 Like