Monitor uptime of other devices on network?

Hi! Is there a way to monitor the uptime of other linux servers on my network?

I do it with a little bash script on the server that runs in crontab and sends the values to mqtt.

#!/bin/bash
MQTT='192.168.1.67'

## Uptime
TOPIC="home/sensors/$(hostname)/uptime"
PAYLOAD=$(cat /proc/uptime | awk '{printf "%0.f", $1}')
/usr/bin/mosquitto_pub -t "$TOPIC" -m "$PAYLOAD" -h "$MQTT"

Then, in HA, a mqtt sensor and for a nicer look a template sensor.

sensor:
  - platform: mqtt
    name: bbb_ha_gw01_uptime
    state_topic: 'home/sensors/bbb-ha-gw01/uptime'
    unit_of_measurement: 's'

  - platform: template
    sensors:
      bbb_uptime_template:
        value_template: >-
          {% set uptime = (states.sensor.bbb_ha_gw01_uptime.state | float) %}
          {% set seconds = uptime % 60 %}
          {% set minutes = ((uptime % 3600) / 60) | int %}
          {% set hours = ((uptime % 86400) / 3600) | int %}
          {% set days = (uptime / 86400) | int %}
          {%- if days > 0 -%}
            {%- if days == 1 -%}
              1 day
            {%- else -%}
              {{ days }} days
            {%- endif -%}
            {{ ', ' }}
          {%- endif -%}
          {{ '%02d' % hours }}:{{ '%02d' % minutes }}

Auswahl_425

I just use a ping sensor.

1 Like

thanks for your suggestion! I got the shell script and sensor updating in HA… I must not understand how mqttt works. my sensors show ‘unknown’ result. Any suggestions?

Screen Shot 2021-01-22 at 4.24.01 PM

Do you have an MQTT broker running?

This, and does the script run in a terminal?
The mosquitto-clients need to be installed on the server.

Yes - I have a broker running on my HA box & ` mosquitto-clients’ on my servers running the minecraft servers.

I can’t seem to find an error msg in my logs either. Any ideas for what I should specifically looking for there? I have another item using MQTT successfully.

my mosquitto broker settings:

logins: []
anonymous: true
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

configuration.yaml:

  - platform: mqtt
    name: monster_uptime
    state_topic: 'home/sensors/monster/uptime'
    unit_of_measurement: 's'
  - platform: template
    sensors:
      bbb_uptime_template:
        value_template: >-
          {% set uptime = (states.sensor.monster_uptime.state | float) %}
          {% set seconds = uptime % 60 %}
          {% set minutes = ((uptime % 3600) / 60) | int %}
          {% set hours = ((uptime % 86400) / 3600) | int %}
          {% set days = (uptime / 86400) | int %}
          {%- if days > 0 -%}
            {%- if days == 1 -%}
              1 day
            {%- else -%}
              {{ days }} days
            {%- endif -%}
            {{ ', ' }}
          {%- endif -%}
          {{ '%02d' % hours }}:{{ '%02d' % minutes }}
  - platform: mqtt
    name: melon_uptime
    state_topic: 'home/sensors/melon/uptime'
    unit_of_measurement: 's'
  - platform: template
    sensors:
      bbb_uptime_template:
        value_template: >-
          {% set uptime = (states.sensor.melon_uptime.state | float) %}
          {% set seconds = uptime % 60 %}
          {% set minutes = ((uptime % 3600) / 60) | int %}
          {% set hours = ((uptime % 86400) / 3600) | int %}
          {% set days = (uptime / 86400) | int %}
          {%- if days > 0 -%}
            {%- if days == 1 -%}
              1 day
            {%- else -%}
              {{ days }} days
            {%- endif -%}
            {{ ', ' }}
          {%- endif -%}
          {{ '%02d' % hours }}:{{ '%02d' % minutes }}

Go to Configuration -> Integrations -> MQTT -> Configure and listen to one of the topics.


Now you can publish something to that topic.
If that works, try to publish something from you servers.

mosquitto_pub -h <mqtt_server_ip> -t home/sensors/monster/uptime -m "something"

Eureka ! thanks, that worked.

However, it seems like the ‘listen to a topic’ disappears after a reboot?

That’s only for testing.
Does it work if you publish from your servers?

Yes, if I run the script on the server, the uptime populates on the HA.
Do I need to specify HA to listen to MQTT topics somehow?

This should be done by the mqtt addon in HA.
Alternatively:

Your template sensors have both the same name, also you should write the templates one after another (not sure it that matters).
Try:

sensor:
  - platform: mqtt
    name: monster_uptime
    ....
  - platform: mqtt
    name: melon_uptime
    ...

  - platform: template
    sensors:
      monster_uptime_template: # <- this was wrong
        value_template: >-
          ...
      melon_uptime_template: # <- this was wrong
        value_template: >-
          ...

Can you share the code you use to have the output you shared?

This is from my binary_sensor.yaml:
It is very simple. I assign static IPs to almost everything on my LAN.

### Ping for connected to GKAL Lan status
# Camera
- platform: ping
  name: ping_Camera-Backyard
  host: 10.74.1.189
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_Camera-Drive
  host: 10.74.1.244
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_Camera-North
  host: 10.74.1.94
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_Camera-Treehouse
  host: 10.74.1.118
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_Camera-North_shelly
  host: 10.74.1.201
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_Camera-Drive_shelly
  host: 10.74.1.192
  count: 5
  scan_interval: 300
# Rpi
- platform: ping
  name: ping_RpiGen
  host: 10.74.1.144
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_RpiLight
  host: 10.74.1.199
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_RpiSalt
  host: 10.74.1.169
  count: 5
  scan_interval: 300
- platform: ping
  name: ping_RpiBed
  host: 10.74.1.38
  count: 5
  scan_interval: 300

Here is the display:

type: 'custom:auto-entities'
sort:
  method: name
  numeric: true
card:
  type: entities
  title: LAN
  state_color: true
unique: true
filter:
  include:
    - entity_id: '*ping*'
      options:
        secondary_info: last-changed
    - entity_id: '*external_ip*'
      options:
        secondary_info: last-changed
  exclude:
    - name: '*Grocy*'

1 Like