From 3013d01f4bd08eea87621f319c2db320862f60d9 Mon Sep 17 00:00:00 2001 From: leaskovski Date: Tue, 6 Apr 2021 20:22:11 +0100 Subject: [PATCH 1/8] Added config setting to enable MQTT discovery messages --- src/config.py.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config.py.example b/src/config.py.example index 7aac73a..4a22762 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -12,6 +12,9 @@ mqtt_topic_prefix = "rpi-MQTT-monitor" # If this is set to True the script will send just one message containing all values group_messages = True +# If this is set, then the script will send MQTT discovery messages meaning a config less setup in HA +discovery_messages = True + # Random delay in seconds before measuring the values # - this is used for de-synchronizing message if you run this script on many hosts, set this to 0 for no delay. # - if you want a fix delay you can remove the randarnge function and just set the needed delay. From b7f053a7899d706b0a2f872211cbe31d3796bc02 Mon Sep 17 00:00:00 2001 From: leaskovski Date: Tue, 6 Apr 2021 21:47:37 +0100 Subject: [PATCH 2/8] Started adding in MQTT discovery broadcasts --- src/rpi-cpu2mqtt.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index f772298..8741b0a 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -61,6 +61,19 @@ def check_uptime(): full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime" return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]) +def config_json(what_config): + data = { + "state_topic": "", + "icon": "", + "name": "", + "unique_id": "", + } + data["state_topic"] = config.mqtt_topic_prefix+"/"+hostname+"/"+what_config + data["unique_id"] = hostname+"_"+what_config + if what_config == "cpu_load": + data["icon"] = "mdi:speedometer" + data["name"] = hostname + " CPU Usage" + def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0): # connect to mqtt server client = paho.Client() @@ -69,6 +82,9 @@ def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sy # publish monitored values to MQTT if config.cpu_load: + if config.descovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"/cpuload/config", config_json('cpu_load'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1) time.sleep(config.sleep_time) if config.cpu_temp: From 428a189dc5d08f75ae0dd9072f9b66bce33866a6 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Apr 2021 22:59:11 +0100 Subject: [PATCH 3/8] Made some changes to fix individual broadcasts not working, also got the inital MQTT discovery implementation working --- src/rpi-cpu2mqtt.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 8741b0a..07b385c 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -67,23 +67,26 @@ def config_json(what_config): "icon": "", "name": "", "unique_id": "", + "unit_of_measurement": "", } data["state_topic"] = config.mqtt_topic_prefix+"/"+hostname+"/"+what_config data["unique_id"] = hostname+"_"+what_config - if what_config == "cpu_load": + if what_config == "cpuload": data["icon"] = "mdi:speedometer" data["name"] = hostname + " CPU Usage" + data["unit_of_measurement"] = "%" + return json.dumps(data) def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0): # connect to mqtt server client = paho.Client() client.username_pw_set(config.mqtt_user, config.mqtt_password) - client.connect(config.mqtt_host, config.mqtt_port) + client.connect(config.mqtt_host, int(config.mqtt_port)) # publish monitored values to MQTT if config.cpu_load: - if config.descovery_messages: - client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"/cpuload/config", config_json('cpu_load'), qos=0) + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cpuload/config", config_json('cpuload'), qos=0) time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1) time.sleep(config.sleep_time) @@ -105,7 +108,7 @@ def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sy if config.sys_clock_speed: client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1) time.sleep(config.sleep_time) - if config.uptime_days: + if config.uptime: client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1) time.sleep(config.sleep_time) # disconect from mqtt server From a4fb6eba32f7d46f549eb32fd4ec42d6111f445d Mon Sep 17 00:00:00 2001 From: leaskovski Date: Wed, 7 Apr 2021 23:16:07 +0100 Subject: [PATCH 4/8] Added in other discovery broadcasts --- src/rpi-cpu2mqtt.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 07b385c..554bb31 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -75,6 +75,34 @@ def config_json(what_config): data["icon"] = "mdi:speedometer" data["name"] = hostname + " CPU Usage" data["unit_of_measurement"] = "%" + elsif what_config == "cputemp": + data["icon"] = "hass:thermometer" + data["name"] = hostname + " CPU Temperature" + data["unit_of_measurement"] = "°C" + elsif what_config == "diskusage": + data["icon"] = "mdi:harddisk" + data["name"] = hostname + " Disk Usage" + data["unit_of_measurement"] = "%" + elsif what_config == "voltage": + data["icon"] = "mdi:speedometer" + data["name"] = hostname + " CPU Voltage" + data["unit_of_measurement"] = "V" + elsif what_config == "swap": + data["icon"] = "mdi:harddisk" + data["name"] = hostname + " Disk Swap" + data["unit_of_measurement"] = "%" + elsif what_config == "memory": + data["icon"] = "mdi:memory" + data["name"] = hostname + " Memory Usage" + data["unit_of_measurement"] = "%" + elsif what_config == "sys_clock_speed": + data["icon"] = "mdi:speedometer" + data["name"] = hostname + " CPU Clock Speed" + data["unit_of_measurement"] = "MHz" + elsif what_config == "uptime_days": + data["icon"] = "mdi:timer-outline" + data["name"] = hostname + " Uptime" + data["unit_of_measurement"] = "s" return json.dumps(data) def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0): @@ -91,24 +119,45 @@ def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sy client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1) time.sleep(config.sleep_time) if config.cpu_temp: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cputemp/config", config_json('cputemp'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1) time.sleep(config.sleep_time) if config.used_space: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_diskusage/config", config_json('diskusage'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1) time.sleep(config.sleep_time) if config.voltage: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_voltage/config", config_json('voltage'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1) time.sleep(config.sleep_time) if config.swap: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_swap/config", config_json('swap'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1) time.sleep(config.sleep_time) if config.memory: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_memory/config", config_json('memory'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1) time.sleep(config.sleep_time) if config.sys_clock_speed: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_sys_clock_speed/config", config_json('sys_clock_speed'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1) time.sleep(config.sleep_time) if config.uptime: + if config.discovery_messages: + client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_uptime_days/config", config_json('uptime_days'), qos=0) + time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1) time.sleep(config.sleep_time) # disconect from mqtt server From aeb4be4edb33cb60cf3691fc942565ebb3383598 Mon Sep 17 00:00:00 2001 From: leaskovski Date: Wed, 7 Apr 2021 23:23:50 +0100 Subject: [PATCH 5/8] Fixed my bad python if code --- src/rpi-cpu2mqtt.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 554bb31..2f3f234 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -75,34 +75,37 @@ def config_json(what_config): data["icon"] = "mdi:speedometer" data["name"] = hostname + " CPU Usage" data["unit_of_measurement"] = "%" - elsif what_config == "cputemp": + elif what_config == "cputemp": data["icon"] = "hass:thermometer" data["name"] = hostname + " CPU Temperature" data["unit_of_measurement"] = "°C" - elsif what_config == "diskusage": + elif what_config == "diskusage": data["icon"] = "mdi:harddisk" data["name"] = hostname + " Disk Usage" data["unit_of_measurement"] = "%" - elsif what_config == "voltage": + elif what_config == "voltage": data["icon"] = "mdi:speedometer" data["name"] = hostname + " CPU Voltage" data["unit_of_measurement"] = "V" - elsif what_config == "swap": + elif what_config == "swap": data["icon"] = "mdi:harddisk" data["name"] = hostname + " Disk Swap" data["unit_of_measurement"] = "%" - elsif what_config == "memory": + elif what_config == "memory": data["icon"] = "mdi:memory" data["name"] = hostname + " Memory Usage" data["unit_of_measurement"] = "%" - elsif what_config == "sys_clock_speed": + elif what_config == "sys_clock_speed": data["icon"] = "mdi:speedometer" data["name"] = hostname + " CPU Clock Speed" data["unit_of_measurement"] = "MHz" - elsif what_config == "uptime_days": + elif what_config == "uptime_days": data["icon"] = "mdi:timer-outline" data["name"] = hostname + " Uptime" data["unit_of_measurement"] = "s" + else: + return "" + # Return our built discovery config return json.dumps(data) def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0): From 0c1ab5404845069abcf235b2d7b3eb7ee2a7a636 Mon Sep 17 00:00:00 2001 From: leaskovski Date: Thu, 8 Apr 2021 09:05:44 +0100 Subject: [PATCH 6/8] Updated unit of measure for uptime to days --- src/rpi-cpu2mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 2f3f234..1639172 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -102,7 +102,7 @@ def config_json(what_config): elif what_config == "uptime_days": data["icon"] = "mdi:timer-outline" data["name"] = hostname + " Uptime" - data["unit_of_measurement"] = "s" + data["unit_of_measurement"] = "days" else: return "" # Return our built discovery config From 4e4f4bc7c2a9706ce53fe765da41c8f303235de4 Mon Sep 17 00:00:00 2001 From: leaskovski Date: Fri, 9 Apr 2021 10:42:24 +0100 Subject: [PATCH 7/8] Updated Readme to define the new discovery_messages setting. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bebf053..7b28f4e 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ This is the default configuration: ``` random_delay = randrange(30) +discovery_messages = False group_messages = True sleep_time = 0.5 cpu_load = True @@ -63,6 +64,8 @@ memory = False uptime = True ``` +If the ```discovery_messages``` is set to true, the script will send MQTT Discovery config messages which allows Home Assistant to automatically add the sensors without having to define them in configuration. Note, this setting is only available for when ```group_messages``` is set to False. + If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format. The group message looks like this: ``` @@ -85,7 +88,7 @@ Create a cron entry like this (you might need to update the path in the cron ent Once you installed the script on your raspberry you need to create some sensors in home assistant. - +If you are using ```discovery_messages```, then this step is not required as the sensors are auto discovered by Home Assistant and added. This is the sensors configuration if ```group_messages = True``` assuming your sensors are separated in ```sensors.yaml``` file. ```yaml From 368beafc6ff30ccbf02cd60c37ac0d9ca1cf40fc Mon Sep 17 00:00:00 2001 From: leaskovski Date: Fri, 9 Apr 2021 10:43:40 +0100 Subject: [PATCH 8/8] Changed the default setting of discovery_messages --- src/config.py.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.py.example b/src/config.py.example index 4a22762..451cc5f 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -12,8 +12,8 @@ mqtt_topic_prefix = "rpi-MQTT-monitor" # If this is set to True the script will send just one message containing all values group_messages = True -# If this is set, then the script will send MQTT discovery messages meaning a config less setup in HA -discovery_messages = True +# If this is set, then the script will send MQTT discovery messages meaning a config less setup in HA. Only works for group_messages being False +discovery_messages = False # Random delay in seconds before measuring the values # - this is used for de-synchronizing message if you run this script on many hosts, set this to 0 for no delay.