Script service response returns empty variable

Hello, I need some help on this one. My goal is to call this script, get a number back and then adjust the covers positions.
I started like this and it works as expected:

alias: Cover position
sequence:
  - variables:
      response:
        position: 1
  - stop: Returns variable successfully
    response_variable: response
mode: single
icon: mdi:window-shutter-auto

image

Now I want to perform some checks and change the value based on conditions, a simple one:

alias: Cover position
sequence:
  - variables:
      response:
        position: 1
  - if:
      - condition: template
        value_template: "{{ 1 == 1}}"
    then:
      - variables:
          response:
            position: 50
      - stop: Fails to return the updated variable 
        response_variable: response
  - stop: Returns variable successfully
    response_variable: response

And the response is empty:
image

When I remove the stop inside the If, the returned value will be 1 since the Stop is in another block and the variable value won’t be available.

When leaving only the stop inside the If it also returns empty.

By adding a step to send a notification, the updated variable appears correctly but the response remains empty:

alias: Cover position
sequence:
  - variables:
      response:
        position: 1
  - if:
      - condition: template
        value_template: "{{ 1 == 1}}"
    then:
      - variables:
          response:
            position: 50
      - service: notify.persistent_notification
        data:
          message: "{{response.position}}"
      - stop: Fails to return the updated variable
        response_variable: response

image

So, is there any way to make this work? It seems that the stop fails to get the correct variable whenever I add any Choose or If even when it’s nested together or I’m missing something.

Thanks in advance

2 Likes

Since I added the Stop inside the If, right after defining the variable, don’t they have the same scope?

Variables in scripts and automation are almost useless. I’m having the same issue. It appears if it’s inside any conditional branching statement that the response variables are blank. It’s utterly idiotic.

I just ran into this same problem, I believe that it is due to the conditional statement as well, but even putting “stop” at the very end after the condition, it still isn’t working. Pulling my hair out!

There is an open issue for this problem.

In the meantime, the problem can often be worked around by doing the logic tasks in a template instead of by using If/Then or Choose Actions. This won’t work for all use cases, but it’s an approach to consider until the issue is resolved.

1 Like

Oh my god, thank you so much. I’ve been pulling my hair out for days trying to make this script work and I thought I just wasn’t understanding the documentation