Script variable in template {{ states.input_boolean.{{ var }}.state == 'on' }}

Hello,

In my push notifications I’m using this template

    - condition: template
      value_template: >
        {{ states.input_boolean.state_push.state == 'on' }}

In need to pass variable defined in script for something like:

    - condition: template
      value_template: >
        {{ states.input_boolean.{{script_variable}}_push.state == 'on' }}

I’ve tried all possible options come to my mind and found everywhere but no luck.

There’s no way that will work using the states.statename.state format. I think you should be able to do it by joining a set of strings using is_state("statename", "on") though.

{% set ent = "input_boolean."~script_variable~"_push" %}
{{ is_state(ent, 'on') }}

Though the state object method is not preferred, with it you would use bracket notation:

{{ states.input_boolean[script_variable~'_push'].state == 'on' }}
1 Like

Thank You So much, I owe You !!!

Actually, both above solutions works.

I’ve figured out my self something simmilar like

{{ states.input_boolean[script_variable~'_push'].state == 'on' }}

but without “~” so details matters !

Code worked like a charm !

Home Assistant is the best automation OS, and have great community !!!

Another way to build the string using printf-style formatting (docs):

{{ is_state('input_boolean.%s_push' % script_variable, 'on' }}

Thank’s a lot guys, i didn’t expected my issue will bee so interesting.
I appreciate Your help.