User_ID Null?

How do I pass the user_id? Regardless of how I turn on a light/switch (automation, frontend, etc), the user_id remains null

I have no idea what you are referring to. Please read this How to help us help you - or How to ask a good question

My apologies. I read that when an event triggers through an automation or through the frontend (i.e. turning on a light), there would be json message containing a populated user_id (under the context portion). My json messages always contain a null value (see the below sample), regardless of how I trigger the event (automation, frontend, etc.)

{"_msgid":"249aa0f6.a759c","topic":"","payload":"off","data": 
{"entity_id":"light.living_room","state":"off","attributes
{"node_id":57,"value_index":0,"value_instance":1,"value_id":"72####","friendly_name 
":"Living Room","supported_features":1,"icon":"mdi:lightbulb"},"last_changed":"2019-08- 
20T01:40:18.758789+00:00","last_updated":"2019-08-20T01:40:18.758789+00:00","context": 
 {"id":"d4####","parent_id":null,"user_id":null},"timeSinceChangedMs":24270115}}

from what? You have to clarify everything. Down to component, and location of these messages.

Side Note: I don’t think automations will ever know what user executed it because automations are just run though home assistant, not a user action. Outside of triggering the automation through the UI. But that code could be missing atm. Or (a more likely scenario) it’s not implemented because automations are never ran by a user.

Specifically, I’m looking at Node Red, but I don’t believe that it is NR specific, as it appears user_id is being written to the state machine under new_state when there is a state_changed event. And it should be for every entity that I have configured, as it doesn’t seem to be component specific, but for my use case, I’m looking at lights and switches. The user_id I’m guessing is being pulled from here:

Capture

I just ran a simple test. User ID is only populated from the UI when the action is performed on the UI. I pressed a switch in the UI and my user id was populated to the context. I used the hardware switch and user id was null. This was performed on an item in the switch domain.

Now on to an automation.

Based on the information we have from the switch behavior, I would assume it would work the same way. I.E. If you trigger from the UI, the user would be populated. However, if the automation naturally fires (it has no user context because this is done without user interaction) the user would remain null.

Unfortunately, triggering from the UI does not populate the user_id, however the parent_id does get populated. I do not know what this parent id references.

1 Like

Ok good to know, then (for now) it seems as it doesn’t allow me to differentiate between hardware and automation action, as in both instances the user_id remains null. Maybe this is something planned for the future. Thanks for testing!

Well, maybe that’s what the parent id is for. I could see a device having a parent ID if the context came from an automation. I can’t test this now but it would be an easy test.

Look at the switch context.
Make an automation that turns on a switch.
Fire the automation.
Look at the context for the switch.
Turn on the switch manually.
Look at the context for the switch.

Hello, I’m looking to something similar to implement an automation based on the user_id logged in. This is to arm and disarm an alarm but I would like the user code instead of default system code to know which user has performed the auction since the alarm system report the event based on the user code used. Is this possible?