Userchecking in an automation (maybe via IF-Clause)

I’m trying to have a card, that when clicked triggers an automation that starts a PC. However the PC started should be relative to the user who clicked the card. It’s not about starting the PC, that one works. It’s about getting the template to find out which user pressed the button. My problem is that it always returns true even if I check for a username that I (the tester) don’t have. (My username is Jo not “name”). For refrence I have a picture of the automation testing telling me “true”, wich is the green text (gerrman for “condition true”).


Thank you very much for your help.
With regards
Jo

Try this

{% if trigger.to_state.context.user_id == "name" %}
  ...
{% else %}
  ...
{% endif %}

Note that context user_id is only relevant for services or automations triggered by a user.

Thx. But now I receive this error.
In ‘template’ condition: UndefinedError: ‘trigger’ is undefined

What are you using as a trigger?

Rn im just using the test button next week to the template

So there will be no trigger and thus no trigger context to obtain a user id.

And where do i have to put your code then (like in an automation, in a dashboard…)

In a template condition in your automation.

I now have the follwoing automation, with a buton that has the following action (and it doesn’t send me a notification on my smarphone):
Automation :

> alias: Pc
> description: ""
> trigger: []
> condition: []
> action:
>   - if:
>       - condition: template
>         value_template: |
>           {% if trigger.to_state.context.user_id == "Jo" %}
>             {{true}}
>           {% else %}
>             {{false}}
>           {% endif %}
>     then:
>       - service: notify.mobile_app_iphone_12
>         data:
>           message: Test
> mode: single

Action:

> tap_action:
>   action: call-service
>   service: automation.trigger
>   target:
>     entity_id: automation.pc
>   data:
>     skip_condition: false

You don’t have a trigger in that automation. Thus you won’t have any trigger context.

Also why do all your line begin with > ?

And what should my trigger be if I want to trigger it via a button?
the > is just because of the formating here in the forum in yaml it’s normal

Please read this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

There is no need to start every line with >.

Ok I apologise.
Here is my code again just this time labeled as a yaml code
However I still didn’t quite get your “missing trigger” point. I don’t want the automation to go off with a trigger. I want the automation to trigger with a card action, therefore I don’t know what trigger to use.
Action part:

tap_action:
  action: call-service
  service: automation.trigger
  target:
    entity_id: automation.pc
  data:
    skip_condition: false

Automation:

alias: Pc
description: ""
trigger: []
condition: []
action:
  - if:
      - condition: template
        value_template: |
          {% if trigger.to_state.context.user_id == "Jo" %}
            {{true}}
          {% else %}
            {{false}}
          {% endif %}
    then:
      - service: notify.mobile_app_iphone_12
        data:
          message: Test
mode: single

This is a missing trigger:

Use a script ?

Good point.
I’ve transferd the automation to a script. However it still doesn’t work.
My action call:

tap_action:
  action: call-service
  service: script.pc_starts
  target: {}

The script:

alias: PC Starts
sequence:
  - if:
      - condition: template
        value_template: |
          {% if trigger.to_state.context.user_id == "Jo" %}
            {{true}}
          {% else %}
            {{false}}
          {% endif %}
        enabled: true
    then:
      - service: notify.mobile_app_iphone_12
        data:
          message: Test
    enabled: true
mode: single

It doesn’t work because it’s now a script, not an automation, and scripts don’t have triggers, only automations have triggers, so the trigger variable is always undefined in a script.

If my explanation is unclear, I suggest you review the documentation for Automation Trigger Variables.

The fact is you can only determine context.user_id from the trigger variable, which exists exclusively in an automation that has been triggered by one of its triggers. So far, nothing you have created meets those requirements.


FWIW, I may be wrong but I don’t think you can determine who pressed a standard-issue Button card. You can determine who pressed an Input Button.

@JoDeer Right…this works…

  • Create button helper f.e. input_button.testing
  • Add the button to a page
show_name: false
show_icon: true
type: button
tap_action:
  action: toggle
entity: input_button.testing
icon: mdi:weather-cloudy-alert
  • Create an automation triggered on the button
alias: "@@testing"
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.testing
condition:
  - condition: template
    value_template: >-
      {{ trigger.to_state.context.user_id ==
      'c4321b2f12e24d599b2e2c5dd08abddd'}}
action:
  - service: notify.mobile_app_myiphone
    data:
      message: "{{trigger.to_state.context.user_id }}"
mode: single

Trace:

Executed: May 24, 2023 at 21:13:02
Result:
params:
  domain: notify
  service: mobile_app_myiphone
  service_data:
    message: c4321b2f12e24d599b2e2c5dd08abddd
  target: {}
running_script: false
limit: 10

Note, .user_id is not a name, but the ID (in my case c4321b2f12e24d599b2e2c5dd08abddd).
You can find your ID by opening the user in http://[my.HA.add.ress]:8123/config/users

PS: There is no need for if/then as it is the nature of a condition that it must be true in order to continue the automation

2 Likes

Nice! I knew Input Button can reveal (via State Trigger) who pressed it but didn’t think of using a Button card to toggle it.

Thank you very much!!!
This one works like a charm!