Shelly: how to automate when constantly not available?

Hi everybody,

I have a Shelly 1 hooked up to a light switch in my living room. Below is the automation I tried to work with, however, it does not work. If I only have to: on (or off) without from: off (or on), it will trigger each time the switch switches from on or off to unavailable. So that does not work. The device should have decent wifi, but as you can see below, it constantly switches to not available.

grafik

The bar looks red, but if you look closely, there are constant gray bars every couple seconds, indicating that the device is unavailable.

The Shelly is configured like this

I chose POWER ON DEFAULT MODE: ON because the light bulb (or one of them) that I want to control is a zigbee controlled device, so it needs to always be powered on. Then I chose BUTTON TYPE: Detached Switch so that using the switch would not turn off the light bulb connected to the switch.

This is the automation I wrote; I had included from above to in the trigger section, but then it won’t work at all as the switch will not change from off to on or vice versa, but rather from something to unavailable to something else.

automation:
  - alias: "[Licht] Toggle Wohnzimmer 1"
    trigger: 
      - platform: state
        entity_id: binary_sensor.shelly1_68a7bf_input_0
        to: "on"
    action:
        # {% if states("light.innr_02_light") == "on" or states("light.wohnzimmer_fenster_light") == "on" %}
      service_template: >
        {% if states("light.wohnzimmer_fenster_light") == "on" %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      entity_id: light.wohnzimmer_fenster_light, light.innr_02_light
  - alias: "[Licht] Toggle Wohnzimmer 2"
    trigger: 
      - platform: state
        entity_id: binary_sensor.shelly1_68a7bf_input_0
        to: "off"
    action:
        # {% if states("light.innr_02_light") == "on" or states("light.wohnzimmer_fenster_light") == "on" %}
      service_template: >
        {% if states("light.wohnzimmer_fenster_light") == "on" %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      entity_id: light.wohnzimmer_fenster_light, light.innr_02_light

The rest can probably be done more elegantly, but it works for me. If either of the two light I’d like to switch is already on, light.turn_off them both, otherwise use light.turn_on. Well, currently it is only supposed to control one light, that’s why the line above service_template is commented out.

Any of you got problems with Shellies like this? I am not sure if it is actually always unavailable (wifi or connection wise), or if something else is wrong (mqtt? what else could it be?)… My router states that the device has been connected for 50+ days; however, the unavailability timeout is so short that perhaps the router does not even recognize it to be disconnected and that is the actual problem.

Either way, this defeats the purpose of having a Shelly at all. Did I do something wrong, or is this a Shelly problem, or do you know what else might cause this? Thank you for your input :slight_smile:

You can have a condition in your automation to ignore transitioning to or from the ‘unavailable’ state.

condition:
  - condition: template
    value_template: >
      {{ 'unavailable' not in [trigger.to_state.state, trigger.from_state.state] }}

Thank you @SteveDinn! This did the trick. :pray:

I will not mark this as solved yet, as I hope that somebody will be able to help me figure out why the Shelly constantly keeps disconnecting. I updated the firmware to the latest version and those disconnecteds / whatever else might cause the unavailability are less frequent, but they are still there.

What the signal strength indicated on the Shelly’s web interface?

Btw, Shelly has a very responsive Facebook Group, perhaps you should ask there if anyone knows how to improve reliability.

It is more likely to be a wifi issue than a shelly issue.

I got -49 dBm signal strength according to the Shelly webUI; my UniFi interface states 99% (-39 dBm).