State-based template binary sensor based on two dates

I’ve been trying to create two template-based binary sensors; one for Halloween; and one for Christmas. If the current month/day date falls within either time frame, the sensor is supposed to be on; otherwise it’s off.

If I put them in my binary_sensors directory (binary_sensor: !include_dir_list ../entities/binary_sensors), HA complains that it’s missing property “entities”. If I move them to the Templates directory, I can restart HA, but they don’t show up in developer tools.

Any idea on how to get these to work properly? The goal is to get these working, then expand on them for future automations. Code is below. I used the thread here as a guideline.

template:
  - binary_sensor:
    - name: Christmas Schedule
      state: "{{ iif((12,11) <= (now().month, now().day) <= (1,8), 'on', 'off') }}"
template:
  - binary_sensor:
    - name: Halloween Schedule
      state: "{{ iif((10,24) <= (now().month, now().day) <= (11,7), 'on', 'off') }}"
state: "{{ iif((12,11) <= (now().month, now().day) <= (1,8), 'on', 'off')|bool }}"

or

state: "{{ iif((12,11) <= (now().month, now().day) <= (1,8), true, false) }}"

The templates must return true or false, not on or off.

Thanks. I changed them to true and false but it’s still complaining that it’s missing entities. Reloading the configuration also produces this error:

Cannot quick reload all YAML configurations because the configuration is not valid: Invalid config for [binary_sensor]: required key not provided @ data[‘platform’]. Got None. (See ?, line ?). Invalid config for [binary_sensor]: required key not provided @ data[‘platform’]. Got None. (See ?, line ?).

Sorry, I missed this:

template: is an integration it does not go under binary_sensor: (another seperate integration). It must have its own place in your configuration.yaml file:

binary_sensor: !include etc...

template:
 - binary_sensor:
     - name: etc...

Or

binary_sensor: !include etc...

template: !include etc...

Good point and it makes the iif unnecessary.

  - binary_sensor:
      - name: Christmas Schedule
        state: "{{ (12,11) <= (now().month, now().day) <= (1,8) }}"

NOTE

It does support values other than boolean True/False but the template can be more concise if it uses booleans.

1 Like

Thanks, @tom_l! Moving them back to the Integrations directory fixed it.

Also thank you @Taras for streamlining the states! I removed the iif.

1 Like