Trigger an automation only when not activated via the UI (mechanical switch)

Hello!

I have found a little bug in the triggers, if I fire up a switch via the Lovelace UI, and into a period of a couple of seconds (minus than 5) I fire up the same switch via a mechanical switch, the trigger.to_state.context.user_id is still populated and not none. could there be some delay in writing down that uid when populating it via a switch? or maybe the delay from the device to homeassistant server?

My server is not really busy, 5% cpu at max, but wondered if the device interrogation at this point could be made faster, I would prefer hammering them, than have a couple of seconds delay, considering that my device are lan triggered (sonoff ewelink, lan controlled)

only for reference, automation, with ids redacted:

alias: Commute
description: ""
triggers:
  - type: turned_on
    device_id: d1
    entity_id: e1
    domain: switch
    trigger: device
conditions:
  - condition: and
    conditions:
      - condition: template
        value_template: >-
          {{ trigger.to_state.context.user_id is none }}
actions:
  - type: toggle
    device_id: d1
    entity_id: e2
    domain: switch
mode: single

Automation traces will answer those questions.

keep you in mind, if your switch was turned on by automation - user_id will also is not null.
so you should also check user_id != SUPERVISOR_user_id

I managed to find out why, that’s a custom component delay (LAG), the entity will be updated later, I reduced the delay into the sonoff LOCAL (LAN), reducing it to 0.5 seconds, in the loop iteration, now it gets updated in a second or less (python files mods).

The automation triggers the second entity, so there isn’t any traces of the supervisor in the first entity (D1 device 1, E1 entity 1), but only in the second entity (same device D1, different entity E2) if activated mechanically

I just switched all the sonoff to ESPHome, I wasn’t really satisfied with the software mode.
It’s just prone to all kind of errors, not really good if used in that way.