安装mqtt获取CPU温度

安装 mqtt 客户端

sudo apt install mosquitto mosquitto-clients

验证安装

sudo systemctl is-enabled mosquitto

sudo systemctl status mosquitto

测试

sudo mosquitto_sub -h localhost -t test

新开一个终端并输入

sudo mosquitto_pub -h localhost -t test -m "Hello from terminal 2"

设置用户 yhkz 及密码

sudo mosquitto_passwd -c /etc/mosquitto/.passwd yhkz

新建配置

sudo nano /etc/mosquitto/conf.d/auth.conf

加入下面配置文件，ctrl+x 保存退出

listener 1883

allow_anonymous false

password_file /etc/mosquitto/.passwd

重启服务

sudo systemctl restart mosquitto

使用用户密码测试发送消息

sudo mosquitto_sub -h localhost -t test -u "yhkz" -P "p4ssw0rd"

sudo mosquitto_pub -h localhost -t "test" -m "Hello - enabled auth" -u "yhkz" -P "p4ssw0rd"

编辑定时任务

crontab -e

1-59/1 * * * * mosquitto_pub -h 192.168.5.250 -u yhkz -P yh -t pve/temp -m sensors | grep -E 'id 0' | cut -c17-20

hass 中配置文件添加如下配置

mqtt:

sensor:

name: cputemp

icon: 'mdi'

state_topic: 'pve/temp'

device_class: temperature

unit_of_measurement: "°C"

homeassistant/config/packages 添加 cpu.yaml，复制上面内容
集成添加设备 MQTT，配置 IP，用户及密码
扩展：发送硬盘温度
1-59/1 * * * * mosquitto_pub -h 192.168.5.250 -u yhkz -P yh -t hdd/temp -m "hddtemp /dev/sdb | grep -E 'HUS726040ALE610' | cut -c33-35"