psmqtt icon indicating copy to clipboard operation
psmqtt copied to clipboard

Enable autodiscovery in Home-Assistant

Open osos opened this issue 2 years ago • 11 comments

Would be nice with possibility to enable discovery features with Home Assistant.

Look at this project for inspiration: https://github.com/Sennevds/system_sensors

osos avatar Aug 07 '21 09:08 osos

Sorry, I'm not aware of autodiscovery feature of Home Assistant Could you please provide me with more information or at least point out where I can read about it?

eschava avatar Aug 10 '21 13:08 eschava

Sure:

  • https://www.home-assistant.io/docs/mqtt/discovery/
  • Example: https://github.com/Sennevds/system_sensors/blob/d416d3bcfc70a1a7f9b523db6322c02cb0f357bb/src/system_sensors.py#L347

osos avatar Aug 10 '21 14:08 osos

Hi, is there an update in sight? here is another example of a similar project.

  • https://mqtt-io.app/2.2.6/#/config/ha_discovery

TBobsin avatar Apr 09 '22 16:04 TBobsin

This would be great. I can see the psmqtt data via mqtt-explorer, but cannot easily get the values in Home Assistant. And I'd like to have the configuration in one place, being in the mqtt config files.

jan-gerard avatar Jun 19 '22 15:06 jan-gerard

I posted a message directly for the 'sensor' that reports as psmqtt/myserver/virtual_memory/percent (where my psmqtt.conf file contains mqtt_topic_prefix = 'psmqtt/' + socket.gethostname() + '/'). The topic of this 'persistent' message was: homeassistant/sensor/myserver_virtual_memory_percent/config and it contained { "name": "myServer: Virtual memory", "state_topic": "psmqtt/myserver/virtual_memory/percent", "icon": "mdi:memory", "unit_of_measurement": "%" } It's as easy as that, basically. HA automatically records the published values.

Now it would be great if psmqtt would, upon restart, send those mqtt messages for all the sensors it will be reporting afterwards.

jan-gerard avatar Jun 19 '22 16:06 jan-gerard

Hi @jan-gerard,

Can you please share how you integrated psmqtt into Home-Assistant or how you enabled auto discovery (e.g. the psmqtt config file or what you did specifically)?

Thanks

vanalmsick avatar Nov 17 '22 20:11 vanalmsick

I installed psmqtt (through aptitude, I guess, but this was a while ago). Then I edited the /etc/psmqtt.conf file as shown below:

import socket

mqtt_broker = '192.168.xxx.xxx'       # default: 'localhost', changed to my mqtt server
mqtt_port = 1883                # default: 1883
mqtt_clientid = 'psmqtt'
mqtt_username = 'xxxxx'
mqtt_password = 'xxxxxxxx'
mqtt_clean_session = False
mqtt_qos = 0
mqtt_retain = False
#mqtt_topic_prefix = 'psmqtt/'
mqtt_topic_prefix = 'psmqtt/' + socket.gethostname() + '/'
mqtt_request_topic = 'request'

schedule = {
    "every 1 minute" :    [
        "cpu_percent",
     ],
    "every 5 minutes" :    [
        "virtual_memory/percent",
        "sensors_temperatures/coretemp/0/current",
     ],
    "every 60 minutes"  : [
        "disk_usage/percent/|",  # slash replaced with vertical slash
        "disk_usage/percent/|home",
        "disk_usage/percent/|data",
     ],
    "every 3 hours" : {
        "boot_time/{{x|uptime}}": "uptime",
    }
}

This was part one. That makes psmqtt send mqtt messages at a regular interval. The next step was to make Home Assistant receive those messages. From an Arduino project, I knew it was possible to post a persistent message, that Home Assistant understands, and makes it record the corresponding sensor. Since psmqtt doesn't do that by itself, I created a shell script to announce the psmqtt topics when I restart the system psmqtt is running on: /usr/local/bin/psmqtt/psmqtt.sh.

#!/bin/bash
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_virtual_memory_percent/config -m '{ "name": "home_server: Virtual memory", "state_topic": "psmqtt/home_server/virtual_memory/percent", "icon": "mdi:memory", "unit_of_measurement": "%" }' -r
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_cpu_percent/config -m '{  "name": "home_server: CPU load",  "state_topic": "psmqtt/home_server/cpu_percent",  "icon": "mdi:cpu-64-bit",  "unit_of_measurement": "%"}' -r
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_disk_usage_root_percent/config -m '{  "name": "home_server: Disk Usage - /",  "state_topic": "psmqtt/home_server/disk_usage/percent/|",  "icon": "mdi:harddisk",  "unit_of_measurement": "%"}' -r
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_disk_usage_home_percent/config -m '{  "name": "home_server: Disk Usage - /home",  "state_topic": "psmqtt/home_server/disk_usage/percent/|home",  "icon": "mdi:harddisk",  "unit_of_measurement": "%"}' -r
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_disk_usage_data_percent/config -m '{  "name": "home_server: Disk Usage - /data",  "state_topic": "psmqtt/home_server/disk_usage/percent/|data",  "icon": "mdi:harddisk",  "unit_of_measurement": "%"}' -r
mosquitto_pub -h 192.168.1.xxx -p 1883 -u **** -P **** -t homeassistant/sensor/home_server_coretemp_0_current/config -m '{ "name": "home_server: Core temperature",  "state_topic": "psmqtt/home_server/sensors_temperatures/coretemp/0/current",  "icon": "mdi:thermometer-lines",  "unit_of_measurement": "°C"}' -r

It would be nice if psmqtt would take care or that whenever it was restarted, based on the lines in the config file.

jan-gerard avatar Nov 18 '22 19:11 jan-gerard