I have a calendar, calendar.town_schools, in Home Assistant. Every event on the calendar is an all day event lasting one or more days. For the purposes of this sensor, I only care about events with the titles:
No School
Last Day of School
First Day of School
I want a school_day binary sensor with the following logic:
If it’s a weekend then the sensor is off
If there’s a “No School” event, the sensor is off
If the current date is > Last Day of School this year and < First Day of School this year, the sensor is off (for summer vacation)
Otherwise, the sensor is on.
The calendar, which I import from the school district, does not explicitly list summer vacation days or weekends as having “No School” and I’d very much like to avoid explicitly adding that.
I see examples of how to handle "if today has this specific event, turn the sensor on`, but can’t figure out how to handle the detection of summer vacation.
I would use a trigger-based template binary sensor or an automation that sets an input boolean helper. Either would be triggered by the Calendar events from `calendar.town_schools’ and conditioned on the event summary… The most basic version of each would be configured as follows:
Template Binary Sensor Example
#configuration.yaml
template:
- trigger:
- trigger: calendar
event: start
entity_id: calendar.town_schools
condition:
- condition: template
value_template: |
{{trigger.calendar_event.summary in ['Last Day of School','First Day of School']}}
binary_sensor:
- name: Summer Break
state: "{{ trigger.calendar_event.summary == 'Last Day of School' }}"
Input Boolean Example
triggers:
- trigger: calendar
event: start
entity_id: calendar.town_schools
conditions:
- condition: template
value_template: |
{{ trigger.calendar_event.summary in ['Last Day of School', 'First Day of School'] }}
actions:
- action: input_boolean.turn_{{ 'on' if trigger.calendar_event.summary == 'Last Day of School' else 'off'}}
target:
- entity_id: input_boolean.summer_break
Then your template could be something like:
{% set weekend = now().isoweekday() > 5 %}
{% set holiday = is_state_attr("calendar.town_public_schools", "message", "No School") %}
{% set summer = is_state('binary_sensor.summer_break', 'on') %}
{{ not (weekend or holiday or summer) }}