Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 105808

Sensors show as "unknown." Help me make sure my settings, python code and mqtt code are correct

$
0
0

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!

2 posts - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 105808

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>