Variables in value template

Hi new to HA and yaml but not to scripting generally.

I want to use variables as much as possible to make my scripts easy to update if/when entities change.

So I have the following script which is that start of a longer process but at the moment all it does is compare two lamps brightness and then change the brighter one to be the same as the brightness of the dimmer one.

The commented line with explicit entity references works but when I try to use variables it does not.

The variables work correctly later in the script but not in the condition. I’ve found many examples of variables being used in condition templates but not with operators i.e. the “>”

Any suggestions gratefully accepted - thanks

alias: M Bed Lamp Dimming
variables:
  lamp01: "{{ 'light.wiz_rgbw_tunable_b7555e' }}"
  lamp02: "{{ 'light.wiz_rgbw_tunable_b7db80' }}"
  init_lamp01_brightness: "{{ state_attr('light.wiz_rgbw_tunable_b7555e','brightness') | int }}"
  init_lamp02_brightness: "{{ state_attr('light.wiz_rgbw_tunable_b7db80','brightness') | int }}"
sequence:
  - if:
      - condition: template
        value_template: >- 
           {{ "{{ init_lamp01_brightness }}">"{{ init_lamp02_brightness }}" }}
#          {{ state_attr('light.wiz_rgbw_tunable_b7555e','brightness') > state_attr('light.wiz_rgbw_tunable_b7db80','brightness') }}

    then:
      - service: light.turn_on
        target:
          entity_id: "{{ lamp01 }}"
        data:
          brightness: "{{ init_lamp02_brightness }}"
    else:
      - service: light.turn_on
        target:
          entity_id: "{{ lamp02 }}"
        data:
          brightness: "{{ init_lamp01_brightness }}"
mode: single

  • There’s no need to use expressions for static strings.
  • Use your variables to reduce repetition in subsequent variables.
  • Take care with quotation marks. Because of the internal "-marks, the expression:
    {{ "{{ init_lamp01_brightness }}">"{{ init_lamp02_brightness }}" }}
    from your post isn’t asking "Is the brightness of lamp01 greater than that of lamp02, it’s asking "Is the string {{ init_lamp01_brightness }} greater than the string {{ init_lamp02_brightness }}. This will always render false. Without them you would have gotten a syntax error because…
  • Nested templates are invalid, just use the variables
alias: M Bed Lamp Dimming
variables:
  lamp01: light.wiz_rgbw_tunable_b7555e
  lamp02: light.wiz_rgbw_tunable_b7db80
  init_lamp01_brightness: "{{ state_attr(lamp01, 'brightness') | int }}"
  init_lamp02_brightness: "{{ state_attr(lamp02, 'brightness') | int }}"
sequence:
  - if:
      - condition: template
        value_template: >- 
           {{ init_lamp01_brightness > init_lamp02_brightness }}
    then:
      - service: light.turn_on
        target:
          entity_id: "{{ lamp01 }}"
        data:
          brightness: "{{ init_lamp02_brightness }}"
    else:
      - service: light.turn_on
        target:
          entity_id: "{{ lamp02 }}"
        data:
          brightness: "{{ init_lamp01_brightness }}"
mode: single

Also, that’s not how you accomplish comments in a multi-line template… instead, surround the expression in {# #} to comment it out.

1 Like

Hi Drew, many thanks for the comprehensive and clear reply - apart from anything else I obviously went a bit OTT with the punctuation!!
Yes, I was wondering if I could use a variable in subsequent variables but hadn’t attempted it so thanks I’ll be doing that from now on.
Of course - single and double quotes always trip me up!
Right ok comments are like that - I just put the commented line in my posted code for ease of reference but when I need it I’ll do it properly :wink:
Thanks again for your help - really appreciate it :grinning: