I have a button on my dashboard that start a script. The script call a service
- service: notify.mobile_app_{{ }}
I need to figure out how to select a phone based on who pressed the UI button, can someone help me on this please?
I have a button on my dashboard that start a script. The script call a service
- service: notify.mobile_app_{{ }}
I need to figure out how to select a phone based on who pressed the UI button, can someone help me on this please?
You have to add these variables in the action section of your script, using a variables action. You’ll have to fill in the user_id’s that are attached to your person and link them to the mobile app notify.
The context is only available after action section starts running, so you can’t place this in the variables section.
- variables:
user_id: |
{{ context.user_id }}
config:
9fd7bf32addb4201a0caade78f99b094: notify.mobile_app_myphone
b853a8717d9648e3990315b4fea390b5: notify.mobile_app_sarasphone
notify_service: |
{{ device.get(user_id) }}
then later on in your action section, you should use a choose so that users that aren’t configured won’t cause the script to error.
- choose:
- conditions:
- condition: template
value_template: "{{ notify_service is not none }}"
sequence:
- service: "{{ notify_service }}"
data:
message: "Blah"
Now, if by chance your notify service is called notify.mobile_app_<person.name>, then you can do the following…
- variables:
user_id: >
{{ context.user_id }}
target_person: >
{{ states.person | selectattr('attributes.user_id', 'eq', user_id) | map(attribute='entity_id') | first | default }}
notify_service: >
{{ 'notify.mobile_app_' ~ slugify(state_attr(target_person, 'friendly_name')) if target_person is not none else None }}
with the same choose condition above.
I’m in awe.
First of all, thank you for joining the conversation petro, I’m really thankful for all this syntax I can learn from and work on!
There’s some logic error in my code (the syntax is not reporting problems) and I can’t figure out why I can’t get the notification:
alias: Find my device
sequence:
- service: "{{ notify_service }}"
data:
message: "test"
variables:
user_id: |
{{ context.user_id }}
config:
9fd7bf32addb4201a0caade78f99b094: notify.mobile_app_myphone
b853a8717d9648e3990315b4fea390b5: notify.mobile_app_sarasphone
notify_service: |
{{ config.get(user_id) }}
mode: single
I tried to use traces section to get some clue but there’s no activity there. May I ask you some troubleshooting tips? Like a good debug playbook I can use in cases like this?
ps: I omitted the condition part just to test it out first
change your mode to parallel
. Try to run the script, when you do, you can look at the variables tab to see what was generated… e.g.
I changed to mode: parallel
but I still get this run error:
Failed to call service script/1687178280235. UndefinedError: 'context' is undefined
edit: that’s because I run the script from the script page right?
Correct. You can’t test this from that page. You have to call it via a service call.
You’ll have to look in the logs for errors.
Got it: I can check the traces for automation/script info about each step executed and then the logs to get verbosity on a error.
Anyway for testing purpouse I did 6 more type of service calls of that script:
myphone
sarasphone
Developers Tools/Service
section and from dashboard button, and every single time I get three errors in the logs:context may only be available after the action sequence starts, meaning you’ll have to put the user_id and notify_service variable in a variables action right before the choose.
- variables:
user_id: |
{{ context.user_id }}
notify_service: |
{{ config.get(user_id) }}
that was it! it works, thank you so much, I’ll be glad to donate you a beer fellow domotizer =D
sequence:
- variables:
user_id: |
{{ context.user_id }}
config:
9fd7bf32addb4201a0caade78f99b094: notify.mobile_app_myphone
b853a8717d9648e3990315b4fea390b5: notify.mobile_app_sarasphone
notify_service: |
{{ device.get(user_id) }}
- service: "{{ notify_service }}"
data:
message: "test"
I’ll update my original post to reflect the proper sequence and then I’ll mark that as the solution.
Many thanks @Azertooth and @petro you solved my problem!!!
Let me just add I found a problem because of variable names.
I think the final full version should be:
sequence:
- variables:
user_id: |
{{ context.user_id }}
config:
9fd7bf32addb4201a0caade78f99b094: notify.mobile_app_myphone
b853a8717d9648e3990315b4fea390b5: notify.mobile_app_sarasphone
notify_service: |
{{ config.get(user_id) }}
- service: "{{ notify_service }}"
data:
message: "test"
the notify_service variable declared as below led me to error
notify_service: |
{{ device.get(user_id) }}
in the end in my case it worked with
notify_service: |
{{ config.get(user_id) }}
Many thanks again: I very much needed to notify only the person executing four of my scripts!!!
Bye