Automation Templating - Comparing between a range of numbers

I’m getting myself confused with syntax here… I have an LDR sensor which I am trying to control my lights with. I am attempting to use Jinja2 templating instead of multiple automations. The general idea is I want to set the brightness_pct level to:

  • 0 when over 630 (Off)
  • 40 from 450 - 630
  • 50 from 280 - 480
  • 60 when below 280

What I have so far is:

{% if states('sensor.sn1_ldr') | float > 630 %}
  0
{% elif states('sensor.sn1_ldr') | float < 630 %}
  40
{% elif states('sensor.sn1_ldr') | float < 450 %}
  50
{% elif states('sensor.sn1_ldr') | float < 280 %}
  60
{% endif %}

Is this right?

It’d be nice if there was a between(280, 480) comparison

{% if states('sensor.sn1_ldr') | float > 630 %}
  0
{% elif states('sensor.sn1_ldr') | float > 450 %}
  40
{% elif states('sensor.sn1_ldr') | float > 280 %}
  50
{% else %}
  60
{% endif %}

Your ranges have overlaps. You might decide to do these as >= instead of > depending upon how you resolve the overlap.

Ah thank you. It’s been one of those days…

I changed the > to >= which actually makes more sense