How to improve Shelly light switch automation, so Shelly unavailabilities don't trigger?

Hey,
I’m using several Shelly 1 Mini Gen3 as detached switchs to switch on and off my zigbee lights without cutting their power supply.

To do so I’m using the “status” of the Shellys to toggle the lights when I toggle my switch. So it doesn’t matter if I open oder close the switch or if the lights are currently on or off.

Everything works as expected, but with one exception:
The Shellys become occasionally “unavailable”, preferable the one in my bedroom during the night. This unavilability changes the “status” and this toggles my lights.

I tried to workaraound these unintended toggles with a condition, that cheks that the shelly is not unavailable. (checking if the shelly is available didn’t work at all)

Attached you can see a screenshot of the visual editor and the yaml code:

The screenshot of the automation:

The code:

alias: Lichtschalter Schlafzimmer
description: ""
triggers:
  - type: opened
    device_id: 6981dc6bdc65b4795cbd06adbdf02849
    entity_id: 69954f6ccc669fd9fa49a29a5443b68e
    domain: binary_sensor
    trigger: device
  - type: not_opened
    device_id: 6981dc6bdc65b4795cbd06adbdf02849
    entity_id: 69954f6ccc669fd9fa49a29a5443b68e
    domain: binary_sensor
    trigger: device
conditions:
  - condition: not
    conditions:
      - condition: state
        entity_id: binary_sensor.shelly_schlafzimmer_status
        state: unavailable
actions:
  - choose:
      - conditions:
          - condition: device
            type: is_off
            device_id: e418671b88493dbb0598e89f0f36f16b
            entity_id: 36a3d8463f94351d9ebff1c52f6fef47
            domain: light
        sequence:
          - type: turn_on
            device_id: e418671b88493dbb0598e89f0f36f16b
            entity_id: 36a3d8463f94351d9ebff1c52f6fef47
            domain: light
      - conditions:
          - condition: device
            type: is_on
            device_id: e418671b88493dbb0598e89f0f36f16b
            entity_id: 36a3d8463f94351d9ebff1c52f6fef47
            domain: light
        sequence:
          - type: turn_off
            device_id: e418671b88493dbb0598e89f0f36f16b
            entity_id: 36a3d8463f94351d9ebff1c52f6fef47
            domain: light
mode: single


And a screenshot of the log:

image

Hi! I think that the problem is that you have created an action on the basis of the “status” changing to “opened”, and the Shelly is already in an available state when that action is being run - so the light comes on as it appears to be a valid action.

I think (and I could be wrong) that you need a “from unknown state” template such as what Tom suggested back in Sept 22:

Instead of using the device status, use just that binary sensor entity value. Then in the triggers you can have one that says FROM off TO on and one that says FROM on TO OFF. That was if the Shelly becomes unavailable and then comes back, nothing will trigger because that entity will be going FROM unavailable TO one of the other states. It’ll look something like this:

triggers:
  - entity_id:
      - binary_sensor.shelly_binary_sensor_name
    from: "on"
    to: "off"
  - entity_id:
      - binary_sensor.shelly_binary_sensor_name
    from: "off"
    to: "on"
1 Like

Kyle is right.
And to expand on his yaml, use this:

triggers:
  - entity_id:
      - binary_sensor.shelly_schlafzimmer_status
    from: "on"
    to: "off"
  - entity_id:
      - binary_sensor.shelly_schlafzimmer_status
    from: "off"
    to: "on"
actions:
 - action: "light.turn_{{ trigger.to_state.state }}"
    metadata: {}
    data: {}
    target:
      entity_id: light.entity_id
1 Like

Far simpler than a template! :grin: