Shelly 1 Relay as MQTT Switch - Online/Offline status?

Hi everyone,

I’m using mqtt to control a shelly 1 relay. I created a mqtt switch for this by doing the following :

mqtt:
  switch:
    - unique_id: Test
      name: "Test Relay"
      state_topic: "shellies/shelly1-xxx/relay/0"
      command_topic: "shellies/shelly1-xxx/relay/0/command"
      payload_on: "on"
      payload_off: "off"
      qos: 1
      retain: true
      payload_available: "true"

I’m able to toggle the switch and turn on/off the relay when there’s WiFi - no problems.

However, if and when the relay disconnects from my WiFi network - I’d like for the switch to become “Unavailable”. This way, users will know that the relay isn’t available/connected to WiFi.

Any way to make this happen?

Shelly documentation says “availability message on shellies/-/online with payload true;” - but I’m not really sure how to use this to show online/offline status.

Thanks a lot for your help!

you are missing availability topic and payload unavailable value

availability_topic: "shellies/shelly1-dining1/online"
payload_available: "true"
payload_not_available: "false"
1 Like

This is wonderful - it worked! Thank you.

One question though - it takes about a minute to show that the device is offline, which is fine, no complaints. However, within that minute (when the device shows as available but is actually not), when I toggle the switch, it immediately retracts to its previous position. Eg. if I toggle to “On” within that minute, it quickly toggles back to “Off” automatically. And when the relay is back online again, I expect it to remain “Off” - however, I’m noticing that it actually turns “On”. ie. it seems to follow the command that was sent to it within the duration of it being displayed as available when it actually wasn’t. I can confirm this because even when I don’t toggle during that 1 minute period, it maintains the last command that was sent to it while it was online, after coming back online.

Does this have anything to do with retain: true? I tried with retain: false and observed the same thing. I’d like for it to not follow the command that was sent to it within the duration of it being displayed as available when it actually wasn’t.

Thanks.

Probably.
Personally, I never use retain on HA side. Retain has to be used with caution because it might trigger unneeded behavior.

On the other hand, I have to retain enabled in Shelly configuration. I found this approach not causing any side-effects during 5 years of using it.
It applies to gen1 devices (like Shelly 1). gen2 and gen3 don’t have retain setting in config. Thus HA, after restart, should send an announce command to all shellies, to get their most recent status, But this is out of the scope of what you are asked.

You can shorten the time after which a dropped connection renders in the unavailable state. It’s to be set in the Shelly settings. By default, it’s 60 secs as you observed.

1 Like

I did not know that about the disconnection timer setting. I just lowered it to 5 secs. This should do it for me. Thank you very much for the info.