Changing script depending on user

Is there a way to change how a script responded depending on which user clicked the button that fired it?

I have this script, which is triggered by a button in the dashboard. The problem is ‘trigger’ comes up as undefined. I assume this is because only automations have triggers. Is there a way to do this via a script? Or do I need to do it via automation?

Edit: I re-created this as an automation. But if you trigger an automation from the dashboard it appears there is no actual trigger.

sequence:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.lamp_red_wood
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{trigger.to_state.context.user_id ==
              '3e21e5461e4348b8a64e84e1c9470eb5'}}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.lamp_flowers
      - conditions:
          - condition: template
            value_template: >-
              {{trigger.to_state.context.user_id ==
              '9903865ba09946f1a8f9e9e0656f3b9d'}}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.lamp_red_metal
    default:
      - service: light.turn_off
        data: {}
        target:
          entity_id:
            - light.lamp_flowers
            - light.lamp_red_metal
mode: single
alias: Goodnight - Bedroom
icon: mdi:bed

Scripts don’t use triggers so I assume you have a button card that is calling the script.

The above script looks correct to me from an action point of view, if you create the above as an automation and use an actual button helper ‘input_button’ as the trigger then display the button entity in the front end then this will pass the user_id.

Example trigger for a input_button helper:

- platform: state
  entity_id:
    - input_button.your_button

Thanks for your help. I’ve recreated the script as an automation and used the input button helper.

Below is the event fired by the button, which contains the user_id. But the following automation does not work (it goes to default instead of the first condition). Looking at the event the trigger should be defined something like the following, but none work.

trigger.event.context.user_id → dictionary error
trigger.context.user_id → dictionary error
trigger.user_id → no error, but doesn’t work

event_type: call_service
data:
  domain: automation
  service: trigger
  service_data:
    skip_condition: true
    entity_id:
      - automation.goodnight_bedroom
origin: LOCAL
time_fired: "2023-04-19T14:12:59.549269+00:00"
context:
  id: 01GYCXS20W2CFZTKTJSRBBJGJX
  parent_id: null
  user_id: 3e21e5461e4348b8a64e84e1c9470eb5
alias: Goodnight - Bedroom
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.goodnight_bedroom
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.lamp_red_wood
  - choose:
      - conditions:
          - condition: template
            value_template: "{{trigger.user_id == '3e21e5461e4348b8a64e84e1c9470eb5'}}"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.lamp_flowers
      - conditions:
          - condition: template
            value_template: "{{trigger.user_id == '9903865ba09946f1a8f9e9e0656f3b9d'}}"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.lamp_red_metal
    default:
      - service: light.turn_off
        data: {}
        target:
          entity_id:
            - light.lamp_flowers
            - light.lamp_red_metal
mode: single

Why have you changed this:

      - conditions:
          - condition: template
            value_template: >-
              {{trigger.to_state.context.user_id ==
              '9903865ba09946f1a8f9e9e0656f3b9d'}}

To this:

      - conditions:
          - condition: template
            value_template: "{{trigger.user_id == '3e21e5461e4348b8a64e84e1c9470eb5'}}"

The first template condition should be correct - did it not work?
What was the reason you have changed it ?

I get this error
Error: In 'template' condition: UndefinedError: 'dict object' has no attribute 'to_state'

trigger.user_id gives me no error (but also… no result).

This works for me without issue:

  - choose:
      - conditions:
          - condition: template
            value_template: "{{trigger.to_state.context.user_id == '5651510db7cd44c8ba5eb0889e57df8a'}}"
        sequence:
           .....
            
      - conditions:
          - condition: template
            value_template: "{{trigger.to_state.context.user_id == '314efe70654b432ca82e15951654e00d'}}"
        sequence:
          .....
1 Like

Oh my god. I updated the wrong button to use the input button helper. So I spent the afternoon clicking on a button that was triggering the automation directly.
Thank you for your persistence! It now works.