Shelly triggers automations when becoming available/unavailable

Hello community, I searched the forum but I find only old queries. I have several shellies correctly configured, who trigger automation to switch on/off related yeelight lamps. Everything works perfect as my wall switch trigger shelly, then HA will switch on/off the related light.

My problem is popping up when the shellies lost connection (e.g. router reboot or something similar). In that case, the automation is triggered and the light is switched on/off in all my house.

As I understood from other old post, when shelly state changes from unknown (disconnected) to on or off (reconnected), this is sufficient to trigger the automation. To avoid that I should add a from field to trigger section, so that automation starts only if the previous state was on or off only.

This is one of the original automation (generated by gui):

- id: '1722951862213'
  alias: Cameretta Interruttore
  description: ''
  trigger:
  - platform: device
    type: changed_states
    device_id: 85d144a105d30a3561f0ffca13e5ab9c
    entity_id: 4141cf81f7f352758859148370c84b43
    domain: switch
  condition: []
  action:
  - type: toggle
    device_id: 2dafb74ebf534be4afccdbfa3d446d09
    entity_id: c13a86c61ce25cc0f66fc9d1e6c65198
    domain: light
  mode: single

Then I tried to add the ‘from’ to trigger. This should trigger only when it changes from “on” to “off” or from “off” to “on” and ignore all other states

- id: '1722951862213'
  alias: Cameretta Interruttore
  description: ''
  trigger:
  - platform: device
    type: changed_states
    device_id: 85d144a105d30a3561f0ffca13e5ab9c
    entity_id: 4141cf81f7f352758859148370c84b43
    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
    domain: switch
  condition: []
  action:
  - type: toggle
    device_id: 2dafb74ebf534be4afccdbfa3d446d09
    entity_id: c13a86c61ce25cc0f66fc9d1e6c65198
    domain: light
  mode: single

Unfortunately this end in:
Error:extra keys not allowed @ data['from']. Got None extra keys not allowed @ data['to']. Got None .

Could someone please help me to understand how to accomplish this?
Thank you,
Lucas

Those keywords only work with state triggers and conditions, which are preferred over device actions (what you do) anyway:

1 Like

Thanks, I completely missed such great community guide. Now I have to rewrite all my automations, but at least I’ll avoid the annoying trig when shelly will become unavailable.

1 Like

Rewrite only when you do work on it. You can leave it as long as they work as expected. The guide is to prevent bulk rewrites, so why bulk rewrite to prevent it… This is generally called: use the “Boy wcout rule”. When you work on it, leave it cleaner then when you started.

I don’t understand your point. You kindly drive me to a solution to my issue (shelly disconnected/unavailable trigger the automation). So, rewriting the automation in below way, will solve my issue:

- id: Cameretta Interruttore
  alias: Cameretta Interruttore
  trigger:
    - platform: state
      entity_id: switch.shellyplus2pm_c049ef8dc054_switch_1
      not_to:
        - unavailable
        - unknown
      not_from:
        - unavailable
        - unknown
  action:
    - service: light.toggle
      entity_id: light.cameretta

You said you were going to rewrite ALL your automations to avoid device id’s. I would not change them all at once, but gradually, one by one if you have a reason to change an automation. Device actions MIGHT force you to change a lot of automations. Not using them avoids that chance. Changing them all just because it might happen is even worse.

The way you wrote it will indeed avoid restart problems. I’m not sure if this will trigger too when only an attribute changes though, so I would stick with:

    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"

Oh yes, with “ALL” I meant only the automations related to shelly/yeelight, moreover the trigger issue, for some reasons, affecting only the Shelly 2 PM, while the old Shelly 1 doesn’t have such issue
And yes, I ended to use:

    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
1 Like