Template sensor broken after update

The below sensor code worked in v114 but I haven’t been able to get it working since updating to the latest version. The sensor never changes out of the unknown state, but seems to return a value for me in the template dev tools if i copy the template data over. I assume there’s some change to templating that I haven’t figured out. I have a few template sensors but this is the only one that I haven’t gotten working on the latest version. I looked thru the docs again but nothing jumped out as a cause, any help would be appreciated.

active_holiday:
    friendly_name: "Active Holiday"
    value_template: >-
      {% set day = now().day %}
      {% set month = now().month %}

      {% if month == 12 and (day >= 1 and day <= 30) %}
        XMas
      {% elif month == 1 and day == 3 %}
        Festival of Sleep
      {% elif month == 1 and day == 15 %}
        Stawberry Ice Cream Day
      {% elif month == 2 and day == 2 %}
        Groundhog Day
      {% elif month == 2 and day == 14 %}
        Valentines Day
      {% elif month == 3 and (day >= 14 and day <= 20) %}
        St Pattys
      {% elif month == 4 and day == 7 %}
        National Beer Day
      {% elif month == 4 and day == 20 %}
        Cannabis Day
      {% elif month == 5 and day == 4 %}
        Star Wars Day
      {% elif month == 5 and day == 5 %}
        Cinco de Mayo
      {% elif month == 7 and day == 4 %}
        Independence Day
      {% elif month == 10 and (day >= 1 and day <= 31) %}
        Halloween
      {% elif month == 11 and day == 11 %}
        Veterans Day
      {% elif month == 11 and (day >= 24 and day <= 28) %}
        Thanksgiving
      {% else %}
        None
      {% endif %}

The logic is working, but templates now return native types and “None” now has a meaning rather than just being a string. If you change “None” to “Nothing” or “No Holiday”, you’ll see that it returns that string.

1 Like

As your template doesn’t include any entities, HA can’t work out when it needs to update the template. You can create an automation to update the template sensor, or create a date/time sensor to use as an entity in the template.

See the docs:

Not true. Including now() will cause the template to be updated at the start of every minute.

Fair enough. Didn’t know that

Would be more elegant to load sensor.date and check against that, as that only updates daily.

(also, “Strawberry”)

2 Likes

How would you extract the day and month from sensor.date?

I don’t have access to my home assistant instance at the moment so can’t try anything in the template editor.

sensor.date, here, at least, is yyyy-mm-dd.

{% set month = states('sensor.date')[5:7]|int %}
{% set day = states('sensor.date')[8:10]|int %}
{% if day == 30 and month == 3 %}
  St Mamertinus of Auxerre's Day
{% endif %}
1 Like

You guys rock! Thanks for the tips and fixes for this, changing from None to any other value fixed this sensor for me. Good to know that None if off limits now. :+1:

@Troon I like this approach of using the date sensor, deff a lot more elegant than updating by the minute. So is the array notation you’re using there just a character range grab?

Yes, exactly that. string[start:end] where start is zero-indexed and end is the first character that is not included. Developer Tools / Templates is useful for playing with this stuff to make sure it’s working as you intend.