2 minutes
Log CPU Temps with Home Assistant
I recently purchased a System76 Lemur Pro for a daily driver. So far I love the laptop! However, the internal fan is annoying. I am trying to determine if this laptop cooler is acually helping keep my laptop cool. I already have Home assistant tracking the temprature of my house why time my CPU too!
I am using this quick little python script to send the data to home asssitant via MQTT
import psutil
import paho.mqtt.client as mqtt
import time
# MQTT broker settings
MQTT_HOST = "192.168.255.17"
MQTT_PORT = 1883
MQTT_TOPIC = "laptop/cpu_temperature"
mqtt_username = "mqtt"
mqtt_password = "mqtt"
MQTT_TOPIC_fan = "laptop/cpu_fan"
# Publish temperature to MQTT topic
client = mqtt.Client("python-mqtt-laptop")
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(MQTT_HOST, MQTT_PORT)
while True:
# Get CPU temperature
cpu_temp = psutil.sensors_temperatures()['system76_acpi'][0].current
cpu_fan = psutil.sensors_fans()['system76_acpi'][0].current
client.publish(MQTT_TOPIC, cpu_temp)
client.publish(MQTT_TOPIC_fan, cpu_fan)
print(cpu_temp)
print(cpu_fan)
time.sleep(5)
Not sure if I will run this under cron in the background yet or not. For now I just have the terminal open.
Lets get the data logging in home assistant by adding this to our config.yaml
mqtt:
sensor:
- name: "Laptop CPU Temp"
state_topic: "laptop/cpu_temperature"
suggested_display_precision: 1
unit_of_measurement: "°C"
- name: "Laptop CPU Fan"
state_topic: "laptop/cpu_fan"
suggested_display_precision: 1
unit_of_measurement: "RPM"
All set! Just run the python script and you are logging! You mean need to pip install paho.mqtt if you havent already.
217 Words
2023-03-30 21:29