Trying to create a template that sets an icon colour based on power wall charge state.
It should show anything less than 40 as red, 40-69 amber 70-00 green and 100 blue. But for some reason I’m missing, when the charge hits 100%, the colour output is red.
{% set state=states('sensor.powerwall_charge') %}
{% if state <'40' %}
red
{% elif state=='100' %}
blue
{% elif state>='40' and state<='69' %}
amber
{% else %}
green
{% endif %}
{{ states('sensor.powerwall_charge') }}
Worth me adding that when I remove the blue section, it still turns red at 100, when I would expect to to be green as it’s >69.
The statements are checked in order, and you are comparing strings when you need to compare numbers. Also your indentation is not good. Try this:
{% set state = states('sensor.powerwall_charge')|float(0) %}
{% if state < 40 %}
red
{% elif 40 <= state <= 69 %}
amber
{% elif state == 100 %}
blue
{% else %}
green
{% endif %}