Mqtt switch started without reason

Hi all,
I’ve a problem with an mqtt switch that I’ve found it started without a reason, as visible by the screenshot , for the event of today at 06:12:03 :

that mqtt switch is configured as follow:

mqtt:
  switch:
    - unique_id: "riscaldamento_camera_matr"
      name: "Riscaldamento Camera Matr"
      state_topic: "shellies/riscaldamento/camera_matr/relay/0"
      command_topic: "shellies/riscaldamento/camera_matr/relay/0/command"
      availability:
        - topic: "shellies/riscaldamento/camera_matr/online"
          payload_available: "true"
      payload_on: "on"
      payload_off: "off"
      state_on: "on"
      state_off: "off"
      optimistic: false
      qos: 1
      retain: true

I know that into activity page it’s possible to see the automation or the user that triggers the change state, but who triggers the change state when nothing is presented into activity page ?
is it possible to have a better log there ?

how can I solve this issue that led me mad ?

thanks

Something outside HA.

In your case, if you have the Shelly app or some kind of voice control, start by checking there.

In most cases, retain is responsible for unexpected state changes. never set retain for command topic!

BTW If topic is already retained (I bet it is), you need to delete the topic manually. Changing config does not influence retained status of the already retained topic.

1 Like

I’ve no voice control nor Shelly app.
I manage Shelly devices v1 through mqtt templates since years (when HA didn’t use years in version) and it’s happening only recently like HA >2025.10

Then check the physical device itself for shorts or loose contacts. That’s the last remaining option which would create those log entries

Look in the MQTT device Activity, whether the device disappears from the network

Also check what he wrote @maxym

I’ve no mqtt device there

Which are the benefits of having devices there instead of only into code (my config yamls for shellies is unchanged since HA 0.106)?

Thanks.

I know about retained topic should be avoided, but I thought them could be useful in case of blackout, so when things power up again the retained topic restarts the heating in this case.
Am I wrong in this scenario? Can be managed differently?

In such case this error should appear often until I physically “fix” it, but in the last 24 hours it has not happened again.
Just for completeness, no one but me can touch that Shelly due to its placement :sweat_smile:

I’ve removed the retain flag from mqtt switch configuration and from mosquitto, let’s see if this fixes.

Thanks.

The point is that blackout is not the only scenario that triggers the usage of the retained topic. It might be a network hiccup, HA restart, or even a reload of manually configured MQTT entities. Every time the mqtt client connects to MQTT, it will retrieve content of such topic, regardless how old the value is.

Instead of this, just make an automation that evaluates the situation after a blackout (either after HA start or a particular device returning from an unavailable state).

Another option (simplified but suitable in most cases) is to use device settings. Shellies offer 3 options for device start: always turn on, always turn off, and activate the last state active before power lost.

Yes, my shellies are already configured to restore the last state when powered up.

Thanks

I’ve remove the retain into configuration, but now happens that, when shellies are turned on and HA is restarted, HA does turn of the shellies, and this is unwanted, how an mqtt switch can survive the HA restart?

Thanks

Ok I’ve found myself the solution for this issue: the switch is linked to a generic thermostat that, during startup, notify different state between switch and itself , so turns off the switch.

So one more helper. When topics are not retained, when HA or MQTT restarts, there is no data HA can pick from MQTT. So entity states are unknown until the next entity change.
The solution is to ask all shelies to republish their states.

Here is automation doing that:

alias: Shellies Announce
description: Asks all Shelly devices to republish their states on HA or MQTT restart
triggers:
  - trigger: homeassistant
    event: start
  - trigger: event
    event_type: event_mqtt_reloaded
actions:
  - delay: 10
  - action: mqtt.publish
    data:
      payload: announce
      topic: shellies/command
mode: single

Thanks, I already have such automation :slightly_smiling_face: