Simple Node-RED Sub-flow to Look Up Username
Here is a quick helper for Node-RED to fetch the Home Assistant user who triggered a state change...
Here is a quick helper for Node-RED to fetch the Home Assistant user who triggered a state change, allowing you to use that name in any of your automations. For example, if your alarm panel is armed, you can see who disarmed it and publish a notification that says, Alarm disarmed by John.
The Sub-flow can attach to any state-trigger node, and it will pull the name of the user who triggered the state change and append that name to the message. Here the output from the example flow above:
The original message properties are preserved, and the Sub-flow adds a new user_name property. This also works for state changes that were triggered by automation:
The Sub-flow works by reading the auth file in home assistant. It takes the user ID from the trigger payload and matches that up with a user in the auth file, and pulls that user's name.
If all of your devices log in with unique usernames, now you can enhance your automations based on which device triggered certain state changes. You can easily drop this node into your existing flows to do new things, like having lights respond differently to different users, or having the media player start with different defaults.
Here is the code. Enjoy!
[{"id":"b62e4b55.b6e888","type":"subflow","name":"Get Trigger User Name","info":"","category":"","in":[{"x":280,"y":140,"wires":[{"id":"8ca2a17.9ca4d6"}]}],"out":[{"x":1080,"y":140,"wires":[{"id":"63bfcf7a.4ea8c","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"71513b70.d07824","type":"file in","z":"b62e4b55.b6e888","name":"auth","filename":"/config/.storage/auth","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":550,"y":140,"wires":[["833cfa89.049518"]]},{"id":"833cfa89.049518","type":"json","z":"b62e4b55.b6e888","name":"","property":"payload","action":"obj","pretty":false,"x":670,"y":140,"wires":[["2bd011b4.4f829e"]]},{"id":"2bd011b4.4f829e","type":"function","z":"b62e4b55.b6e888","name":"","func":"var user_id = msg.data.event.new_state.context.user_id;\nvar user_name = \"\";\n\nfor (var i = 0; i < msg.payload.data.users.length; i++) {\n \n var user = msg.payload.data.users[i];\n \n console.log(user.id);\n \n if (user.id == user_id) {\n user_name = user.name;\n break;\n }\n}\n\nmsg.user_name = user_name == \"Hass.io\" ? \"Home Assistant\" : user_name;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":140,"wires":[["63bfcf7a.4ea8c"]]},{"id":"8ca2a17.9ca4d6","type":"change","z":"b62e4b55.b6e888","name":"","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":140,"wires":[["71513b70.d07824"]]},{"id":"63bfcf7a.4ea8c","type":"change","z":"b62e4b55.b6e888","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":140,"wires":[[]]},{"id":"35dbeae6.2f8b46","type":"subflow:b62e4b55.b6e888","z":"4d4043ba.fd1dcc","name":"","env":[],"x":530,"y":320,"wires":[[]]}]