Skip to content

Instantly share code, notes, and snippets.

@helgibbons
Last active August 28, 2023 22:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgibbons/5c93d0504398cd457e504aa7f8aeff25 to your computer and use it in GitHub Desktop.
Save helgibbons/5c93d0504398cd457e504aa7f8aeff25 to your computer and use it in GitHub Desktop.
Weather HAT - MQTT for Home Assistant
#!/usr/bin/env python3
from time import sleep
import weatherhat
import json
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
sensor = weatherhat.WeatherHAT()
# We can compensate for the heat of the Pi and other environmental conditions using a simple offset.
# Change this number to adjust temperature compensation!
OFFSET = -7.5
# We're using the Mosquitto HA add-on as our MQTT broker, other setups may vary!
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(username="brokerusernamegoeshere",password="brokerpasswordgoeshere")
mqtt_client.connect("homeassistant.local",1883) # you could also use the broker IP here
mqtt_client.loop_start()
#initial setup of mqtt sensors:
Dict1 = {
"name": "Weather Station Temperature",
"device_class": "temperature",
"state_topic": "homeassistant/sensor/WeatherTemp/state",
"unit_of_measurement": "C",
}
mqtt_client.publish("homeassistant/sensor/WeatherTemp/config",json.dumps(Dict1))
Dict2 = {
"name": "Weather Station Pressure",
"device_class": "pressure",
"state_topic": "homeassistant/sensor/WeatherPres/state",
"unit_of_measurement": "hPa"
}
mqtt_client.publish("homeassistant/sensor/WeatherPres/config",json.dumps(Dict2))
Dict3 = {
"name": "Weather Station Humidity",
"device_class": "humidity",
"state_topic": "homeassistant/sensor/WeatherHumi/state",
"unit_of_measurement": "%"
}
mqtt_client.publish("homeassistant/sensor/WeatherHumi/config",json.dumps(Dict3))
Dict4 = {
"name": "Weather Station illuminance",
"device_class": "illuminance",
"state_topic": "homeassistant/sensor/WeatherLux/state",
"unit_of_measurement": "lx"
}
mqtt_client.publish("homeassistant/sensor/WeatherLux/config",json.dumps(Dict4))
Dict5 = {
"name": "Weather Station Wind speed",
#"device_class": "none",
"state_topic": "homeassistant/sensor/WeatherWS/state",
"unit_of_measurement": "m/s"
}
mqtt_client.publish("homeassistant/sensor/WeatherWS/config",json.dumps(Dict5))
Dict6 = {
"name": "Weather Station wind gusts",
#"device_class": "none",
"state_topic": "homeassistant/sensor/WeatherWG/state",
"unit_of_measurement": "m/s"
}
#mqtt_client.publish("homeassistant/sensor/WeatherWG/config",json.dumps(Dict6)) #Not done yet
Dict7 = {
"name": "Weather Station wind direction",
#"device_class": "none",
"state_topic": "homeassistant/sensor/WeatherWD/state",
"unit_of_measurement": "none"
}
mqtt_client.publish("homeassistant/sensor/WeatherWD/config",json.dumps(Dict7))
Dict8 = {
"name": "Weather Station rain",
#"device_class": "none",
"state_topic": "homeassistant/sensor/WeatherRain/state",
"unit_of_measurement": "mm/s"
}
mqtt_client.publish("homeassistant/sensor/WeatherRain/config",json.dumps(Dict8))
#read the BME280 and discard the initial readings
sensor.update(interval=10.0)
sensor.temperature_offset = OFFSET
temperature = sensor.temperature
humidity = sensor.relative_humidity
pressure = sensor.pressure
print("Discarding the first few BME280 readings...")
sleep(10.0)
# Now send some data
while True:
sensor.update(interval=30.0)
wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction)
temperature = sensor.temperature
humidity = sensor.relative_humidity
pressure = sensor.pressure
light = sensor.lux
windspeed = sensor.wind_speed
winddirection = wind_direction_cardinal
rain = sensor.rain
try:
mqtt_client.publish("homeassistant/sensor/WeatherTemp/state", round(temperature,2))
mqtt_client.publish("homeassistant/sensor/WeatherPres/state", round(pressure,2))
mqtt_client.publish("homeassistant/sensor/WeatherHumi/state", round(humidity,2))
mqtt_client.publish("homeassistant/sensor/WeatherLux/state", round(light,2))
mqtt_client.publish("homeassistant/sensor/WeatherWS/state", round(windspeed,2))
mqtt_client.publish("homeassistant/sensor/WeatherWD/state", winddirection)
mqtt_client.publish("homeassistant/sensor/WeatherRain/state", round(rain,2))
print('Data sent to Home Assistant')
except Exception as e:
print(e)
sleep(30.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment