Why is my binary_sensor with value_template not working anymore?

I just noticed 2 sensors from my boolean’s do not work anymore. No errors in the log and other boolean sensors work well…

Did I miss a breaking change?


      kerst_seizoen:
        friendly_name: Kerst Seizoen (1 dec - 15 jan)
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 12 and day >= 1 or month == 1 and day <= 15 }}
      winter_seizoen:
        friendly_name: Winter Seizoen (15 oktober - 15 maart)
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 10 and day >= 15 or month == 3 and day <= 15 }}

both are “off” after restart of homeassistant…

It worked last year :slight_smile:

With help from @petro on discord, it now works well again.

Thanks Petro!!!

      kerst_seizoen:
        friendly_name: Kerst Seizoen (25 nov - 15 jan)
        value_template: >
          {% set date = now().date() %}
          {{ date.replace(month=11, day=25) <= date or date <= date.replace(month=1, day=15) }}
      winter_seizoen:
        friendly_name: Winter Seizoen (15 oktober - 15 maart)
        value_template: >
          {% set date = now().date() %}
          {{ date.replace(month=10, day=15) <= date or date <= date.replace(month=3, day=15) }}
1 Like

You’re second sensor can only be true in October or March; never any other month. Also, it’s probably not required in this case, because I think ands are evalated before ors, but you would be safer using parenthesis around your clauses to make sure your tests are evaluated in the right order.

Let’s assume the parentheses are there and evaluate your test mentally

In your picture, month will be 11 and day will 29.

( ( 11 == 10 and 29 >= 15) or (11 == 3 and 29 <= 15) )
( (  FALSE   and    TRUE ) or (  FALSE and   FALSE ) )
( (         FALSE        ) or (      FALSE         ) )
(                        FALSE                       )

So to fix it, you need clauses that will be true in November, December, January and February. Maybe something like this

        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ (month == 10 and day >= 15) or (month == 3 and day <= 15) or month >= 11 or month <= 2 }}
1 Like