How to correctly write "greater than one number but less than another" for a reference field with variable data (electricity prices)

As per the title.

i currently have a yaml file from another person, and it has a bunch of elif commands to change the colour of a light based on prices of electricity.

but one thing i’d like to tidy up is that the fact the light “rainbows” through all colours when the price is very low. as the price is true for all rules, where there is an option for less than 100, and less than 50, and less than 20, and less than 10.

i hope to modify the sections so each is a range of possible numbers, to eliminate this effect.

here’s a sample of the file as it stands:

if current < 0 %}
[0, 0, 255]
{% elif current < 0.20 %}
[0, 255, 0]
{% elif current < 0.30 %}
[255, 247, 0]
{% elif current < 0.40 %}
[255, 165, 0]
{% elif current < 0.99 %}
[255, 0, 0]
{% else %}
[160, 32, 240]
{% endif %}

Here are the ranges you asked for:

{% if current < 0 %}
  [0, 0, 255]
{% elif 0.01 <= current < 0.20 %}
  [0, 255, 0]
{% elif 0.21 <= current < 0.30 %}
  [255, 247, 0]
{% elif 0.31 <= current < 0.40 %}
  [255, 165, 0]
{% elif 0.41 <= current < 0.99 %}
  [255, 0, 0]
{% else %}
  [160, 32, 240]
{% endif %}

Note however I doubt this will fix your problem. Your original set of if statements would stop at the first true statement and would not “rainbow through all colours”. Sounds like you have some other issue.

Shouldn’t it be elif 0.20 <= current < 0.30 and similar? Otherwise 0.20 would not be matched by neither this nor the previous line.

But yeah, I agree that this wouldn’t fix the issue, as the whole idea of elif is that it only runs if the previous “if” was not true.

thanks Tom (and Fanful).

the issue isnt bad, it’s just not perfect seeing the light flash through all possible options when the price is below 0c/kwh, and similar for each step up the ladder the price might be.

the overall automation has my RGB light on a solid colour, based on the same price/colour structure, and it also flashes a colour every 10 seconds to show what the forecast price will be in X minutes (currently set to 60).

it’s this flash through all colours that i’m trying to tidy up.

my lack of understanding about yaml’s syntax (or any other language) limits me in fully understanding the how and why of the complete script.

as i didnt write it i dont want to share the whole thing out of respect. which makes it harder for you to help me, i know.

Yes it should. Well spotted.

It should not do that. It should stop here (before it gets to the other colours):

{% if current < 0 %}
  [0, 0, 255]

Something else is going on. Please share the entire automation, sensor config or whatever it is.

alias: Electricity Price Awareness with Flash 30 min Forecast
description: >
  Shows the current electricity price color and flashes the forecasted price
  color
mode: single
triggers:
  - entity_id: sensor.home_general_forecast
    trigger: state
actions:
  - data:
      message: >
        Triggered forecast change, forecasted price for next 30 mins: {{
        state_attr('sensor.home_general_forecast', 'forecasts')[0].per_kwh }}
      level: warning
    action: system_log.write
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.home_general_price') | float < 0 }}"
        sequence:
          - target:
              entity_id: light.hue_play_1
            data:
              rgb_color:
                - 0
                - 0
                - 255
              brightness_pct: 10
            action: light.turn_on
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.home_general_price') | float < 0.2502 }}"
        sequence:
          - target:
              entity_id: light.hue_play_1
            data:
              rgb_color:
                - 0
                - 255
                - 0
              brightness_pct: 10
            action: light.turn_on
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.home_general_price') | float < 0.4102 }}"
        sequence:
          - target:
              entity_id: light.hue_play_1
            data:
              rgb_color:
                - 255
                - 247
                - 0
              brightness_pct: 10
            action: light.turn_on
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.home_general_price') | float >= 0.4102 and
              states('sensor.home__general_price') | float < 0.5102 }}
        sequence:
          - target:
              entity_id: light.hue_play_1
            data:
              rgb_color:
                - 255
                - 165
                - 0
              brightness_pct: 10
            action: light.turn_on
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.home_general_price') | float >= 0.5102 }}"
        sequence:
          - target:
              entity_id: light.hue_play_1
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 10
            action: light.turn_on
  - repeat:
      count: 180
      sequence:
        - delay: "00:00:09"
        - target:
            entity_id: light.hue_play_1
          data_template:
            rgb_color: >
              {% set forecast = state_attr('sensor.home_general_forecast',
              'forecasts')[0].per_kwh | float %} {% if forecast < 0 %}
                [0, 0, 255]
              {% elif forecast < 0.2502 %}
                [0, 255, 0]
              {% elif forecast < 0.4102 %}
                [255, 247, 0]
              {% elif forecast < 0.5102 %}
                [255, 165, 0]
              {% else %}
                [255, 0, 0]
              {% endif %}
            brightness_pct: 15
          action: light.turn_on
        - delay: "00:00:01"
        - target:
            entity_id: light.hue_play_1
          data_template:
            rgb_color: >
              {% set current = states('sensor.home_general_price') | float %} {%
              if current < 0 %}
                [0, 0, 255]
              {% elif current < 0.2502 %}
                [0, 255, 0]
              {% elif current < 0.4102 %}
                [255, 247, 0]
              {% elif current < 0.5102 %}
                [255, 165, 0]
              {% else %}
                [255, 0, 0]
              {% endif %}
            brightness_pct: 15
          action: light.turn_on

The above is the original script, before i added some edits (which work as intended).
Thanks.

i’m not certain, but i may have fixed it, by changing the main price section (top half of script). i copied this bit:

{{ states(‘sensor.home_general_price’) | float >= 0.4102 and
states(‘sensor.home__general_price’) | float < 0.5102 }}

and used it in all the other price brackets that needed defined ranges. i left the bottom half of the script alone.