Shelly 1L switch toggling itself when relay is off

Hi,

I flashed a Shelly 1L (without neutral) with ESPHome, and connected a regular two gang switch to it.
Both switch only do a toggle through the home assistant API in normal operations, with the relay being toggled only if HA is not connected :

binary_sensor:
  - platform: gpio
    name: Switch 1
    pin:
      number: GPIO4
    internal: True
    on_state:
      - homeassistant.service:
          service: switch.toggle
          data:
            entity_id: switch.light_bedroom_master_nightstand

  - platform: gpio
    name: Switch 2
    pin:
      number: GPIO14
    internal: True
    on_state:
      - if:
          condition:
            binary_sensor.is_on: ha_connected
          then:
            - if:
                condition:
                  switch.is_off: relayid
                then:
                  - switch.turn_on: relayid
                else:
                  - homeassistant.service:
                      service: light.toggle
                      data:
                        entity_id: light.light_bedroom_master_main
          else:
            - switch.toggle: relayid

Now as long as the relay is on, this works as expected.
But as soon as the relay is off for any reason it starts detecting ‘Switch 1’ toggling over and over, which toggles the nightstand light through home assistant. As soon as the relay is turned back on it starts behaving itself again.

Note that only Switch 1 is doing that, Switch 2 is fine meaning even if I shut HA off I can still operate the relay just fine through Switch 2, which fixes Switch 1 as soon as it goes on.

Any clue on why the state of the relay, which is a completely separate output not tied to these pins, would affect ‘Switch 1’ only ? Switch 2 is not affected.
I could have wired it wrong somehow but I can’t even think of what would lead to this weird behavior.

Thanks

I’ve just installed another Shelly 1L (brand new, different batch and all), this time with a ‘proper’ Shelly wall switch to go with it, and exactly the same issue. If the relay is off, it thinks the first switch is being pressed / toggled continuously which is problematic.

It seems that adding

    filters:
      - delayed_on_off: 50ms

to my switch fixes it. I’m not quite sure why that is, saw mentions online of these being a bit noisy, but it’s working on both so I suppose I’ll go with that for now.

I’d guess it’s a probably a voltage issue. Do you have the bypass connected and what type of bulb do you have in the night-stand?

The nightstand is not physically connected to that Shelly, the ceiling light (Philips Hue) is.
Without that filter when the relay is off it is making the nightstand blink (on, off, on, off …) through home assistant, not electrically, because it thinks the light switch on GPIO4 is being toggled.

I do have the bypass around the ceiling bulb, without it nothing works at all so it is there and connected. The delayed_on_off filter has been working really well since I put it on yesterday however, on both Shellys,

I have seen odd stuff with these in no-neutral configs and I think it’s because of voltages.
As they are missing a neutral, they don’t have a strong reference voltage (point of reference for dimming and control) and what I believe is happening is that there is a current change on the circuit which causes the voltage to fluctuate rapidly and the Shelly - which already has a weak point of reference - detects a state change and triggers an event. Adding in a 50mS delay may be allowing it to ride-out the voltage change and stabilise the system. That’s my perception of it all.

There was also mention somewhere on this forum of a potential problem with back-EMF from the relay affecting some components (maybe WiFi?), but I didn’t read into any theory behind that yet.

One other test to make would be to pull out the Hue bulb and remove the delay leaving only the bypass, and see whether you get the same effect. This could be a clue as you’d be removing a small capacitive load out of the circuit and the reaction may differ. If it’s the same, then the issue could be with the Shelly rather than being an overall circuit-voltage/noise issue.

Interesting, that makes sense.
I’ll try removing the bulb later today to test it and report back, thanks