Monitoring DietPi with Home Assistant Topic is solved

Suggestions for features and software you would like to see in DietPi, goes here.
Post Reply
DarrenHill
Posts: 54
Joined: Wed May 08, 2019 3:00 pm

Monitoring DietPi with Home Assistant

Post by DarrenHill »

Is there any way to monitor the current and available versions of DietPi using Home Assistant (running on a different Pi) using a JSON-rest API or suchlike? Or would that be something that could be added?

At the moment the only real way of knowing that an update is available is either the announcements here, or the banner notice on log-in.

It would be nice to be able to monitor my DietPi install with HA and have that notify when updates are available, plus perhaps some other useful monitoring information (memory usage, sd card space used/available etc, the usual kinda stuff).

Is there any way that can currently be done, or would it be something that could be added?
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Monitoring DietPi with Home Assistant

Post by Joulinar »

DietPi don't have any API that can be used to retrieve data from external. And there is no plan to have something like this implemented. At least I'm not aware of such a plan.

A tool to monitor a Linux system is telegraf agent. It can be used to report monitoring data into an InfluxDB. But not sure if this is something that could be used in HA.
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
DarrenHill
Posts: 54
Joined: Wed May 08, 2019 3:00 pm

Re: Monitoring DietPi with Home Assistant

Post by DarrenHill »

Thanks - I'll take a look at that.

I've found a couple of Python scripts which can be installed generically onto Pi-based systems which should allow transmission of general state information via MQTT, but of course it will lack the triggers for specific DietPi update information.

That was what I was most after, as my systems with DietPi are generally headless server systems (VPN and PiHole mostly) and so don't get directly connected to via SSH so often. Hence it's easy to miss update availability, other than what gets notified here.
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Monitoring DietPi with Home Assistant

Post by Joulinar »

actually, DietPi has a release cycle of 4 weeks since a couple of version. As well you could follow us on Twitter or Facebook. (if you use it).

Basically you could write your own script to extract release information. There was a user inside this forum who created his own script to display these information inside HA viewtopic.php?f=9&t=5935
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
DarrenHill
Posts: 54
Joined: Wed May 08, 2019 3:00 pm

Re: Monitoring DietPi with Home Assistant

Post by DarrenHill »

I don't use either, although I do pop in here fairly often.

Anyway I've got a script up and running giving general feedback to HA (this one with its associated LoveLace card).

Now just need to hack it to include the DietPi information you mention. Should be a nice little project, with some useful info in that thread you linked.
DarrenHill
Posts: 54
Joined: Wed May 08, 2019 3:00 pm

Re: Monitoring DietPi with Home Assistant

Post by DarrenHill »

To follow-up, put together a quick little Python 3 script to monitor the versions (and available apt updates) and then output them via MQTT for HA to read and act on.

There isn't much by way of checking in the script for connections etc, but it seems to work ok (it detected the 7.4.2 to 7.5.2 update, but as I haven't got any pending apt updates that file is absent to confirm that works). I'm sure it can be written better, cleaner and safer, but for now it's doing the job I need :)

MQTT parameters will of course need to be set up depending on local set-up and requirements.

Code: Select all

#!/usr/bin/env python3

import paho.mqtt.client as mqtt
import json
from time import sleep

MQTT_SERVER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_TOPIC = "dietpi"
MQTT_CONN = "connections"
MQTT_USER = ""
MQTT_PASS = ""

installed_file = "/boot/dietpi/.version"
update_file = "/run/dietpi/.update_available"
apt_file = "/run/dietpi/.apt_updates"

def on_connect(client, userdata, flags, rc):
    ret = client.publish(MQTT_CONN,"Connected", qos=0, retain = True)

client = mqtt.Client()
client.on_connect = on_connect

if MQTT_USER is not None and MQTT_PASS is not None:
    client.username_pw_set(username=MQTT_USER, password=MQTT_PASS)

client.connect(MQTT_SERVER, MQTT_PORT, 60)
client.loop_start()
sleep(1)

while True:
    try:
        f = open(installed_file, "r")
        current_core = f.readline()[22:].strip()
        current_sub = f.readline()[21:].strip()
        current_rc = f.readline()[20:].strip()
        current_version = current_core + "." + current_sub + "." + current_rc
        f.close()
    except:
        current_version = "0.0.0"

    try:
        f = open(update_file, "r")
        update_version = f.readline().strip()
        f.close()
        update_flag = True
    except:
        update_version = current_version
        update_flag = False

    try:
        f = open(apt_file, "r")
        apt_count = f.readline().strip()
        f.close()
    except:
        apt_count = 0

    data = {
        'current': current_version,
        'latest': update_version,
        'update': update_flag,
        'apt': apt_count
    }

    send_data = json.dumps(data)
    ret = client.publish(MQTT_TOPIC, send_data, qos=0, retain = True)

    sleep(3600)
I'm starting it via a systemd service.
Am I right in thinking that once set up and enabled, I can just add it in `dietpi-services` to have dietpi start and stop it when it needs to?
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Monitoring DietPi with Home Assistant

Post by Joulinar »

I'm not sure that there is a need for DietPi to control it. But you are free to add them.
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
Post Reply