Getting ahead of the game - Christmas sensor template

I’m trying to reduce the clutter on Lovelace by making most things conditional that don’t need constant control or are controlled mainly elsewhere.

I’ve now got to my three Christmas switches and want the card to appear during the festive period only. The card part is fine, but I’m trying to set up a sensor template to sense if it’s Christmas or not. I copied this from another post, but I’m getting an error in the log.

The error is;
Could not render template christmas_time: UndefinedError: list object has no element 1

# Create the time_date sensors
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
      
# Christmas time sensor to show the cards and other useful things
sensor:
  - platform: template
    sensors:
      christmas_time:
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 12 and day >= 5 or
             month == 1 and day <= 6 }}

It looks right. Are you sure sensor.date got created ok? What does its state look like? Have you tried the template in the template editor?

Hi Phil, thanks for checking.

The template editor was showing as true/false as expected.

I’ve since swapped it to a binary sensor and all is well;

# Christmas time sensor to show the cards and other useful things
binary_sensor:
  - platform: template
    sensors:
      christmas_time:
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 12 and day >= 5 or
             month == 1 and day <= 6 }}

I had two sensor: entries in the yaml file, by changing it to binary_sensor: it therefore worked, but broke another binary_sensor entry. Getting my head around only one entry for each type now.

If you have two binary_sensor: entries in one file, merge them together. E.g.:

binary_sensor:
  - platform: ...
...
binary_sensor:
  - platform: ...

Merge them into:

binary_sensor:
  - platform: ...
  - platform: ...

Thanks Phil, that’s how I have it now for all sensors and binary_sensors. Getting there…

1 Like