Hello,
I would like to use a variable in an automation from a script but not managing to do that. Could you give me some help?
Automation looks like this:
Variables have scope local to the script. If you want a script to return a variable you need to use the stop action with the response_variable option. See this portion of the docs.
The response variable must be a mapping of key-value pairs. And in your script, the value you assign to response_variable is the name of the variable to return. Which needs to be defined elsewhere in the script.
In the script, here’s an example that should work for the the part of the first if statement:
No success yet push notification returns now without quotes but not giving back the variable. However if statement in the script fulfills. Do you have any idea? Thank you in advance.
Well I learned something today, but that seems like a bug that needs to be fixed. Or if it’s not a bug it needs to be documented that the stop action’s response_variable option can only be used at the first level of the automation or script. Which is unique to the stop action; other actions have no problem using locally scoped variables.
Here’s a test I put together to confirm. Running with the condition template set to true it will send a notification that the variable is set to if_statement however the stop action returns nothing. Changing the template to false will show the variable is set to main_level and the stop action correctly returns {"value": "main_level"}
alias: Test variable scope
description: ""
sequence:
- variables:
my_var:
value: main_level
- if:
- "{{ true }}"
then:
- variables:
my_var:
value: if_statement
- action: notify.persistent_notification
data:
message: In "if" statement my_var is {{my_var.value}}
- stop: If
response_variable: my_var
- action: notify.persistent_notification
data:
message: At main level my_var is {{my_var.value}}
- stop: Main
response_variable: my_var
mode: single
I’ll file a bug report later this week and see where it goes.
Edit: 99352 opened a year ago and went stale and got closed 6 months ago.
It seems in general the stop action was a poorly-thought-out implementation but nobody seems to want to fix it. Other stale and closed issues: 116429104218
You can try something like this in your script. This uses your original logic, which was to provide only the first matching switch in the order they were listed. This code could be compacted but I tried to separate out the variables into chunks so its more clear what each step does, and it should be easier to modify if you decide you want to take action on more than one switch at a time. They key thing here is that, as @Didgeridrew suggested, the logic is performed in a template and exists at the main level of the script. The stop action also appears at the main level of the script. So the response variable is correctly returned.
This script will return a response {'value': 0} if there is no matching switch that is in the ON state.
script:
test_var_pass:
alias: Test to see how to pass a variable
sequence:
- variables:
entity_dict:
switch.switch_one: 1
switch.switch_two: 2
switch.switch_three: 3
entity_on_dict: >
{% set ns = namespace(output_kvps=[(None, 0)]) %}
{% for k, v in entity_dict.items() %}
{% if states(k) == 'on' %}
{% set ns.output_kvps = ns.output_kvps + [(k, v)] %}
{% set ns.output_kvps = ns.output_kvps | rejectattr(1, 'eq', 0) | list %}
{% endif %}
{% endfor %}
{{ dict.from_keys(ns.output_kvps) }}
lowest_value_match_number: "{{ entity_on_dict.values() | min }}"
lowest_value_match_entity: "{{ entity_on_dict.items() | selectattr(1, 'eq', lowest_value_match_number) | map(attribute=0) | first }}"
response:
value: "{{ lowest_value_match_number }}"
- if:
- condition: template
value_template: "{{ lowest_value_match_number != 0 }}"
then:
- action: switch.turn_off
target:
entity_id: "{{ lowest_value_match_entity }}"
- stop: Completed
response_variable: response
Perhaps in the future there will be a bug fix where we don’t have to go through this effort and can use the stop action w/ response variable at indented levels in the code.