[SOLVED] Detect which HA user toggled entity state

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?

No not yet.

There is a hope.

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.

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "light.bulb",
        "old_state": {
            "entity_id": "light.bulb",
            "state": "on",
            "attributes": {
                "friendly_name": "Light bulb",
                "supported_features": 0,
                "icon": "mdi:lightbulb-on-outline"
            },
            "last_changed": "2020-07-04T06:57:29.943770+00:00",
            "last_updated": "2020-07-04T06:57:29.943770+00:00",
            "context": {
                "id": "ac6d870e31b04bb2acb9456309f7f7c1",
                "parent_id": null,
                "user_id": "9478b8d2572e4594b48adb964590bbag"
            }
        },
        "new_state": {
            "entity_id": "light.bulb",
            "state": "off",
            "attributes": {
                "friendly_name": "Light bulb",
                "supported_features": 0,
                "icon": "mdi:lightbulb-on-outline"
            },
            "last_changed": "2020-07-04T06:57:31.559381+00:00",
            "last_updated": "2020-07-04T06:57:31.559381+00:00",
            "context": {
                "id": "1a0baec35d1741f7867d8171f614dd52",
                "parent_id": null,
                "user_id": "9478b8d2572e4594b48adb964590bbag"
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-07-04T06:57:31.559534+00:00",
    "context": {
        "id": "1a0baec35d1741f7867d8171f614dd52",
        "parent_id": null,
        "user_id": "9478b8d2572e4594b48adb964590bbag"
    }
}

I will now have to find most efficient way to use it to have notifications if anyone else is switching lights from HA UI.

In the latest version of Home Assistant (0.112) the username is displayed in Logbook, next to the changed state :slight_smile:

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.

2 Likes

This would be handy for me if it could be used to tell if a command came via Google assistant rather than lovelace or another automation

The logbook identifies when something was turned on from Google Assistant like this:

Living Room Light turned on (Home Assistant Cloud)

And by a user:

Living Room Light turned on (Carlos)

The only thing it doesn’t identify is if a switch was turned on through some automation in HA or manually ( physical button press).

Overall it’s pretty handy.