Git Product home page Git Product logo

mqtt-monitoring's Introduction

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" 

mqtt-monitoring's People

Contributors

alex00x6 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.