Automation condition based on an event and the user which trigger the event

Hi,

I would like to create an automation which runs only when a switch xxx is set (off → on, as an example) by an user which is not myself (mahikeulbody in this case).

In the logbook, I can see message as xxx turned on by mahikeulbody, but I fail to find how to test the user which triggered the switch into an automation condition.

Thanks for any help.

If your automation uses a State Trigger, the following will be available (for use in a template within the condition or action):

trigger.to_state.context.user_id

However, user_id is not the user’s name but a long-winded identifier (string of letters and numbers). Although it’s possible to use it to determine the user’s actual name, that’s not necessary for your application. To determine if the user is not you, just compare their user_id to your user_id. Of course, to do that you must first discover what is your user_id. Create a simple automation that reports the user_id and then trigger it (logged in with your user account).

alias: example 1
trigger:
- platform: state
  entity_id: input_boolean.whatever
  to: 'on'
action:
- service: notify.persistent_notification
  data:
    title: User ID
    message: "{{ trigger.to_state.context.user_id }}"
2 Likes

Thanks, I could get my user_id (7c46721dgbvd42769019066bs3f11883). But I fail to use it. I set the following condition :

condition: template
value_template: >-
  "{{ states('trigger.to_state.context.user_id') != '7c46721dgbvd42769019066bs3f11883' }}"

But the condition seems to be ever false, even triggering the switch from another user.

If I run manually the automation (Execute) logged as mahikeulbody, the condition is true.

I was wrong, the correct syntax is :

condition: template
value_template: "{{ trigger.to_state.context.user_id != '7c46721dgbvd42769019066bs3f11883' }}"
1 Like

I am trying to use the whole context.user_id stuff, but I am seriously missing something!

Here is the result of the entire trigger structure, when I personally turn on a light in lovelace.

[ {'id': '1', 'idx': '1', 'platform': 'device', 'entity_id':
      'light.altanlampah', 'from_state': <state light.altanlampah=off;
      effect_list=['blink', 'breathe', 'okay', 'channel_change',
      'finish_effect', 'stop_effect'], supported_color_modes=['brightness'],
      linkquality=199, power_on_behavior=off, update=state=idle,
      update_available=False, friendly_name=AltanLampaH, supported_features=45 @
      2021-09-01T21:13:34.369730+02:00>, 'to_state': <state
      light.altanlampah=on; effect_list=['blink', 'breathe', 'okay',
      'channel_change', 'finish_effect', 'stop_effect'],
      supported_color_modes=['brightness'], color_mode=brightness,
      brightness=255, linkquality=199, power_on_behavior=off, update=state=idle,
      update_available=False, friendly_name=AltanLampaH, supported_features=45 @
      2021-09-01T21:13:36.577907+02:00>, 'for': None, 'attribute': None,
      'description': 'state of light.altanlampah'} ]

Where is the context id??

I wanted to try out this user id to change a input text but it doesn’t seem to chabge the text?


choose:
  - conditions:
      - condition: template
        value_template: >-
          {{ trigger.to_state.context.user_id !=
          'd7572c27e0e943489edd369fda218ae4' }}
    sequence:
      - service: input_text.set_value
        target:
          entity_id: input_text.hvem_stovsuger
        data:
          value: André