I have 3 Home Assistant users and any of them can toggle lights and switches.
I know that currently HA management of users and their access is not yet sophisticated enough and it is work in progress but
is it possible to somehow detect and get the username who changed entity state for further processing?
Just found out there is actually user_id available about user who toggles light or switch from Lovelace UI.
Probably introduced in latest releases of HA - there is still no straightforward way but I could get this from state_changed events.
Finally managed to get it working as I wanted long time ago.
In order to get notified if other family members are toggling lights from HA UI I setup notifications a la
- alias: "Notify light/switch being toggled by others user via HA UI"
trigger:
- platform: state
entity_id:
- light.bedroom_ceiling_light
...
condition:
- condition: template
value_template: "{{ trigger.to_state.context.user_id != None }}" # filter out all occasions where state toggled not by user via HA UI. e.g. automations
- condition: template
value_template: "{{ trigger.to_state.context.user_id != '1234b8d2572e4594b48adb964590bbac' }}" # filter out all occasions where state toggled by me
action:
- service: notify.an_telegram
data_template:
message: "{{ trigger.to_state.context.user_id.replace('54748845b224c83a169f219164fe8da', 'Papa').replace('967438550bd94d4d8dfec7c26f411175', 'Mama') }} turned {{ trigger.to_state.attributes.friendly_name }} {{ trigger.to_state.state }} via HA UI."
Not completely sure but it seems like new Home Assistant 0.112.x is required for this to work.
Maybe someone finds it useful.
N.B. .context.user_id is undocumented feature and may be changed in future but then just find refer to up to date event structure and update parameter accordingly.