Shelly Detached Switch

Hello,

I am testing out a Shelly 1 and it works perfectly for dumb lights but I have some with smart bulbs in. I want to run the Shelly in detached mode.

How are people integrating Shelly 1 in detached mode so that when the light switch happen Home Assistant sees it so I can build an automation off it?

Many thanks

So, sorry it’s taken so long for someone to provide you an answer. I was actually Googling this and searching the forums and found this, but obviously no one had responded. I eventually figured it out, so here goes:

Using a Shelly in Detached Switch mode within Home Assistant to drive automations:

First, you need to have your Shelly configured and actually in Detached Switch mode. This is the mode where the Input (your wall switch) and the Relay (what actually turns your appliance on/off) are detached. This allows you to do things like provide constant power to an appliance (say because you put Smart Bulbs into the sockets) but want the switch on the wall to actually control the lights (or something totally unrelated like some automated blinds). Check your Shelly docs to configure this (it’s under the switch, settings, button type).

Secondly, this tutorial is going to have to make an assumption that you’re running MQTT on your Shelly (this is what I’m doing), as it gives you a lot of control over the Shelly itself. If you’re running Shelly for HASS or the built-in integration, you’ll have to look elsewhere, sorry.

UPDATE: See my post below if you’re using the native Shelly Integration, as it’s easy then.

Finally, one last assumption is that your Shelly is WORKING in Home Assistant with MQTT already. If it’s not working in MQTT and you’ve done your definitions for switches and sensors, then this isn’t going to help. Go back and get that working, then come here and it’ll make a lot more sense.

On the Shelly, there are two different topics that will be leveraged:
shellies/shellyswitch25-123456/input
shellies/shellyswitch25-123456/relay

The input topic is tied to the state of the wall switch/button (on/off).
The relay topic is tied to the state of the actual appliance (on/off).

So, in my mind, you want to create a SENSOR for the input and a SWITCH for the relay in HA.
You can’t physically control the input from HA, you can only report on what it is (sensor), but you CAN control the state of the relay, so it should be a switch.

So, here’s how you define each of them in HA, in your appropriate .yaml file:

The Switch:

- platform: mqtt
  unique_id: shelly_light
  name: "Shelly Light"
  icon: mdi:lightbulb
  state_topic: "shellies/shellyswitch25-123456/relay/1"
  command_topic: "shellies/shellyswitch25-123456/relay/1/command"
  payload_on: "on"
  payload_off: "off"
  retain: false

Now the Binary Sensor:

- platform: mqtt
  unique_id: shelly_light_switch
  name: "Shelly Light Switch"
  payload_on: "1"
  payload_off: "0"
  device_class: "power"
  state_topic: "shellies/shellyswitch-123456/input/1"

Now that you have a binary sensor defined, you can trigger automations off of it.

You can find other device_class types here: https://www.home-assistant.io/integrations/binary_sensor/

6 Likes

Quick update…

I took a look and if you’re using the native Shelly Integration, then it will automatically create a binary_sensor that ends in “_input” to represent the input topic. You’d use that to trigger your automation in a detached switch mode.

3 Likes

Hi,
I’m using this automation to on/off my smart light (philips hue) with a smart wall switch (shelly1) with ShellyForHass component :

- alias: 'what you want'
  trigger:
    - platform: event
      event_type: shelly_switch_click
      event_data:
        entity_id: binary_sensor.shelly_shsw_1_ID_switch
  action:
    - service: homeassistant.toggle
      entity_id: light.XXXXXX

So if i can put on my wall switch physically it’s turn on or off the light

You sure? The documentation doesn’t mention this at all, but only that detached button types are only fired with events. There isn’t a binary sensor created automatically with native shelly integration.

EDIT: It’s actually disabled by default, which seems a bit odd. After enabled it, it works as expected! Thanks. I would have never known to even look since the documentation doesn’t even mention it.

Why go through all this bother?

Don’t wire in the Shelly relay, just power and the switch. Short the switch wires to the smart light so it is always powered. Automate control of the light with the switch.

Because I’d rather use the natively available sensors? If I short the switch and make it permanently wired, and then I replace the fixture itself, I’d have to unwire the wall switch. Also I can, in software, return the light switch to operating normally if I sell my house. Not sure why you’d bother changing how the switch works when you don’t have to…

3 Likes

Yea, there are two different ways to integrate the Shellys. Using the native integration and manually via MQTT. I found the input sensor via MQTT as that’s how I have all of my Shellys configured.

Added bonus, if you do it this way you can also just have the Input on the switch power something completely unrelated.

1 Like

If you’re going to remove the Shelly before selling it’s just as easy to replace the wires into the switch.

How often do you replace your light fittings?

You know you should tag out the circuit breaker when doing that anyway?

I’m not sure what else inside my wall cavity I could power…

Guess I’m not sure why you don’t want to use the natively supported capabilities in the Shelly. To each their own: ¯_(ツ)_/¯

Just ease of setup.

Do you have any automation example using mqtt?

Have tried implementing this and created an automation to act as a light switch, but every time I toggle to ‘off’ the lights turn off and then come back on instantly… any ideas?

And then the shelly being triggered drops off the network… does not come back until a hard reset of the mains happens. Its almost as if it is getting overwhelmed and just dies.

Yes, I had that. Solution was to flash it with ESPHome using a cheap 3.3V FTDI module. I’ve done this with two Shelly 1s which were unusable with the original firmware and are now absolutely rock solid.

2 Likes

MQTT itself isn’t special to create automations from, assuming you’ve used it to create switches as I showed back in December 2020 on this thread. Once you have the switch, you can automate it as you would any other switch.

You can create automations directly from MQTT without ever creating the switches or binary_sensor that I mentioned, you’d do that with the “MQTT Trigger” in an automation as shown here.

If you can be more specific around the type of automation you’re trying to create, I can whip one up.

Is the light itself actually going on and off?

I’ve had this issue where in the UI I tell it to turn it, the UI shows it going on, then it immediately turns off. This usually means that the Shelly is not attached to the network and cannot be reached by HA (which aligns with your other post below this one).

I’ve only had it happen with one switch, I dropped mains power to the switch and it came back. It only happened once, but I suspect weak WiFi signal as the culprit. I haven’t gone down the path of pulling the switch from the wall and re-flashing it, as it’s not been a chronic condition for me. However, it was running an older firmware than the others, so once I got it back I updated the firmware and the situation hasn’t repeated.

Fixed by adding a neutral to the switch - it was the Shelly 1L that should be able to work without it but needed it anyway, if I had known that I would have got the 1PM instead.

i’ve followed your guide and i’ve been able to create a sensor that works in lovelace (and is almost instant!) so huge thanks.

i had a question however it was only due to me being bad at typing :smiley:
i take advantage of my experience to share with the community hoping to help somebody noober then me

configuration.yaml (note: input 0 because it’s the first switch of a shelly 2.5, this may vary from model to model)

binary_sensor:
  - platform: mqtt
    unique_id: "shellymqttprovswitch"
    name: "shellymqttprovswitch"
    payload_on: "1"
    payload_off: "0"
    device_class: "power"
    state_topic: "shellies/shellyswitch25-xxx/input/0"

Automation configuration. i have one for on and one for off, might try to manually build a toggle by adding both conditions in “or”

- id: '1622570123939'
  alias: Camera_Luci_-_accendi_camera_tutto
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.shellymqttprovswitch
    to: 'on'
    id: Shellypassaadon
    from: 'off'
  condition: []
  action:
  - service: light.turn_on
    target:
      area_id: d8d73758539d48cd827f71ef6d9ee645
    data: {}
  mode: single

Is the automation just to toggle lights? if so, there is a simpler way of doing it…