Automation Help - Check Calendar Update Boolean

Hello!

I’m fairly new to HA. I’m looking for a bit of support if possible :slight_smile:
I am trying to create an automation which checks my work calendar every morning, to determine if I am working or not.

If I am working a day shift then input_boolean.heating_decision should be off. If I am working a night shift, then input_boolean.heating_decision should be on.
I have created this as a helper through the UI.

My day shift may start anywhere from 06:00 to 10:00 and my night shift may start anywhere from 16:00 to 21:00.

Here is my automation code.

- id: 'decide_heating_based_on_shift'
  alias: "Decide Heating Based on Work Shift"
  trigger:
    - platform: time
      at: '05:00:00'
  action:
    - choose:
        # Day Shift (No Heating)
        - conditions: 
            - condition: template
              value_template: >-
                {% set start_time = '06:00:00' %}
                {% set end_time = '10:00:00' %}
                {% set today = now().strftime('%Y-%m-%d ') %}
                {% set events = state_attr('calendar.work', 'events') %}
                {% set day_shift = events | selectattr('start', 'ge', today ~ start_time) | selectattr('start', 'le', today ~ end_time) | selectattr('summary', 'search', 'Work') | list | count > 0 %}
                {{ day_shift }}
          sequence:
            - service: input_boolean.turn_off
              target:
                entity_id: input_boolean.heating_decision
        # Night Shift (Heating)
        - conditions: 
            - condition: template
              value_template: >-
                {% set start_time = '16:00:00' %}
                {% set end_time = '21:00:00' %}
                {% set today = now().strftime('%Y-%m-%d ') %}
                {% set events = state_attr('calendar.work', 'events') %}
                {% set night_shift = events | selectattr('start', 'ge', today ~ start_time) | selectattr('start', 'le', today ~ end_time) | selectattr('summary', 'search', 'Work') | list | count > 0 %}
                {{ night_shift }}
          sequence:
            - service: input_boolean.turn_on
              target:
                entity_id: input_boolean.heating_decision

I then plan to have another automation which reads the input_boolean.heating_decision to turn on the heating.

However, I keep getting the error:

Message malformed: extra keys not allowed @ data[‘0’]

Solved by playing around.

{% set start_time = '06:00:00' %}
{% set end_time = '10:00:00' %}
{% set today = now().strftime('%Y-%m-%d') %}
{% set next_event = state_attr('calendar.work', 'start_time') %}
{% set event_datetime = as_timestamp(next_event) | timestamp_custom('%Y-%m-%d %H:%M:%S') %}
{{ start_time <= event_datetime and event_datetime <= end_time }}

Google Calendar only lists the next event.

The state object only lists the current or next event, but more events are accessible using the calendar.list_event service.