Can't figure out how to do if/else loops in scripts

Hey everyone! I’m just having a lot of trouble with if/else loops. I’ve followed the syntax in this example script and modified it to suit my needs but I don’t think I have it right. Here’s my code. I’m really at a loss here. Thanks in advance! :slight_smile:

rain: #This changes the colour of the first den chandelier bulb depending on what the possibility of rain is
  sequence:
  - service: scene.turn_on
    data_template:
      entity_id: >
        {% if is_state('sensor.dark_sky_precip_probability_1d, < 30) %}
          scene.30rain
        {%-elif is_state('sensor.dark_sky_precip_probability_1d, > 30) %}
          scene.70rain
        {%-elif is_state('sensor.dark_sky_precip_probability_1d, > 70) %}
          scene.70rain
        {% endif %}

Hey, thank you! I’m just having a small problem with that code. Once we sort that out I assume the syntax we’ve got will apply to the rest of the code?

When I put that into Configurator I get this:

image

which tells me something’s broken. Please advise. Cheers :slight_smile:

Edit: nvm, I think it’s because I have it commented out. Hang tight.

Edit 2: Yes, it was because I had it commented out.

Moving on.

Okay, I’m going to assume I just use that same syntax across the rest of my code, eh?

Oh, I looked right over that part. Try:

rain: #This changes the colour of the first den chandelier bulb depending on what the possibility of rain is
  sequence:
  - service: scene.turn_on
    data_template:
      entity_id: >
        {% if states('sensor.dark_sky_precip_probability_1d')|float < 30 %}
          scene.30rain
        {%-elif states('sensor.dark_sky_precip_probability_1d')|float > 30 %}
          scene.70rain
        {%-elif states('sensor.dark_sky_precip_probability_1d')|float > 70 %}
          scene.70rain
        {% endif %}

Having said that, this doesn’t make much sense. Is this what you meant?

rain: #This changes the colour of the first den chandelier bulb depending on what the possibility of rain is
  sequence:
  - service: scene.turn_on
    data_template:
      entity_id: >
        {% if states('sensor.dark_sky_precip_probability_1d')|float < 30 %}
          scene.30rain
        {% else %}
          scene.70rain
        {% endif %}

Honestly this is the first script I’ve written. I’m not totally clear on what I’m doing yet. Thank you so much for your help. :smiley:

The second script you have there is much more elegant. I’ll go with that and modify the rest of my code to be the same. I’ll hit up this thread if I have any more issues. Thanks again! That really helped me out! :smile:

1 Like

Alright, I’ve got another problem. How do I make it so a float can be a range of numbers, ie. 20.0-24.9? My floundering Googling isn’t telling me what I need.

It depends on what you’re overall trying to do. You can do this:

{% if 20 < states('sensor.dark_sky_precip_probability_1d')|float <= 24.9 %}

But my guess is you’d want to do something like this:

{% set value = states('sensor.dark_sky_precip_probability_1d')|float %}
{% if value < 25 %}
  aaa
{% elif value < 50 %}
  bbb
{% elif value < 75 %}
  ccc
{% else %}
  ddd
{% endif %}
1 Like

Ok, just want to make sure I understand the second code block you’ve got there. It goes:

If the float is under 25, aaa fires. If it’s under 50 but over 25, bbb fires. Etc. and so on until the end of the loop. Do I have that right?

An example of what I’m trying to do is have this happen at 10pm every night: “aaa” will fire if the temp forecast for the next day is under 10°C. “bbb” will fire if it’s between 10-15°C. “ccc” will fire between 15-20°C and so on up to 35-40°C. I just want to make sure the right scene will fire and not have it glitch out not knowing which scene to fire depending on the float.

I hope that makes sense!

I wouldn’t use the term “fire.” When the value is less than 25 the template evaluates to aaa (which, in the larger sense of the script, yes, it turns on that scene.) If the value is less than 50 but greater than or equal to 25, then bbb, and so on, until if it’s greater than or equal to 75 then ddd.

Also, this isn’t a “loop”, this is an if statement. :slight_smile:

Lastly, notice how the last case is an else, not an elif. Generally you should never end with elif because then it’s possible for the template to evaluate to nothing, and that will generally cause an error, especially in this type of construct.

Thanks guys, this help me as well.