Remote RPI system monitor

Hi,

I know there are already several topics about this but thought someone maybe find my approach interesting.

I’ve created a simple python script that runs every 60 seconds and sends several system data over MQTT. It uses the MQTT Discovery for Home Assistant so you don’t need to configure anything in Home Assistant if you have discovery enabled for MQTT
Here’s the repo
system_sensors
Currently you need to manualy install the following python3 libraries:

  • psutil
  • paho.mqtt
  • pytz

Here’s how I’ve set it up in Lovelace:

26 Likes

Excellent. Thanks for this.

I’m using glances to monitor my remote raspis at the moment but the volume of data glances sends is excessive for what I am actually monitoring.

I wanted to make something simple and something I had control over.
It’s quiet easy to add extra sensors the only thing you have to add is an extra publish for the config and add the variable to the publish of the sensor values.

The only thing I hoped was that I could publish only one config per device instead of create a config per sensor

You’ve got nearly everything I’m monitoring, just missing a swap use % sensor.

I think this is what I want:

def get_swap_usage():
    return str(psutil.swap_memory().percent)

And add it to the mqtt topic of course.

I’m monitoring 3 additional boards to HassOS Pi: one running OpenMediaVault, one MotionEyeOS and one Raspbian and I tried several monitoring options

Some OS like MotionEyeOS is limited so it’s impossible to install something there

Universal monitoring way I used for a while is similar to this but with .sh scripts and HomeAssistant API, this way does not require installing additional packages on devices but it requires setting up the monitoring .sh scripts.

Now I’m using SSH Generic Sensor from HACS and it does all the work from HassOS without doing anything on devices just configuring this component, for exaple here is temp monitor on OMV:

  - platform: ssh
    host: IP
    unit_of_measurement: "°C"
    name: OMV Temp
    username: username
    password: password
    command: cat /sys/class/thermal/thermal_zone0/temp
    value_template: >-
      {%- set line = value.split("\r\n") -%}
      {{ (line[1]|int / 1000) | round(1) }}
2 Likes

That’s a really nice way to do the logging!
For me the way I did it was also to understand and learn mqtt discovery.
I have a Matrix Creator and want to send the sensor data to Hass. So first I thougt lets start with something easy like this!

Maybe I will upload the script to a repository instead so you can make a pull request!
Then I also can add a requirements file to easy install the deps

1 Like

Edited the first post and created the repo

need some help on how to autostart the script please.

What’s the problem? Did you look at the repository? There I explain how you create the service

Sorry, forgot a [ in the service.

Great job.

1 Like

the script works correctly when i launch it from the shell command, but nothing append if I start from sysemd following your instruction.

look at the log found this:
ModuleNotFoundError: No module named 'paho'
but pahomqtt it’s correctly installed.

You have 2 options:

  • Install the requirements with sudo
  • Let the service run as a user
    I have updated the service script in the readme to include the user part

installing requirements with sudo solved.
thanks.

This looks awesome, great job! :grin: will try it out on my rpi running zigbee2mqtt

I’m trying to set this up but am getting the following error at step 4:

[email protected]_mqtt_bridge_1:~/system_sensors $ python /home/pi/system_sensors/src/system_sensors.py
  File "/home/pi/system_sensors/src/system_sensors.py", line 49
    def utc_from_timestamp(timestamp: float) -> dt.datetime:

SyntaxError: invalid syntax

This is my configuration in the file:

UTC = pytz.utc
DEFAULT_TIME_ZONE = timezone('Australia/Hobart')#Local Time zone
broker_url = "10.1.1.100"#MQTT server IP
broker_port = 1883 #MQTT server port
client = mqtt.Client()
client.username_pw_set("mqtt_user", "redacted") #Username and pass if configured otherwise you should comment out this
deviceName = "gpio_bridge_1" #Name off your PI
SYSFILE = '/sys/devices/platform/soc/soc:firmware/get_throttled'
WAIT_TIME_SECONDS = 60

EDIT: actually I think it’s step 3 that failed:

[email protected]_mqtt_bridge_1:~/system_sensors $ pip install -r requirements.txt
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: paho-mqtt==1.4.0 in /usr/local/lib/python2.7/dist-packages (from -r requirements.txt (line 1)) (1.4.0)
Collecting psutil==5.6.3 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/1c/ca/5b8c1fe032a458c2c4bcbe509d1401dca9dda35c7fc46b36bb81c2834740/psutil-5.6.3.tar.gz (435kB)
     |████████████████████████████████| 440kB 2.3MB/s 
Collecting pytz==2019.2 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/87/76/46d697698a143e05f77bec5a526bf4e56a0be61d63425b68f4ba553b51f2/pytz-2019.2-py2.py3-none-any.whl (508kB)
     |████████████████████████████████| 512kB 5.2MB/s 
Building wheels for collected packages: psutil
  Building wheel for psutil (setup.py) ... done
  Stored in directory: /home/pi/.cache/pip/wheels/90/7e/74/bb640d77775e6b6a78bcc3120f9fea4d2a28b2706de1cff37d
Successfully built psutil
ERROR: influxdb 5.2.2 has requirement requests>=2.17.0, but you'll have requests 2.12.4 which is incompatible.
Installing collected packages: psutil, pytz
  Found existing installation: psutil 5.6.1
    Uninstalling psutil-5.6.1:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/psutil-5.6.1.dist-info/DESCRIPTION.rst'
Consider using the `--user` option or check the permissions.

U are using python 2.7 you need to use python3

I just worked that out and am seeing the data. :slight_smile: Thanks for this.

Perhaps you should update the instructions for total newbs like me?

3. pip3 install -r requirements.txt
4. python3 system_sensors.py

Will do. First I’m going to further implement the mqtt discovery so all the metrics will be seen as one device!
You always can make a pull request to update the readme.md