Else IF Command?

Having a shot in the dark guess here on how to use else if and was wondering if someone can point me in the right direction:

test:
  sequence:
  - alias: Message Test
    service: notify.knotty
    data:
      message: >
        {% if is_state('sensor.door_count' > '1') %}
          There are currently no doors open.
        {% elseif is_state('sensor.door_count' = '1') %}
          There is currently one door open.
        {% else %}
          There are currently {{ states("sensor.door_count")}} doors open.
        {% endif %}
1 Like

I’m not the expert here but it is

elif
not
elseif

1 Like

Let’s give that a try.

Edit: Nope still doesn’t work for me.

test:
  sequence:
  - alias: Message Test
    service: notify.knotty
    data:
      message: >
        {% if is_state('sensor.door_count' > '1') %}
          There are currently no doors open.
        {% elif is_state('sensor.door_count', '1') %}
          There is currently one door open.
        {% else %}
          There are currently {{ states("sensor.door_count")}} doors open.
        {% endif %}
1 Like

Right, so I now get that the elif isn’t what I’m doing wrong it’s defining the query.

So how do I correctly do the following

  • check if state of sensor.door_count is less than 1
  • check if state of sensor.door_count is equal to 1
  • check if state of sensor.door_count is greater than 1
test:
  sequence:
  - alias: Message Test
    service: notify.knotty
    data_template:
      message: >
        {% set count = states('sensor.door_count')|int %}
        {% if count < 1 %}
          There are currently no doors open.
        {% elif count == 1 %}
          There is currently one door open.
        {% else %}
          There are currently {{ count }} doors open.
        {% endif %}

EDIT: Fixed bug - changed data: to data_template:.

3 Likes

You’ll notice in @pnbruckner solution above that he specifically converts the states value to an integer. In your template, you were comparing the state value to a string. Just something to keep an eye out for going forward!

2 Likes

Yep. In this particular automation he (probably) could have also gotten away with changing:

is_state('sensor.door_count' > '1')

to

is_state('sensor.door_count', '0')

but in general it’s better to convert state strings to an int or float before doing any sort of numeric comparison.

1 Like

I also thought that you need to use data_template: instead of data: if using a template in the data block. At least that’s what the templating docs say:

Am I misunderstanding something?

Nope, you’re exactly right. I just looked right past that when I saw the problem with the template. :blush: I’ll go back and update my reply. Thx!

1 Like

Thank you and @martokk, thank you too.

I’ll give this a go tonight.

Edit: Works a treat, thank you again.

@Knottyboy, I don’t understand why your second attempt in this post, failed ?
It cannot be because elif was incorrect?
Was it because a nested if-then-elif-then-else can only handle test against a simple variable, and not a complex expression like is_state( ) ?
Or is it because there was a syntax error somewhere in your expression?

There were two reasons. First he needed to use data_template instead of data. Second, because the function is_state requires two parameters – an entity_id and a state. You can’t just decide to ignore that and use something invalid like is_state('sensor.door_count' > '1').

Aha - I see. Thanks for enlightening me :smiley:

1 Like