Context gets only updated after some time

My issue arises in a node-red flow, but I assume it already happens in how HA handles the entities, so it is uncategorized.

I have a switch connected by the Tuya Local integration and try to figure out if it has been toggled manually or by node-red (later maybe also dashboard).
Therefore, I check if the data.context.user_id is null or not.
This works quite well but only if there was some time between the last toggle by node red and the manual press (about 5 - 10 seconds).
With debugging, I found out that before that time, context.user_id is still filled with the id of node red.

My assumption is that HA or the Tuya Local integration only update context if it has been changed or after a timeout. Since context is null in that case, the previous context is compared to nothing and thus, deemed as not changed and not updated.

Is there another explanation / idea / point I could debug, or is it possible to “reset” the context in another way?

Post your flow, it may be the way it’s structured.

This is it:

Debug 5 already displays the “wrong” context.

[
    {
        "id": "a7851ab230b3ef83",
        "type": "subflow",
        "name": "Marker: Manuell ausgeführt",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 60,
                "y": 240,
                "wires": [
                    {
                        "id": "6a62f612b7b0ef3c"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 700,
                "y": 240,
                "wires": [
                    {
                        "id": "6a62f612b7b0ef3c",
                        "port": 1
                    },
                    {
                        "id": "aba83476cf9f4785",
                        "port": 1
                    },
                    {
                        "id": "25ee3ca067bd307a",
                        "port": 1
                    },
                    {
                        "id": "f1cc60b4e9db854d",
                        "port": 1
                    },
                    {
                        "id": "e27bc7ef9b80721d",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "6a62f612b7b0ef3c",
        "type": "switch",
        "z": "a7851ab230b3ef83",
        "name": "data != undefined",
        "property": "data",
        "propertyType": "msg",
        "rules": [
            {
                "t": "neq",
                "v": "undefined",
                "vt": "jsonata"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 290,
        "y": 240,
        "wires": [
            [
                "aba83476cf9f4785"
            ],
            []
        ]
    },
    {
        "id": "aba83476cf9f4785",
        "type": "switch",
        "z": "a7851ab230b3ef83",
        "name": "context != undefined",
        "property": "data.context",
        "propertyType": "msg",
        "rules": [
            {
                "t": "neq",
                "v": "undefined",
                "vt": "jsonata"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 300,
        "y": 180,
        "wires": [
            [
                "25ee3ca067bd307a"
            ],
            []
        ]
    },
    {
        "id": "25ee3ca067bd307a",
        "type": "switch",
        "z": "a7851ab230b3ef83",
        "name": "context != null",
        "property": "data.context",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nnull"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 280,
        "y": 120,
        "wires": [
            [
                "f1cc60b4e9db854d"
            ],
            []
        ]
    },
    {
        "id": "f1cc60b4e9db854d",
        "type": "switch",
        "z": "a7851ab230b3ef83",
        "name": "user_id == null",
        "property": "data.context.user_id",
        "propertyType": "msg",
        "rules": [
            {
                "t": "null"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 280,
        "y": 60,
        "wires": [
            [
                "e27bc7ef9b80721d"
            ],
            []
        ]
    },
    {
        "id": "e27bc7ef9b80721d",
        "type": "change",
        "z": "a7851ab230b3ef83",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "manually",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 540,
        "y": 60,
        "wires": [
            []
        ]
    },
    {
        "id": "de639a6fbce15616",
        "type": "subflow",
        "name": "Fenster offen?",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 40,
                "y": 40,
                "wires": [
                    {
                        "id": "eacf6d5c176c2e7f"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 700,
                "y": 80,
                "wires": [
                    {
                        "id": "ca3d2e933e450c5f",
                        "port": 0
                    }
                ]
            },
            {
                "x": 710,
                "y": 160,
                "wires": [
                    {
                        "id": "ca3d2e933e450c5f",
                        "port": 1
                    }
                ]
            },
            {
                "x": 710,
                "y": 300,
                "wires": [
                    {
                        "id": "adc2e297705c5ad8",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99",
        "outputLabels": [
            "ja",
            "nein",
            "array"
        ],
        "status": {
            "x": 680,
            "y": 380,
            "wires": [
                {
                    "id": "adc2e297705c5ad8",
                    "port": 0
                }
            ]
        }
    },
    {
        "id": "7aac9afe7a54bbb5",
        "type": "api-current-state",
        "z": "de639a6fbce15616",
        "name": "Balkontür Auf",
        "server": "bb4214b1.c101f8",
        "version": 3,
        "outputs": 2,
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "binary_sensor.tursensor_wohnzimmer_z2m_contact",
        "state_type": "str",
        "blockInputOverrides": true,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "die Balkontür",
                "valueType": "str"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 120,
        "y": 200,
        "wires": [
            [
                "349fba9f70c77b70",
                "2ad0dd313dfdeed8"
            ],
            [
                "349fba9f70c77b70"
            ]
        ],
        "outputLabels": [
            "ja",
            "nein"
        ]
    },
    {
        "id": "9a504cb3815b5163",
        "type": "api-current-state",
        "z": "de639a6fbce15616",
        "name": "Fenster Schlafzimmer Auf",
        "server": "bb4214b1.c101f8",
        "version": 3,
        "outputs": 2,
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "binary_sensor.fenstersensor_schlafzimmer_1_z2m_contact",
        "state_type": "str",
        "blockInputOverrides": true,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "das Schlafzimmerfenster",
                "valueType": "str"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 150,
        "y": 140,
        "wires": [
            [
                "7aac9afe7a54bbb5",
                "2ad0dd313dfdeed8"
            ],
            [
                "7aac9afe7a54bbb5"
            ]
        ],
        "outputLabels": [
            "ja",
            "nein"
        ]
    },
    {
        "id": "349fba9f70c77b70",
        "type": "api-current-state",
        "z": "de639a6fbce15616",
        "name": "Wohnungstür Auf",
        "server": "bb4214b1.c101f8",
        "version": 3,
        "outputs": 2,
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "binary_sensor.tursensor_wohnungstur_z2m_contact",
        "state_type": "str",
        "blockInputOverrides": true,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "complete",
                "propertyType": "msg",
                "value": "true",
                "valueType": "bool"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "die Wohnungstür",
                "valueType": "str"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 130,
        "y": 260,
        "wires": [
            [
                "2ad0dd313dfdeed8"
            ],
            [
                "2814f12828762764"
            ]
        ],
        "outputLabels": [
            "ja",
            "nein"
        ]
    },
    {
        "id": "2814f12828762764",
        "type": "change",
        "z": "de639a6fbce15616",
        "name": "Clear Topic",
        "rules": [
            {
                "t": "delete",
                "p": "topic",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 310,
        "y": 300,
        "wires": [
            [
                "2ad0dd313dfdeed8"
            ]
        ]
    },
    {
        "id": "ca3d2e933e450c5f",
        "type": "switch",
        "z": "de639a6fbce15616",
        "name": "",
        "property": "topic.length",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gt",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 570,
        "y": 120,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "adc2e297705c5ad8",
        "type": "join",
        "z": "de639a6fbce15616",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "topic",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": false,
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 550,
        "y": 300,
        "wires": [
            [
                "ca3d2e933e450c5f"
            ]
        ]
    },
    {
        "id": "eacf6d5c176c2e7f",
        "type": "subflow:a7851ab230b3ef83",
        "z": "de639a6fbce15616",
        "name": "",
        "x": 240,
        "y": 40,
        "wires": [
            [
                "9a504cb3815b5163"
            ]
        ]
    },
    {
        "id": "2ad0dd313dfdeed8",
        "type": "change",
        "z": "de639a6fbce15616",
        "name": "",
        "rules": [
            {
                "t": "delete",
                "p": "data.context",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 220,
        "wires": [
            [
                "adc2e297705c5ad8"
            ]
        ]
    },
    {
        "id": "bb4214b1.c101f8",
        "type": "server",
        "name": "Home Assistant",
        "addon": true,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "",
        "connectionDelay": false,
        "cacheJson": false,
        "heartbeat": false,
        "heartbeatInterval": "10",
        "statusSeparator": "",
        "enableGlobalContextStore": false
    },
    {
        "id": "f5fa79c09a7c02ce",
        "type": "subflow",
        "name": "Status Fenster/Türen",
        "info": "",
        "category": "",
        "in": [],
        "out": [],
        "env": [],
        "meta": {},
        "color": "#DDAA99",
        "status": {
            "x": 680,
            "y": 180,
            "wires": [
                {
                    "id": "3f60512170c923fc",
                    "port": 0
                }
            ]
        }
    },
    {
        "id": "85a6e72ee93fce16",
        "type": "subflow:de639a6fbce15616",
        "z": "f5fa79c09a7c02ce",
        "name": "",
        "x": 340,
        "y": 100,
        "wires": [
            [
                "b55ac076060cc762"
            ],
            [
                "e368f337e7dae5f6"
            ],
            [
                "3f60512170c923fc"
            ]
        ],
        "outputLabels": [
            "ja",
            "nein",
            "array"
        ]
    },
    {
        "id": "b55ac076060cc762",
        "type": "api-call-service",
        "z": "f5fa79c09a7c02ce",
        "name": "Status 2 Rot",
        "server": "bb4214b1.c101f8",
        "version": 7,
        "debugenabled": false,
        "action": "light.turn_off",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "light.statusschalter_haustur_gang_2"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": true,
        "domain": "light",
        "service": "turn_off",
        "x": 630,
        "y": 60,
        "wires": [
            [
                "995a39e1fe8ed168"
            ]
        ]
    },
    {
        "id": "b927879ae9bb6c9c",
        "type": "server-state-changed",
        "z": "f5fa79c09a7c02ce",
        "name": "Fenster/Tür offen",
        "server": "bb4214b1.c101f8",
        "version": 6,
        "outputs": 2,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "binary_sensor.fenstersensor_schlafzimmer_1_z2m_contact",
                "binary_sensor.tursensor_wohnzimmer_z2m_contact",
                "binary_sensor.tursensor_wohnungstur_z2m_contact"
            ],
            "substring": [],
            "regex": []
        },
        "outputInitially": false,
        "stateType": "habool",
        "ifState": "true",
        "ifStateType": "bool",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 120,
        "y": 60,
        "wires": [
            [
                "85a6e72ee93fce16"
            ],
            [
                "85a6e72ee93fce16"
            ]
        ]
    },
    {
        "id": "e368f337e7dae5f6",
        "type": "api-call-service",
        "z": "f5fa79c09a7c02ce",
        "name": "Status 2 Blau",
        "server": "bb4214b1.c101f8",
        "version": 7,
        "debugenabled": false,
        "action": "light.turn_on",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "light.statusschalter_haustur_gang_2"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": true,
        "domain": "light",
        "service": "turn_on",
        "x": 630,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "1110de8f893b0c73",
        "type": "trigger-state",
        "z": "f5fa79c09a7c02ce",
        "name": "Statusschalter 2",
        "server": "bb4214b1.c101f8",
        "version": 5,
        "inputs": 0,
        "outputs": 2,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "light.statusschalter_haustur_gang_2"
            ],
            "substring": [],
            "regex": []
        },
        "debugEnabled": false,
        "constraints": [
            {
                "targetType": "this_entity",
                "targetValue": "",
                "propertyType": "current_state",
                "propertyValue": "new_state.state",
                "comparatorType": "is",
                "comparatorValueDatatype": "str",
                "comparatorValue": "off"
            }
        ],
        "customOutputs": [],
        "outputInitially": true,
        "stateType": "str",
        "enableInput": false,
        "x": 120,
        "y": 320,
        "wires": [
            [
                "85a6e72ee93fce16",
                "007c4366f23d73b4"
            ],
            [
                "85a6e72ee93fce16",
                "007c4366f23d73b4"
            ]
        ]
    },
    {
        "id": "3f60512170c923fc",
        "type": "change",
        "z": "f5fa79c09a7c02ce",
        "name": "Nutze msg.topic",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "topic",
                "tot": "msg",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 580,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "a5899e3ffa28f204",
        "type": "link call",
        "z": "f5fa79c09a7c02ce",
        "name": "",
        "links": [
            "dbe4febfe16afe9a"
        ],
        "linkType": "static",
        "timeout": "1",
        "x": 750,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "995a39e1fe8ed168",
        "type": "function",
        "z": "f5fa79c09a7c02ce",
        "name": "Manuell ausgeführt?",
        "func": "if (msg.manually === true)\n    return msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 260,
        "wires": [
            [
                "a5899e3ffa28f204"
            ]
        ]
    },
    {
        "id": "b62dc5cb884be03b",
        "type": "debug",
        "z": "f5fa79c09a7c02ce",
        "name": "debug 7",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1070,
        "y": 180,
        "wires": []
    },
    {
        "id": "007c4366f23d73b4",
        "type": "debug",
        "z": "f5fa79c09a7c02ce",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 340,
        "y": 320,
        "wires": []
    }
]

I don’t see a tuya light in that flow, what you posted is for a smart meter.

It is about

This is a smart switch with 3 buttons, and each button has an LED indicator to indicate whether it is on or off. I use that indication to display statuses, hence the light type. The entity is “created” by TuyaLocal.

Apologies I didn’t notice this was subflow. This is the relevant section?

Where is the input coming from? I assume another HA node like a trigger or event state? If yes, it’s output should be current and reflect what HA has.

You can use an event node to intercept the raw events. I think if the entity’s context is updated, you would see it come through the event bus. Change the entity id in the data section. Don’t leave it deployed, test then disable it.

[{"id":"41f0f55d223130a6","type":"server-events","z":"d4b982ad2e73341f","name":"","server":"","version":3,"exposeAsEntityConfig":"","eventType":"","eventData":"{\"domain\":\"light\",\"service_data\":{\"entity_id\":\"light.dj\"}}","waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":400,"y":1640,"wires":[["fbd3555e50d8ea15"]]}]

This can also be done in dev tools → events. The only other way I could think of testing it would be to setup an automation in HA and see if it has same result. This is similar, the template needs to be adjusted.

https://community.home-assistant.io/t/track-who-and-when-pressed-a-button-in-ui-dashboard/784099/6

There is no specific node that causes the problem.
Using the dev tools was a good suggestion. Here, I can see that homeassistant has the problem even before NodeRed.

Here I just pressed the switch (data.context.user_id = null):

event_type: state_changed
data:
  entity_id: light.statusschalter_haustur_gang_2
  old_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "on"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: onoff
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:35:27.712855+00:00"
    last_reported: "2025-04-07T19:35:27.712855+00:00"
    last_updated: "2025-04-07T19:35:27.712855+00:00"
    context:
      id: 01JR8W09FMVV1Y7A1PYYZHJYFT
      parent_id: null
      user_id: c5ac1c711349404a9eed9527f8aa3658
  new_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "off"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: null
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:43:19.003867+00:00"
    last_reported: "2025-04-07T19:43:19.003867+00:00"
    last_updated: "2025-04-07T19:43:19.003867+00:00"
    context:
      id: 01JR8WENYVYP1TD7W3VXN4PRRP
      parent_id: null
      user_id: null
origin: LOCAL
time_fired: "2025-04-07T19:43:19.003867+00:00"
context:
  id: 01JR8WENYVYP1TD7W3VXN4PRRP
  parent_id: null
  user_id: null

Here, the switch is toggled by NodeRed following an event by another sensor (data.context.user_id is set):

event_type: state_changed
data:
  entity_id: light.statusschalter_haustur_gang_2
  old_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "on"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: onoff
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:43:19.255371+00:00"
    last_reported: "2025-04-07T19:43:19.255371+00:00"
    last_updated: "2025-04-07T19:43:19.255371+00:00"
    context:
      id: 01JR8WEP05QA56PJN7GJ29HXED
      parent_id: null
      user_id: c5ac1c711349404a9eed9527f8aa3658
  new_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "off"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: null
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:43:23.060411+00:00"
    last_reported: "2025-04-07T19:43:23.060411+00:00"
    last_updated: "2025-04-07T19:43:23.060411+00:00"
    context:
      id: 01JR8WESQHCPKCMH4NG8RNRHXG
      parent_id: null
      user_id: c5ac1c711349404a9eed9527f8aa3658
origin: LOCAL
time_fired: "2025-04-07T19:43:23.060411+00:00"
context:
  id: 01JR8WESQHCPKCMH4NG8RNRHXG
  parent_id: null
  user_id: c5ac1c711349404a9eed9527f8aa3658

And here, I did exactly the same like in the first log and just pressed the switch manually (data.context.user_id is still set):

event_type: state_changed
data:
  entity_id: light.statusschalter_haustur_gang_2
  old_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "off"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: null
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:43:23.060411+00:00"
    last_reported: "2025-04-07T19:43:23.060411+00:00"
    last_updated: "2025-04-07T19:43:23.060411+00:00"
    context:
      id: 01JR8WESQHCPKCMH4NG8RNRHXG
      parent_id: null
      user_id: c5ac1c711349404a9eed9527f8aa3658
  new_state:
    entity_id: light.statusschalter_haustur_gang_2
    state: "on"
    attributes:
      supported_color_modes:
        - onoff
      color_mode: onoff
      friendly_name: Statusschalter Haustür Gang 2
      supported_features: 0
    last_changed: "2025-04-07T19:43:24.642677+00:00"
    last_reported: "2025-04-07T19:43:24.642677+00:00"
    last_updated: "2025-04-07T19:43:24.642677+00:00"
    context:
      id: 01JR8WESYPS59Q8BCK82YNBXVQ
      parent_id: null
      user_id: c5ac1c711349404a9eed9527f8aa3658
origin: LOCAL
time_fired: "2025-04-07T19:43:24.642677+00:00"
context:
  id: 01JR8WESYPS59Q8BCK82YNBXVQ
  parent_id: null
  user_id: c5ac1c711349404a9eed9527f8aa3658

If I wait 5 - 10 seconds and press it again, I will get the same output as in log 1 (data.context.user_id = null).
It would like to have that result every time I press the switch manually.

I just tested this and can confirm that it reports the previous user_id instead of null if the switch is turned on or off manually within about 5 seconds. If the switch is first changed manually (null) followed by an automation or the Companion App making the change within 5 seconds, it correctly reports the used_id.

I tested with a switch using the Shelly Integration and a fan using the Modern Forms Integration. I’m going to assume that the issue lies with HA, and I don’t see any way to force it to update faster or to reset it. In my case the timing isn’t critical so it’s not an issue.

I think you can assume that any null report is accurate (switched manually), but if the user_id is populated you could compare the data.old_state.last_changed and data.new_state.last_changed values to see if it was previously changed by an automation in the last 5? seconds.

There could be an overlap of two automations in between 5 seconds, or there probably are.
But the idea is still good, I can define a global variable or a trigger which is reset with every automated change of state, and only the one flow that is triggered by a manual press can evaluate (and not reset) it.

Thanks you two!