What & Why ?
If you already have MQTT integration in your Home Assistant, e.g. you are using Zigbee2MQTT or something similar - you already have a very powerful way to aggregate any data from your local server, remote server, public API, or anywhere else. There is only one limitation - data should be possible to obtain via Bash script (in other words - there are almost no limits)
Obviously, there are other ways to get desired data and send it to the MQTT broker, but for me, this was the fastest and easiest one.
Requirements:
For publishing into MQTT (required):
mosquitto_pub (sudo apt install mosquitto-clients)
For HDD temperature monitoring in example:
smartctl (sudo apt install smartmontools)
For data from HTTP/HTTPs and JSON parsing in example:
curl, jq (sudo apt install curl jq)
Installation:
Create & open file for future service:
sudo nano /etc/systemd/system/mqtt-monitoring.service
Copy-paste code below to the created file and replace /path/to/mqtt-monitoring.sh
with path to .sh file on your server, don't forget to save changes.
By default script will be invoked every 15 seconds, delay can be adjusted by changing RestartSec
value
[Unit]
Description=MQTT Monitoring
After=multi-user.target
[Service]
Type=idle
User=root
ExecStart=/bin/bash /path/to/mqtt-monitoring.sh
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
Set permissions, reload systemctl, enable service, reboot (for good measure)
sudo chmod 644 /etc/systemd/system/mqtt-monitoring.service
sudo systemctl daemon-reload
sudo systemctl enable mqtt-monitoring.service
sudo reboot
Example of entry in Home Assistant configuration.yaml:
sensor:
- platform: mqtt
name: 'HDD1 Temperature'
unit_of_measurement: '°C'
state_topic: 'mqtt_monitoring/host/temperature_sda'
- platform: mqtt
name: 'HDD2 Temperature'
unit_of_measurement: '°C'
state_topic: 'mqtt_monitoring/host/temperature_sdb'
- platform: mqtt
name: 'Dogecoin Price'
state_topic: 'mqtt_monitoring/crypto/doge_price'
unit_of_measurement: 'USD'
Example of mqtt_monitoring.sh:
temperature_sda=$(smartctl -A /dev/sda | grep Temperature | awk '{print $10}')
temperature_sdb=$(smartctl -A /dev/sdb | grep Temperature_Celsius | awk '{print $10}')
crypto=$(curl -s -X GET "https://api.coingecko.com/api/v3/simple/price?ids=dogecoin&vs_currencies=usd" -H "accept: application/json")
doge_price=$(echo $crypto | jq -r '.dogecoin.usd')
mosquitto_pub -t mqtt_monitoring/host/temperature_sda -m "$temperature_sda"
mosquitto_pub -t mqtt_monitoring/host/temperature_sdb -m "$temperature_sdb"
mosquitto_pub -t mqtt_monitoring/crypto/$doge_price -m "$doge_price"