MQTT Sensor - A Little Help Please

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.

The sensors are defined as:

# MQTT sensors
  - platform: mqtt
    name: "DietPi Current"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json.current }}"
  - platform: mqtt
    name: "DietPi Latest"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json.latest }}"
  - platform: mqtt
    name: "DietPi Update"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json.update }}"
  - platform: mqtt
    name: "DietPi Apt"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json.apt }}"

(with sensor: !include_dir_merge_list sensors in configuration.yaml and the above in the sensors directory as dietpi.yaml)

Output from the script on the DietPi device is

  "current": "7.5.2",
  "latest": "7.5.2",
  "update": false,
  "apt": 0

As viewed from MQTT Explorer, with the full output being

response = Connected
dietpi = {"current": "7.5.2", "latest": "7.5.2", "update": false, "apt": 0}

Where response is just a topic set on connection to show it’s connected.

I’m obviously doing something dumb here - can someone point me in the right direction to get the values into HA as sensors?

I cant see anything wrong with what you have done. What state does this return:

  - platform: mqtt
    name: "DietPi Test"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json }}"

If nothing, what about this:

  - platform: mqtt
    name: "DietPi Test"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value }}"

@tom_l Thanks for the suggestions. The first one gives

{'current': '7.5.2', 'latest': '7.5.2', 'update': False, 'apt': 0}

and the second gives

{"current": "7.5.2", "latest": "7.5.2", "update": false, "apt": 0}

But now the other ones are working too?

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… :slight_smile:

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.

  - platform: mqtt
    name: "DietPi Update Available"
    state_topic: "vpnpi/dietpi"
    value_template: "{{ value_json.available }}"
    json_attributes_topic: "vpnpi/dietpi"

So I think we’re done here overall now…

Don’t change anything and restart Home Assistant. Let us know if the sensors report unknown on startup.

Also, what is the python script’s publishing frequency? Every 15 seconds, every minute, or what interval?

Yes, it reports unknown until it updates. You’re on the money as normal :slight_smile:

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.

I was coming back to do that very thing, but I see someone else already has. So all is good. And thanks again for the insight.

1 Like