Complex condition in template in jinja

I am trying to figure out if it is possible to change icon based on complex parameters.
I have door sensor and lock and I am trying to chcnge icon based on states of these two combined.

Simple thing like this, if door is open show one icon, if closed show another is easy.

{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off')  %}
mdi:door
{% else %}
mdi:door-open
{% endif %}

BUT is it possible to make complex condition something like this
if door sensor closed AND lock is locked show icon mdi:door-closed-lock
if door is closed AND lock is unlocked show mdi:door
if door is open show mdi:door-open

adding simple AND and second condition doesn’t work, I have not found anything in documentation although it mentions it supports and.

Most likely its a syntax problem on my side.

Thanks for help

{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off')  %}
  {% if is_state('binary_sensor.YOUR_LOCK', 'on') %}
    mdi:door-closed-lock
  {% else %}
    mdi-door
  {% endif %}
{% else %}
  mdi:door-open
{% endif %}
{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off')  and is_state('binary_sensor.lock', 'on')  %}
    mdi:door-closed-lock
{% elif is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off')  %}
    mdi:door
{% else %}
    mdi:door-open
{% endif %}

Thanks both of you for super fast response.

I was typing uppercase AND, and it must be lowercase, stupid me…

Shortest I can get it:

mdi:door{{iif(is_state('binary_sensor.YOUR_LOCK','on'),'-closed-lock',iif(is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening','on'),'-open',''))}}

Thanks, but there is third state closed but unlocked, how to add that into this short version?

It’s already there. Spaced out a bit and with a note that this is iif (docs) behaving like the nested if above:

mdi:door
{{ iif(is_state('binary_sensor.YOUR_LOCK','on'),
       '-closed-lock',
       iif(is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening','on'),
           '-open',
           ''
          )
      )
}}

I see now, thanks.

But this would only work if the icon name can be constructed from the strings that have at least several leftmost characters in common, correct?

For changing for example colors I need to use the if and version, correct?

Not necessarily: that was just to make it shorter. I could have written:

{{ iif(is_state('binary_sensor.YOUR_LOCK','on'),
       'mdi:door-closed-lock',
       iif(is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening','on'),
           'mdi:door-open',
           'mdi:door'))
}}

…with the proviso that in a YAML configuration, a single-line template without leading characters must be quoted. For example:

# this is OK
  icon_template: mdi:door{{ '-open' }}

# this is OK
  icon_template: "{{ 'mdi:door-open' }}"

# this is not OK
  icon_template: {{ 'mdi:door-open' }}

# but this is OK
  icon_template: >
    {{ 'mdi:door-open' }}

Thanks a million, its a lot more clear to me now.

1 Like