Using variables in templates

Hi,

I’m using the visual editor to define variables (livingroom1, livingroom2, kitchen) so I can later use them to pass some data to a message. I does not seems to work. I do not get any value within the message.

Why?

alias: סגירה חלקית של תריסי סלון ומטבח בצהריים
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 49
    below: 50
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 29
    below: 30
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 14
    below: 15
condition:
  - condition: state
    entity_id: sensor.sun_solar_rising
    state: "False"
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sun_solar_elevation
            above: 49
            below: 50
        sequence:
          - device_id: c03d5c86e45151e5139cac8e34fc169b
            domain: cover
            entity_id: 43804277317ba51547532f333c01952f
            type: set_position
            position: 50
          - device_id: 26ed077e21fd555f5e2775e52451b221
            domain: cover
            entity_id: 4917d47abf7807d46b3ffe0e6f69473d
            type: set_position
            position: 75
          - device_id: 7f5c7fb313f2a6c19795d1a67c30a755
            domain: cover
            entity_id: 557dbd56a4632735d4f2ee04fccc9efe
            type: set_position
            position: 40
          - variables:
              livingRoom1: 50
              livingRoom2: 75
              kitchen: 40
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sun_solar_elevation
            above: 29
            below: 30
        sequence:
          - device_id: c03d5c86e45151e5139cac8e34fc169b
            domain: cover
            entity_id: 43804277317ba51547532f333c01952f
            type: set_position
            position: 25
          - device_id: 26ed077e21fd555f5e2775e52451b221
            domain: cover
            entity_id: 4917d47abf7807d46b3ffe0e6f69473d
            type: set_position
            position: 50
          - device_id: 7f5c7fb313f2a6c19795d1a67c30a755
            domain: cover
            entity_id: 557dbd56a4632735d4f2ee04fccc9efe
            type: set_position
            position: 25
          - variables:
              livingRoom1: 25
              livingRoom2: 50
              kitchen: 25
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sun_solar_elevation
            below: 15
            above: 14
        sequence:
          - device_id: c03d5c86e45151e5139cac8e34fc169b
            domain: cover
            entity_id: 43804277317ba51547532f333c01952f
            type: open
          - device_id: 26ed077e21fd555f5e2775e52451b221
            domain: cover
            entity_id: 4917d47abf7807d46b3ffe0e6f69473d
            type: open
          - device_id: 7f5c7fb313f2a6c19795d1a67c30a755
            domain: cover
            entity_id: 557dbd56a4632735d4f2ee04fccc9efe
            type: open
          - variables:
              livingRoom1: 100
              livingRoom2: 100
              kitchen: 100
  - service: telegram_bot.send_message
    metadata: {}
    data:
      message: >
        זוית השמש: {{ state_attr("sun.sun", "elevation") }}°

        מעדכן פתיחת תריסים:

        סלון 1: {{ state_attr("cover.1_blinds_livingroom1", "current_position")
        }}% -> {{ livingRoom1 }}%

        סלון 2: {{ state_attr("cover.1_blinds_livingroom2", "current_position")
        }}% -> {{ livingRoom2 }}%

        מטבח: {{ state_attr("cover.1_blinds_kitchen", "current_position") }}% ->
        {{ kitchen }}%
mode: single

Variables are local to the block they are defined in. So will not be available outside the - sequence: Likewise if you define the variables at the top level, like just below action:, then any changes you make to these variables in a sub sequence will not be persisted outside these sequences.

Your variables are local to Choose option where you are defining them, they aren’t going to be available in the next action.

In this case you could just move the variables to their respective triggers and get rid of the Choose action:

alias: סגירה חלקית של תריסי סלון ומטבח בצהריים
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 49
    below: 50
    variables:
      livingRoom1: 50
      livingRoom2: 75
      kitchen: 40
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 29
    below: 30
    variables:
      livingRoom1: 25
      livingRoom2: 50
      kitchen: 25
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 14
    below: 15
    variables:
      livingRoom1: 100
      livingRoom2: 100
      kitchen: 100
condition:
  - condition: state
    entity_id: sensor.sun_solar_rising
    state: "False"
action:
  - service: cover.set_cover_position
    target:
      entity_id: 43804277317ba51547532f333c01952f
    data:
      position: "{{ livingRoom1 }}"
  - service: cover.set_cover_position
    target:
      entity_id: 4917d47abf7807d46b3ffe0e6f69473d
    data:
      position: "{{ livingRoom2 }}"
  - service: cover.set_cover_position
    target:
      entity_id: 557dbd56a4632735d4f2ee04fccc9efe
    data:
      position: "{{ kitchen }}"           
  - service: telegram_bot.send_message
    metadata: {}
    data:
      message: >
        זוית השמש: {{ state_attr('sun.sun', 'elevation') }}°

        מעדכן פתיחת תריסים:

        סלון 1: {{ state_attr('cover.1_blinds_livingroom1', 'current_position')
        }}% -> {{ livingRoom1 }}%

        סלון 2: {{ state_attr('cover.1_blinds_livingroom2', 'current_position')
        }}% -> {{ livingRoom2 }}%

        מטבח: {{ state_attr('cover.1_blinds_kitchen', 'current_position') }}% ->
        {{ kitchen }}%
mode: single
1 Like

It didn’t work so I tried to simplified it just to see what is going on:

alias: TEST VARS
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sun_solar_elevation
    above: 49
    variables:
      livingRoom1: 66
      livingRoom2: 66
      kitchen: 66
condition:
  - condition: state
    entity_id: sensor.sun_solar_rising
    state: "False"
action:
  - service: cover.set_cover_position
    metadata: {}
    data_template:
      position: "{{livingRoom1 | int}}"
      device_id: c03d5c86e45151e5139cac8e34fc169b
    enabled: true
  - service: telegram_bot.send_message
    metadata: {}
    data:
      message: >
        זוית השמש: {{ state_attr("sun.sun", "elevation") }}°

        מעדכן פתיחת תריסים:

        סלון 1: {{ state_attr("cover.1_blinds_livingroom1", "current_position")
        }}% -> {{livingRoom1}}%

        סלון 2: {{ state_attr("cover.1_blinds_livingroom2", "current_position")
        }}% -> {{ livingRoom2 }}%

        מטבח: {{ state_attr("cover.1_blinds_kitchen", "current_position") }}% ->
        {{ kitchen }}%
mode: single

I'm getting:

> Error: Error rendering data template: UndefinedError: 'livingRoom1' is undefined

Are you attempting to test it using the “Run” button or automation.trigger service? Both of those will completely skip the trigger block, so the variables have no defined value.

Troubleshooting - Testing Your Automation

Oh, I didn’t know that. I was trying to test it using the ‘Run’ button.
What is the best way to test it then?

Use the States tool in Developer Tools to find the trigger entity, click the name, then change the state (at the top) to something that’ll trigger the automation before clicking Set State.

1 Like

tried to move the variables in the root instead of the trigger?

Not the way they are using them because of this: https://www.home-assistant.io/docs/scripts#scope-of-variables

They are changing the variables in a nested sequence and expecting them to retain those changed values outside the nested sequence. That will never work because of the local scope.

1 Like