Track who and when pressed a button in UI dashboard

Hi,

I am looking for a way to track who and when pressed a button in UI. It would be best if I could see that in journal.

Is there an option to enable that?
or is there a workaround with scripts etc.

I am in situation in which multiple people can control specific device.
I would like to know who triggered an action when no one remembers who did it.

Best regards!
Headscarf

There are many posts about this subject on the forum. You can get who performed an action from the context:

If you want to get the name from it, here’s an example how:

Hi @Edwin_D,

I know I can extract that info and use it for example in notification message.

I am looking for something else: a history of clicks. How could I build a trace/history of clicks? Best case scenario would be similar to Zigbee buttons that have history of clicks.

Do you have any idea how could I achieve this?

You could log using logbook.log or set up a syslog server and log to that. But you need to set it up per entity. I do not know how to log everything of everything. You could look at events Home Assistant emits, but I believe you are not supposed to listen using a wildcard on everything.

Ok, I made it work with a workaround.

What I did to achieve this:

  1. I have created a text_input helper named helper_button_history_<name>
  2. I have created a script wrapper_button_tap_<name>
  3. In this script I have inserted two actions:
    1. Update text_input helper with name from script’s context
    2. Call an action that I need
  4. I have updated button in my UI daschoard to call this script instead of an action (3.2)

Script:

alias: wrapper_button_tap_<name>
sequence:
  - service: input_text.set_value
    data:
      value: >-
        {% set x = context.user_id %} {{ expand(states.person) |
        selectattr('attributes.user_id', 'eq', x) |    
        map(attribute='attributes.friendly_name')|first }}
    target:
      entity_id: input_text.helper_button_history_<name>
  - service: esphome.<my_custom_service>
    data: {}
mode: single
icon: mdi:gesture-tap

Thanks to this I have a history in journal as I wanted:

With this approach I upload a name of person that triggered this script (which I only use with this button) and now I know who called a specific action or set of actions.

Thanks @Edwin_D. When I was responding to your last comment I got a spark of an idea :smiley:

Why not use the logbook.log action that I mentioned before? Then you do not need to create a helper and you can log any text you need. You can use any information from the trigger variable (such as the entity id) or the context to create whatever message you like.

triggers:
  - trigger: state
    entity_id:
      - light.one
      - light.two
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
conditions: []
actions:  
  - action: logbook.log
    data:
      name: My logbook entry
      message: >-
        Changed by {% set x = trigger.to_state.context.user_id %} {{
        expand(states.person) | selectattr('attributes.user_id', 'eq', x) |    
        map(attribute='attributes.friendly_name')|first }}
      entity_id: "{{ trigger.entity_id }}"

You are right. That would be better approach

Edited to fix using the wrong context. Should work now