From 69dafdbae51e339b39d0d8583cef70e23321a54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Wed, 10 Dec 2025 22:15:00 +0000 Subject: [PATCH 1/4] feat(MQTT): include session_present and reason parameters in connection callbacks --- content/components/mqtt.md | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/content/components/mqtt.md b/content/components/mqtt.md index cf8fd97a39..5451695dbf 100644 --- a/content/components/mqtt.md +++ b/content/components/mqtt.md @@ -513,21 +513,36 @@ command_retain: false ## Triggers -{{< anchor "mqtt-on_connect_disconnect" >}} +{{< anchor "mqtt-on_connect" >}} -### `on_connect` / `on_disconnect` Trigger +### `on_connect` Trigger -This trigger is activated when a connection to the MQTT broker is established or dropped. +This trigger is activated when a connection to the MQTT broker is established. To retrieve if the session is present, +use a [lambda](/automations/templates#config-lambda) template, it is available under the name `session_present` inside that lambda. ```yaml mqtt: # ... on_connect: - switch.turn_on: switch1 +``` + +{{< anchor "mqtt-on_disconnect" >}} + +### `on_disconnect` Trigger + +This trigger is activated when a connection to the MQTT broker is dropped. To retrieve the disconnect reason, +use a [lambda](/automations/templates#config-lambda) template, the reason is available under the name `reason` inside that lambda. + +```yaml +mqtt: + # ... on_disconnect: - switch.turn_off: switch1 ``` +### `on_connect` / `on_disconnect` Trigger + {{< anchor "mqtt-on_message" >}} ### `on_message` Trigger From 126791643f9d54fd5ffa0fb9029a48a047c1e7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Wed, 10 Dec 2025 22:22:54 +0000 Subject: [PATCH 2/4] Update content/components/mqtt.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/components/mqtt.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/content/components/mqtt.md b/content/components/mqtt.md index 5451695dbf..e93a776bd6 100644 --- a/content/components/mqtt.md +++ b/content/components/mqtt.md @@ -541,8 +541,6 @@ mqtt: - switch.turn_off: switch1 ``` -### `on_connect` / `on_disconnect` Trigger - {{< anchor "mqtt-on_message" >}} ### `on_message` Trigger From b88f594bd056714aa89d38f314d7150cfdb47314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Wed, 10 Dec 2025 22:23:08 +0000 Subject: [PATCH 3/4] Update content/components/mqtt.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/components/mqtt.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/content/components/mqtt.md b/content/components/mqtt.md index e93a776bd6..5e0ee3c064 100644 --- a/content/components/mqtt.md +++ b/content/components/mqtt.md @@ -525,6 +525,11 @@ mqtt: # ... on_connect: - switch.turn_on: switch1 + - lambda: |- + ESP_LOGI("mqtt", "Session present: %s", session_present ? "true" : "false"); + if (!session_present) { + // Do something if session is not present + } ``` {{< anchor "mqtt-on_disconnect" >}} From c6420706e9901cd53d19bbb9db46738263bea5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Wed, 10 Dec 2025 22:24:37 +0000 Subject: [PATCH 4/4] Update mqtt.md --- content/components/mqtt.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/components/mqtt.md b/content/components/mqtt.md index 5e0ee3c064..53f681d7d7 100644 --- a/content/components/mqtt.md +++ b/content/components/mqtt.md @@ -544,6 +544,8 @@ mqtt: # ... on_disconnect: - switch.turn_off: switch1 + - lambda: |- + ESP_LOGI("mqtt", "Disconnect reason: %d", reason); ``` {{< anchor "mqtt-on_message" >}}