weewx-mqtt icon indicating copy to clipboard operation
weewx-mqtt copied to clipboard

Add LWT to mqtt connection

Open ThomDietrich opened this issue 2 years ago • 8 comments

Fixes #21

For testing in Home Assistant:

mqtt:
  sensor:
    - name: weather_station_winddir
      unique_id: uniqueid__weather_winddir
      icon: mdi:wind-turbine
      state_topic: "weather/windDir"
      value_template: "{{ value | round(0) }}"
      availability_topic: "weather/lwt"
      unit_of_measurement: "°"

ThomDietrich avatar Sep 11 '22 20:09 ThomDietrich

@roe-dl would you mind testing this?

It's pretty straight forward and works as intended in my setup.

ThomDietrich avatar Sep 13 '22 08:09 ThomDietrich

I tested it.

For that I used a simple WeeWX installation including the driver to fetch the weather data from my weather station and the MQTT extension in patch-1 version.

  • Topic "lwt" is sent to the broker.
  • It becomes "online" when WeeWX is started.
  • It immediately becomes "offline" when WeeWX is stopped.
  • If I pull the plug of the PC that is running WeeWX (i.e. hard shutdown), the topic "lwt" remains "online" for some time, and then becomes "offline".
  • After reboot it becomes "online" again.

So I guess, it works as it should do.

roe-dl avatar Sep 17 '22 18:09 roe-dl

@roe-dl your test is way more detailed than my own. Everything sounds exactly as it should. Amazing, thank you. @matthewwall ready to merge.

the topic "lwt" remains "online" for some time, and then becomes "offline".

Fyi: This is MQTTs lwt feature at its best. When the client disconnects cleanly, "offline" is set by the broker right away. If the client dies off, the broker publishes "offline" after a certain timeout.

ThomDietrich avatar Sep 18 '22 12:09 ThomDietrich

One question: Why is the topic called "lwt"? I found it not really intuitive. What about "connection" or "server"? Together with the possible values "offline" and "online" those names would be self explaining, I guess.

roe-dl avatar Sep 18 '22 14:09 roe-dl

It's an established topic name used by many project. However, I agree with you. Alternatives are "availability" or "connection_status". I will go with the first

ThomDietrich avatar Sep 18 '22 18:09 ThomDietrich

@matthewwall ready to merge

ThomDietrich avatar Sep 28 '22 16:09 ThomDietrich

Added one improvement where the broker would eventually decide to mark as offline and the running weewx wouldn't care.

ThomDietrich avatar Oct 02 '22 09:10 ThomDietrich

So just to be clear, even though I have installed the autodiscovery variant of weewx-mqtt, I should declare this LWT in the configuration.yaml because otherwise I cannot "discover" anything. Am I doing right? Or i'm missing something?

juandelarocha avatar Jan 16 '23 21:01 juandelarocha