Testing variables (in a script) using `contains`

Hey all,

I know the following works (using == or in):

{% set scene = '1_hallway' %}
{{ scene == '1_hallway' }}
{{ scene in ['1_hallway','2_some_on'] }

However, I’d like to use contain (as below):

{% set scene = '1_hallway' %}
{{ scene contains '1' }}

However, this results in TemplateSyntaxError: expected token 'end of print statement', got 'contains):

I’ve also tried this (which results in unknown):

{% set scene = '1_hallway' %}
{{ states(scene,'contains','1') }}

Is using contains (or match, search) not possible with variables or am I missing a trick?

contains checks if an item exists within a list (or string; it needs to be iterable).

Example 1

{% set scene = '1_hallway' %}
{{ scene == '1_hallway' }}
{{ scene in ['1_hallway','2_some_on'] }}

{{ ['1_hallway','2_some_on'] | contains(scene) }}

image

Example 2

{% set scene = '1_hallway' %}
{{ scene | contains('1') }}

image

2 Likes

thanks - helpful as usual!

1 Like

follow up: I was going to use a list, but, if so, I should always use your first example?

i.e. example 2 isn’t for lists?

{{ scene | contains('1') }}

edit: I should rephrase my question:

I’m looking for something like this (and I know this doesn’t work) where I can check to see if either ‘1’ or ‘2’ is in the variable.

{{ scene | contains('1'|'2') }}
{{ scene is search('1|2') }}
1 Like