Smart switches turning on after a reboot

I have two different rooms that each have an Aqara smart switch in them. The switches control Sonoff smart plugs that have been flashed with ESPHome. The plugs have lamps attached to them which is how I light these rooms. My Home Assistant machine is restarted once a week in the middle of the night. About 25% of the time, I come downstairs in the morning to find the lights are on for these two rooms. I looks like the Aqara switches toggle to a different state upon the server coming back up.
Since these are the only two Aqara switches that control smart plugs in the house, I am guessing they just don’t like controlling smart plugs. I am at a bit of a loss on how to fix this. One of the rooms is a guest room and I really don’t want the lights coming on at 2am if we have someone in there.

If you have an automation for the switches that controls the lights then show it here, properly formatted. It may need some small adjustments to account for unavailability.

Here is my automation. It is just a very basic toggle automation.

alias: "Switch: Living Room Lamp"
description: ""
triggers:
  - type: changed_states
    device_id: 81e7c55a9ac9c4d85a56f8646dd665e2
    entity_id: 36a88509fbe844954c23188c766fc108
    domain: light
    trigger: device
conditions: []
actions:
  - type: toggle
    device_id: e96afc3ffcda2fe9b89e4ddb4f760445
    entity_id: f07af4993ddac9150286e44a5a3c0d62
    domain: switch
mode: single

The automation reacts on ANY state change to toggle the light. So it also reacts to the switch being unavailable during a reboot and then changing to an available state. That is why it misbehaves.

Unfortunately it is a device trigger. Those are best avoided for several reasons, among which that afaik this problem cannot be fixed with this type of trigger. It can of course be fixed, but for that I need to know what entities it has (if any) or what events it emits.

Assuming there is an entity for the switch (I see light domain?) the automation would look something like below. Replace with the entities of your specific devices:

alias: "Switch: Living Room Lamp"
description: ""
triggers:
  - trigger: state
    entity_id: light.livingroom_switch
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - action: light.toggle
    metadata: {}
    data: {}
    target:
      entity_id: light.livingroom
mode: single

Notice I also replaced the device action with a light action. That is not needed, but advised. Also there you would need to put in the right entity id. Why it is wise to change it too is explained here:

1 Like