If loop gives different result in dev tools and configuration.yaml

So I’m trying to make this light helper sensor

          {% if  now().hour|float >= 21  or now().hour|float  < 7 %} 
            night
          {% elif states('sensor.motion_carport_illuminance_lux') | float >  35 %}
            daylight
          {% elif states('sensor.motion_carport_illuminance_lux') | float <  30 and states('sensor.motion_carport_illuminance_lux') | float > 5  %}
            dusk
          {% else %}
            night
          {% endif %}

At this moment, it is

  • 20:30
  • sensor.motion_carport_illuminance_lux = 0

My expected result is “night”, and this is also true in the dev tools modus. But somehow my helper displays “dusk”. I can’t get my head arround it why… anyone who can? :slight_smile:

Screenshot 2023-09-20 204140

I have learned that use of parentheses is always a good idea, because HA can make unexpected decisions otherwise.
I am not certain this is the culprit here, but try to use parentheses around the statements you do the AND operation on too.

1 Like

please rewrite you template like:

{% set now = now().hour|float(0) %}
{% set lux = states('sensor.motion_carport_illuminance_lux') | float(0) %}
     
{% if  now >= 21  or now < 7 %} night
{% elif lux >  35 %} daylight
{% elif 5 < lux < 30 %} dusk
{% else %}  'else' night
{% endif %}

and then play with it in the dev tools, by replacing some numbers in the ‘set’ section

Ive added the ‘else’ word in the else output, to be able to discern whether your night is returned after the if, or after the final else…

finally, and that’s more of a semantics thing, you need to consider whether these conditions are mutually exclusive enough to device whether its night or not, iow, whether they truly return what you are after?

1 Like

That seems to work: same result for dev tools and in my actual config.yaml.

Any idea why mine wasn’t working?

With regard to the setup of the if/else: I want to force night mode (lights will turn on dimmed) between specific hours, regardless of the lux. Outside these hours, I want lux to determine the light mode.


fix1