Thanks a lot, this helped my out and is much more elegant!
I am just facing one more “issue”. I added the “Availability Topic”, but if my mqtt client (in my case an esp32) turns off without notification there is no change in the status of my thing in openhab. Is there a way to define a timeout when a thing is considered to be offline?
Currently I am programming an “ESP32 Dev Kit C V4 NodeMCU” by myself and I did not look into Last Will and Testament topic yet.
Would this still be send before power off?
Or is the last will stored in the MQTT Broker and the broker handles the timeout?
I am new to MQTT and this is my first project so not all things are fully clear to me yet.