Sensors show as "unknown." Help me double check my settings, python code and mqtt code

Hi there! I’m new to Home Assistant (HA) and I’m attempting to get a moisture sensor to show in HA. I’ll list the hardware I’m using then show you the code I am using and hopefully you all can spot the problem causing HA to show the sensor data as "Unknown."

Hardware

  • Home Assistant is installed on a Raspberry Pi 3 (using HA recommended install)
  • Pimoroni Grow Kit Hat and Raspberry Pi zero W (This is what supplies 3 moisture sensors and one Lux sensor)

Libraries, Software and Guides

  • Pimoroni comes with a python based library maintained by them.
  • I’m using Eclipse paho for mqtt on the sensor device (used command pip3 install paho.mqtt).
  • I used This Guide and the related repository to get the python code for connecting the Pimoroni sensors to HA.

Code In Question

First is the Python code from the above mentioned guide (file called “watcher.py”). From what I can tell this all looks good. But I could be missing something.

import time
import json
import yaml
import ltr559
import sys, os, pathlib
from grow.moisture import Moisture
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    print(client.subscribe(broker().get('topic')))

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

#This code if for loading the config file which contains the login credentials for the broker (i.e. Home Assistant User account)
def load_config():

    pathlib.Path(__file__).parent.absolute()
    with open(os.path.join(pathlib.Path(__file__).parent.absolute(), 'config.yaml')) as f:
        return yaml.load(f)


def broker():
    return load_config().get('broker')


def auth():
    return load_config().get('auth')


config = load_config()
broker = broker()
auth = auth()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(auth.get('username'), auth.get('password'))
client.connect(broker.get('host', 'homeassistant.local'), broker.get('port', 1883), 60)

print("Start submitting sensor data on MQTT topic {}".format(broker.get('topic')))

sensors = [Moisture(1), Moisture(2), Moisture(3)]
light = ltr559.LTR559()

while True:

    i = 0
    payload = {"light": light.get_lux()}
    for i in range(0, len(sensors)):
        payload["sensor_{}".format(i)] = {
            "moisture": sensors[i].moisture,
            "saturation": sensors[i].saturation
        }

    client.publish(broker.get('topic'), json.dumps(payload))

    print(json.dumps(payload))

    time.sleep(30)

client.loop_forever()

This is the code from the connected file (named “config.yaml”) giving the credentials for the broker, i.e. HA credentials.

broker:
    port: 1883
    host: HOST_IP_ADDRESS # 192.168.86.x
    topic: TOPIC          # home/livingroom/plants

auth:
    username: USERNAME    # MQTT username
    password: PASSWORD    # MQTT password

This is the code I put within HA’s configuration.yaml. The sensors show up in HA, but are shown as “unknown” I am thinking the error might be in here somewhere.

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
# sensors.yaml: sensor 1 of 3
 - platform: mqtt
   name: "Saturation"
   state_topic: "home/livingroom/plants"
   value_template: "{{ value_json.sensor_0.saturation }}"
   json_attributes_topic: " home/livingroom/plants"
   json_attributes_template: "{{ value_json.sensor_0 | tojson }}"
   
# sensors.yaml: sensor 1 of 3
 - platform: mqtt
   name: "Moisture"
   state_topic: "home/livingroom/plants"
   value_template: "{{ value_json.sensor_0.moisture }}"
   json_attributes_topic: " home/livingroom/plants"
   json_attributes_template: "{{ value_json.sensor_0 | tojson }}"
   
#Board Light Sensor 
 - platform: mqtt
   name: "Lux"
   state_topic: "home/livingroom/plants"
   unit_of_measurement: 'Lux'
   value_template: "{{ value_json.light }}"
   json_attributes_topic: "home/livingroom/plants"
   json_attributes_template: "{{ value_json.light }}"

Here are the broker settings within HA.

Mosquitto Broker Configuration

certfile: fullchain.pem
customize:
  active: false
  folder: mosquitto
keyfile: privkey.pem
logins: []
require_certificate: false

Mosquitto Broker Integration Settings

Broker: core-mosquitto
Port: 1883
Username: MQTT-User
Password: PASSWORD

I hope I’ve given enough info for someone to be able to fix my issue. Thanks for taking a look!

Screen Shot 2022-02-17 at 6.18.43 PM

I could only put 2 links in the main because I’m a new user.

Here is a link to the Pimoroni setup guide I used
Here is a direct link to the repository in the mqtt setup guide

Welcome to HA! :slightly_smiling_face:
Have you checked the mqtt topic with a mqtt client?
If this woks, there was a breaking change in the last release for binary sensors.
They are ‘unknown’ on HA start and need a state change to show the real state.
So if your sensors work after a state change (client.publish) you can avoid this by publishing with the retain flag.

Thanks, Could you please be a bit more specific in relation to my code above? I’m trying to figure out MQTT and HA all at once, an example of where i should add your suggestion would help.

From what I can see my current watcher.py has client.publish(broker.get('topic'), json.dumps(payload))

Are you suggesting if I add a retain flag to this line? For example something like this:
client.publish(broker.get('topic'), json.dumps(payload), qos=0,retain=True)

Yes, if your sensor works, this should prevent the unknown state at HA start.

What do you mean by “if your sensor works?”

It means if your python code fails to publish a payload to home/livingroom/plants the two MQTT Sensors you defined in Home Assistant will report unknown.

Use an MQTT client, like MQTT Explorer, to confirm your python code is publishing what you expect it should to home/livingroom/plants. If nothing is published to the topic, you should focus your attention on the python code. If it is publishing to the topic then focus your attention on Home Assistant’s connection to the MQTT broker.

1 Like

Hmmm, This is what my MQTT Explorer shows.

The screenshot you posted doesn’t show anything relevant to solving the problem. We need to see the topic your python script is publishing to, not the topics related to the MQTT broker’s performance metrics.

Before we proceed any further, how would you rate your familiarity with MQTT?

Very little. My quick backstory is I started a nonprofit. Our mission is to reduce food insecurity by creating sustainable micro-farms in neighborhoods and sharing the bounty with those in need. We want to use readily available IoT components to build our farm monitoring system. We are choosing MQTT and Raspberry Pi because it is something others will be able to utilize. We don’t want to be software/hardware developers but rather implement an open-source style system that people can use. I’m looking for volunteers to help with the tech, until then its up to me lol

My history is some server backend setup, and PHP/CSS/JS Website design (using Drupal and Wordpress CMS). So Python and MQTT is outside of my current knowledge, although I’m doing my best to learn, read documentation and watch youtube.

I’m new to HA, new to MQTT, and new to MQTT Explorer app. :slight_smile:

1 Like

Fantastic project, wonderful.

@newgardenfarms it looks like your MQTT server isn’t getting any messages from the MQTT sensor client. I say that because there should be home assistant messages at the same level as $SYS. $SYS is generated by the MQTT server software to give metrics about the MQTT server itself.

So the first thing to do is work that out.

The pi zero should be able to show you some logs which may reveal more.

YAYYYYY!!! HECK YEA! Thanks so much for this suggestion! the Pi logs showed it wasn’t connecting to the HA server because of a line of code that was (I assume) the tutorial writer’s IP address. Once i replaced that with the defined Broker Host from the config file, all is connected now.

Thanks to you and everyone else in the thread who helped me get there. The MQTT Explorer now shows “Home” and HA now has data instead of “unknown.”

Now i’m off to setup influxdb and grafana :slight_smile:

Best feeling in a while lol

Here’s a solution wrap up for everyone:

  1. inside command line use tail -f /var/log/syslog to see realtime errors
  2. make sure in your config files and your python code all “Host” areas are the same and pointing to the correct info.

Out of interest, what part of the world are you doing these microgardens in?

OK. That explains why your alleged Solution missed the mark. Others already explained what were the potential sources of the problem and where to look for clues to narrow it to the root cause. Most issues with MQTT are due to simple “plumbing” problems (one “pipe” not connected to another).

For future reference, I suggest you refer to this excellent series of tutorials (they helped me understand the fundamentals over three three years ago):

Good luck

We’re in SF Bay Area, California

I think You should reread his comment. From an outside standpoint, he was not saying anything with malice and was trying to help you learn more by providing additional learning resources. Please do not resort to name calling on these forums. If you have an issue with a post, flag it for moderation. Thanks.