Dynamic Script Responses

I’m trying to return dynamic responses from a script based on conditions within the script itself. The below code is not what I’m actually trying to implement but simplified proof of concept script. When I run this script I do not receive a result. Is something like this possible? Am I doing something wrong?

alias: Test
sequence:
  - if:
      - condition: template
        value_template: "{{ now().second % 2 == 0 }}"
    then:
      - variables:
          response: |
            {{ { 'value': 'even' } }}
      - stop: Seconds were even
        response_variable: response
    else:
      - variables:
          response: |
            {{ { 'value': 'odd' } }}
      - stop: Seconds were odd
        response_variable: response

The issue is variable scope:

while @Didgeridrew is totally right and is the most common mistake in using variables,
in this particular case, in this example, op might be hitting somethign else because they aren’t tryin to change the variable and also they are trying to use it within the same block…

maybe the issue is they need:

response_variable: "{{ response }}" 

?

I’m keeping the variables contained within the control structure though and simply to bailing out with the Stop to send the desired response.

Good idea @armedad but it gives an error in the trace when executed

Executed: June 28, 2024 at 11:04:06 AM
Error: '{{ response }}'
Result:
stop: Seconds were even
error: false

ok, you’re telling me i gotta get off my lazy butt and try it so i can debug it… will do unless someone gets to it first :slight_smile:

I was able to achieve it with the following. But my real script would be more complicated and I was hoping I could use more gui blocks instead of so much hand coded stuff.

alias: Test All Variable
sequence:
  - variables:
      response: |
        {% if (now().second % 2) %}
          {{ { 'value': 'even' } }}
        {% else %}
          {{ { 'value': 'odd' } }}
        {% endif %}
  - stop: ''
    response_variable: response
description: ''

I’d sure appreciate another brain looking at. Everything I’ve read seems to say it should work.

this is the problem, which was in @Didgeridrew’s first link:

But the example in this link shows setting a variable inside the then, which I am doing, and then consuming it in the service, which I would imagine is the same as the Stop.

I’m not trying to pass anything from inside an if, choose, or repeat to outside, I’m still consuming it from within.

yeah, it is strange to me too. and it’s specific just to response_variable. perhaps a good feature request to file

Yeah, seems like an issue with response_variable as this example works.

alias: Example
sequence:
  - variables:
      people: 1
  - if:
      - condition: state
        entity_id: device_tracker.wobani
        state: home
    then:
      - variables:
          persons: "{{ people + 1 }}"
          bob: fred
      - service: notify.notify
        data:
          message: There are {{ persons }} people home named {{ bob }}
  - service: notify.notify
    data:
      message: There are {{ people }} people home
description: ""

I’ll see about opening a feature request. Thanks for your help.

Similar questions were asked in the past and the answer is the same, it’s a scoping restriction. response_variable cannot transport the local variable’s value out of the sequence where the local variable was defined.


The script example in the documentation is different because the local variable’s value is consumed within the sequence where it’s defined. You’re trying to export that value (via response_variable) out of the scope where it’s defined.