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

[FR] Home Assistant Auto Discovery #4982

Open
TD-er opened this issue Feb 19, 2024 · 15 comments
Open

[FR] Home Assistant Auto Discovery #4982

TD-er opened this issue Feb 19, 2024 · 15 comments
Labels
Category: Controller Related to interaction with other platforms Category: UX Related to operating ESP-easy Type: Feature Request Add a completely new feature (e.g. controller/plugin)

Comments

@TD-er
Copy link
Member

TD-er commented Feb 19, 2024

Need to have auto discovery for Home Assistant.
So first need to collect some info.

@TD-er TD-er added Category: Controller Related to interaction with other platforms Type: Feature Request Add a completely new feature (e.g. controller/plugin) Category: UX Related to operating ESP-easy labels Feb 19, 2024
@sincze
Copy link

sincze commented Feb 19, 2024

That would be nice indeed, currently (as yesterday...) I used the RULES to PUBLISH the autodiscovery message so HA will pick it up (as well as Domoticz.

On MQTT#Connected do
  Publish homeassistant/binary_sensor/%sysname%_door/config,"{"name": "%sysname%_door","device_class": "door","state_topic": "ESP_Easy/%sysname%/sensor/door/status","unique_id": "%sysname%_door_%mac%","device": {"identifiers": ["%mac%_1"],"name": "Door_Sensor"}}"  
  Publish homeassistant/binary_sensor/%sysname%_contact/config,"{"name": "%sysname%_alarm","device_class": "safety","state_topic": "ESP_Easy/%sysname%/sensor/contact/status","unique_id":"%sysname%_alarm_%mac%","device": {"identifiers": ["%mac%_2"],"name": "Alarm_Sensor"}}"
  Publish espeasy-discovery/binary_sensor/%sysname%_door/config,"{"name": "%sysname%_door","device_class": "door","state_topic": "ESP_Easy/%sysname%/sensor/door/status","unique_id": "%sysname%_door_%mac%","device": {"identifiers": ["%mac%_1"],"name": "Door_Sensor"}}"  
  Publish espeasy-discovery/binary_sensor/%sysname%_contact/config,"{"name": "%sysname%_alarm","device_class": "safety","state_topic": "ESP_Easy/%sysname%/sensor/contact/status","unique_id":"%sysname%_alarm_%mac%","device": {"identifiers": ["%mac%_2"],"name": "Alarm_Sensor"}}"
endon

On Deur#State=1 do
  Publish ESP_Easy/%sysname%/sensor/door/status,ON
Endon

On Deur#State=0 do
  Publish ESP_Easy/%sysname%/sensor/door/status,OFF
Endon

On Alarm#State=1 do
  Publish ESP_Easy/%sysname%/sensor/contact/status,ON
Endon

On Alarm#State=0 do
  Publish ESP_Easy/%sysname%/sensor/contact/status,OFF
Endon
image

@TD-er
Copy link
Member Author

TD-er commented Feb 19, 2024

So this would really benefit from a specific publish command to set the retained state of those messages.

@sincze
Copy link

sincze commented Feb 19, 2024

So this would really benefit from a specific publish command to set the retained state of those messages.

I think I get the point of retained.

  1. Option 1, Publish you config on each message (ChatGPT came up with that idea. (I offered ChatGPT to do this on MQTT Connect and he agreed, or on startup of the ESP.
    On System#Boot do
  2. Option 2, Send it to a topic hand have Node-Red forward the topic with retain flag on.
image
  1. Use automations in HA to scan for the device topic and do the whole autodiscovery in there, or in an external YAML (I don't like this but in can be done).
  2. Ideally ... look at the developer of ESPEasy for having this automated ;-)

@TD-er
Copy link
Member Author

TD-er commented Feb 19, 2024

I think I'll go for option 4 as that would be the least amount of work for me.... oh wait.... ;)

@sincze
Copy link

sincze commented Feb 19, 2024

I think I'll go for option 4 as that would be the least amount of work for me.... oh wait.... ;)

Yes I can imagine, I've read through all the forum and noticed your tag attached to it so I thought, first try to solve myself .... ask questions later.

Than again if I could modify the PUBLISH so it sets the RETAIN flag you are in business as well. Downside is figuring out all the device templates ;-)

image

@TD-er
Copy link
Member Author

TD-er commented Feb 19, 2024

Well there is already an issue/feature request for publish_r (or whatever the name will be) to be able to pick which messages will be retained and which aren't.

Anyway the idea is to make the auto discovery a bit more like the name suggests... auto.
So even though this rules example is immensely helprul for me to get some idea of how to implement it, I would rather have it integrated in the MQTT controller itself, so users don't need to think about this.

So keep them examples coming, as it gives great insight in the process.

@sincze
Copy link

sincze commented Feb 19, 2024

Ok, one moment, So a lot of inspiration I found here.
https://github.com/enesbcs/shellyteacher4domo/blob/main/mqtt_templates.txt

I looked at: https://github.com/FlyingDomotic/domoticz-mqttmapper-plugin but that is a mapper not AD.

topic for switch device => tasmota12/switch/123456789ABC_relay/config

{
  "name": "switch1",
  "stat_t": "stat/switch1/POWER",
  "avty_t": "tele/switch1/LWT",
  "pl_avail": "Online",
  "pl_not_avail": "Offline",
  "cmd_t": "cmnd/switch1/POWER",
  "pl_off": "OFF",
  "pl_on": "ON",
  "val_tpl": "{{value_json.POWER}}",
  "uniq_id": "123456789ABC-Switch",
  "dev": {
    "ids": [
      "123456789ABC"
    ]
  }
}

topic for power device => tasmota12/sensor/123456789ABC_power/config

{
  "name": "switch1",
  "stat_t": "tele/switch1/SENSOR",
  "avty_t": "tele/switch1/LWT",
  "pl_avail": "Online",
  "pl_not_avail": "Offline",
  "enabled_by_default": false,
  "uniq_id": "123456789ABC-Power",
  "dev": {
    "ids": [
      "123456789ABC"
    ]
  },
  "unit_of_meas": "W",
  "dev_cla": "power",
  "frc_upd": true,
  "val_tpl": "{{value_json['ENERGY']['Power']}}"
}

topic for energy device => tasmota12/sensor/123456789ABC_total/config

{
  "name": "switch1",
  "stat_t": "tele/switch1/SENSOR",
  "avty_t": "tele/switch1/LWT",
  "pl_avail": "Online",
  "pl_not_avail": "Offline",
  "enabled_by_default": false,
  "uniq_id": "123456789ABC-Total",
  "dev": {
    "ids": [
      "123456789ABC"
    ]
  },
  "unit_of_meas": "kWh",
  "dev_cla": "energy",
  "stat_cla": "total_increasing",
  "frc_upd": true,
  "val_tpl": "{{value_json['ENERGY']['Total']}}"
}

source: https://www.domoticz.com/forum/viewtopic.php?p=312108#p312108

The process by itself looks like there needs to be a config topic for the device where it is defined and how it can be controlled (So HA or Domoticz should be able to find that topic, in my case espeasy-discover).

In addition the config (state_topic) will also tell you where the 'real messages' can be found in mqtt. That is why teacher4domo works. It tells Domoticz only once where the 'retained' messages are found and unless you install new devices, you do not need to re-run the teacherscript. At least that is how I see the world of autodiscovery at this moment.

@TD-er
Copy link
Member Author

TD-er commented Feb 19, 2024

Yep, the idea of using retained messages for discovery/config topics is that Home Assistant can find those again right after connecting to the MQTT broker again (e.g. after a reboot of HA)

@sincze
Copy link

sincze commented Feb 19, 2024

Yep, the idea of using retained messages for discovery/config topics is that Home Assistant can find those again right after connecting to the MQTT broker again (e.g. after a reboot of HA)

I started cheating a bit (in case mosquitto thinks about 'restarting') using the following config change as well: (is not an espeasy problem of course more general hygiene..)

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log


# Port to use for the default listener.
listener 1883
listener 9001
protocol websockets

log_timestamp true
allow_anonymous true

# Store info on disk ecery 30 sec
autosave_interval 30
autosave_on_changes false

At least storing every 30 sec the last messages.

@TD-er
Copy link
Member Author

TD-er commented Feb 19, 2024

Retained messages don't have a lot to do with Mosquitto MQTT broker restarting.
It is about sending the last message of a topic to anyone subscribing to that topic.
Thus if HomeAssistant reboots, then it subscribes to the topics on the MQTT broker and will receive all retained messages sent to those topics it subscribes to.

@tonhuisman
Copy link
Contributor

storing every 30 sec the last messages.

Not sure if that's a good plan when running your RPi on an SD-card... (default interval is 30 minutes, that seems often enough)

@sincze
Copy link

sincze commented Feb 19, 2024

storing every 30 sec the last messages.

Not sure if that's a good plan when running your RPi on an SD-card... (default interval is 30 minutes, that seems often enough)

Ok, Point taken ;-) still learning ;-0

I left SD-Cards back in 2015, fried them like hot chicken. MediaMarkt customer support even had a nickname for me.

So since then Domotica on SSD-ish media minimum with a backup for power outages..
image

@ArjenWols
Copy link

@TD-er
Dear developer,
Is there any idea regarding timelines for implementing Auto Discovery for MQTT?
Is there a way to accelerate this implementation? ( funding?)

Best regards
Arjen Wols

@TD-er
Copy link
Member Author

TD-er commented Nov 2, 2024

Have been away the last couple of days, so a late reply.

Currently I am working on doing a full re-work of the WiFi part of the code as I promised I would do as soon as the transition to ESP-IDF5.x was finished.
So that takes up all my time for ESPEasy right now.

After this I will have a look at the auto discovery.

I know there is also some auto discovery code implemented in C014, however I have no idea about how compatible this is with HomeAssistant.
Also the code of C014 may need "some" TLC as it is a lot of <copy><paste><paste><paste><paste><paste>, which makes it near impossible to maintain and thus I have postponed looking into that controller code for quite some time.

@ArjenWols
Copy link

Good morning TD-er,

Thanks the update and all the effort you pun into this.

Thanks for all the help.

Best regards
Arjen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Controller Related to interaction with other platforms Category: UX Related to operating ESP-easy Type: Feature Request Add a completely new feature (e.g. controller/plugin)
Projects
None yet
Development

No branches or pull requests

4 participants