Was looking for a way to monitor my NUC temperature on Proxmox installation and Hass.io supervisor running a a VM. And, as I could not find something with MQTT, I build one.
So here it is Python script running in crontab on Proxmox, sending data to Mosquito server:
nuc_info.py
=========
import paho.mqtt.client as mqtt
import subprocess
from requests import post
# function
def connect_msg():
print('Connected to Broker')
# function
def publish_msg():
print('Message Published')
# Creating client
MQTT_IP = "192.168.1.XXX"
MQTT_PORT = "1883"
# Retrieving CPUs temperatures from sensor
cpus = subprocess.check_output("sensors|grep 'Core'|awk '{print $3}'| rev | cut -c 4- | rev | cut -c2-", shell=True)
cpus = cpus.decode()
cpus = cpus.splitlines()
# Topics to publih for MQTT
TOPIC_TO_PUBLISH1 = "homeassistant/sensor/NUC_system/cpu1"
TOPIC_TO_PUBLISH2 = "homeassistant/sensor/NUC_system/cpu2"
TOPIC_TO_PUBLISH3 = "homeassistant/sensor/NUC_system/cpu3"
TOPIC_TO_PUBLISH4 = "homeassistant/sensor/NUC_system/cpu4"
client = mqtt.Client(client_id='NUC_system')
# Connecting callback functions
client.on_connect = connect_msg
client.on_publish = publish_msg
# Connect to broker
client.connect(MQTT_IP,MQTT_PORT)
# if you experience Socket error then replace above statement with following one
# client.connect("192.168.1.XXX",1883,60)
# Publish a message with topic
client.publish(TOPIC_TO_PUBLISH1,cpus[0])
client.publish(TOPIC_TO_PUBLISH2,cpus[1])
client.publish(TOPIC_TO_PUBLISH3,cpus[2])
client.publish(TOPIC_TO_PUBLISH4,cpus[3])
# Run a loop
client.loop()
I didn’t want to have to worry about installing any additional dependencies at the Proxmox host level, so instead I was able to set up host CPU temperature tracking in Home Assistant using a shell script (requires MQTT):
Substitute your thermal_zone number, add in a long-lived access token for the authorization, and fill in your IP. Then save that as cpu_temp.sh and schedule it to run in cron. It will push the CPU temperature to the pve/cpu_temp topic in MQTT. Then, you can extract it in Home Assistant with the following sensor:
- platform: mqtt
name: CPU Temperature
state_topic: "pve/cpu_temp"
value_template: "{{ value | multiply(0.001) | round(1) }}"
Nice alternative.
In my case the need was also to monitor used space of whole proxmox box, thus I could easily adapt similar mqtt approach to report used disks.
almost two years later, but sir, you made my day. Been trying all kind of python stuff, nothing worked.
This was fairly easy! I only changed the value_template | Round(1) to | Int because the value would always have a 0 as a decimal (which just annoys me)
Thanks for this useful script, I made a json version to compine the cpus in one publish:
Change the UPPERCASE variables to your needs and it will generate a message like