Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable autodiscovery in Home-Assistant #21

Open
osos opened this issue Aug 7, 2021 · 11 comments
Open

Enable autodiscovery in Home-Assistant #21

osos opened this issue Aug 7, 2021 · 11 comments

Comments

@osos
Copy link
Contributor

osos commented Aug 7, 2021

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

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

@eschava
Copy link
Owner

eschava commented Aug 10, 2021

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?

@osos
Copy link
Contributor Author

osos commented Aug 10, 2021

@TBobsin
Copy link

TBobsin commented Apr 9, 2022

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

@jan-gerard
Copy link

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
Copy link

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.

@vanalmsick
Copy link

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

@jan-gerard
Copy link

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.

@f18m
Copy link
Collaborator

f18m commented Jun 3, 2024

if I have some time, I might be working on this, since I already did implement this feature (home assistant auto-discovery via MQTT) for another project of mine: https://github.com/f18m/rpi2home-assistant/

@eschava what do you think? would that (home assistant MQTT auto-discovery messages) be a welcome addition to the psmqtt project?

@eschava
Copy link
Owner

eschava commented Jun 3, 2024

@f18m Sure, I'd be very grateful!

@jasjeetsuri
Copy link

This would be so useful..

@f18m
Copy link
Collaborator

f18m commented Jun 27, 2024

I have recently completed MQTT discovery message implementation in my other open source project so I might take a look at adding to psmqtt but it will take some time...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants