Context:
I have a Raspberry Pi Zero W on my desk. Attached to the Pi is a Sense Hat and an LCD screen.
The purpose of which is to show the current temperature, Humidity, Pressure and Time.
The python code was written a few years ago by my grandfather, based on research from other peoples code. Once I inherited it, I cleaned up the code a little (adding the time feature, which replaced the temperature in Fahrenheit) and its just been sat on my desk looking cool.
I decided I wanted to pass the temperature reading to home assistant for reasons.
I did a bit of research and found the paho.mqtt.client python module and imported that into my code.
import paho.mqtt.client as mqtt
I added the broker details and the topic:
#MQTT broker settings
broker_address = "<IP_ADDRESS>"
broker_port = 1883
topic = "homeassistant/sensor/office_temperature"
Defined the connection event:
#callback when connection to the broker is established
def on_connect(client, userdata, flags, rc):
print("connected with result code "+str(rc))
Initialised the client:
#Initialise MQTT Client
client = mqtt.Client()
client.on_connect = on_connect
client.username_pw_set(username="mqtt_user",password="<password>")
#Connect to the broker
client.connect(broker_address, broker_port, 60)
#Publish temperature to MQTT topic
client.publish(topic, temp1)
Then from within the MQTT Explorer in home assistant, we can see the topic and the data:
To get this as a sensor in home assistant, add an addition to the configuration.yaml
sensor:
- name: "Office Temperature"
state_topic: "homeassistant/sensor/office_temperature"
Expanding:
The "Sun Solar Elevation" entity is from the Sun Integration, which gives me an rough idea of where the sun is and to use for other automation's.
I already have an old android tablet sat on the other side of my desk, which displays the above dashboard, but I wanted to add a line on to the Pi's LCD display to show the battery's current charge level.
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
#callback when message from the topic is received
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
global battmess
battmess=str(msg.payload)
#Initialise MQTT Client
client = mqtt.Client()
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect(broker_address, broker_port, 60)
client.subscribe(topic_battery,2)
client.publish(topic, temp1)
client.loop_start()
No comments:
Post a Comment