Offset in Calendar Template Trigger

As I’m only now trying to understand templates (and really, they’re complicated - to me); I’m struggling with adding an offset to the following template trigger in an automation that helps me to figure out if today is an Orthodox (Christian) Fast Day (and what type of fasting is required). I’ve tried the following for update a sensor for today, but I’d like to use one for tomorrow as well using an offset:

alias: "Alpha: Fasting Day"
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ 'Fast Free' in
      state_attr('calendar.orthodox_ecclesiastical_calendar_office_365','description')
      }}
    id: fast_free
  - platform: template
    value_template: >-
      {{ 'Strict Fast' in
      state_attr('calendar.orthodox_ecclesiastical_calendar_office_365','description')
      }}
    id: strict_fast
  - platform: template
    value_template: >-
      {{ 'Fast Day (Wine and Oil Allowed)' in
      state_attr('calendar.orthodox_ecclesiastical_calendar_office_365','description')
      }}
    id: wine_and_oil
  - platform: template
    value_template: >-
      {{ 'Fast Day (Fish Allowed)' in
      state_attr('calendar.orthodox_ecclesiastical_calendar_office_365','description')
      }}
    id: fish_allowed
  - platform: template
    value_template: >-
      {{ 'Fast Day (Dairy\, Eggs\, and Fish Allowed)' in
      state_attr('calendar.orthodox_ecclesiastical_calendar_office_365','description')
      }}
    id: dairy_fish_eggs_allowed
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: fast_free
        sequence:
          - service: input_text.set_value
            data:
              value: Fast Free
            target:
              entity_id: input_text.fasting_today
      - conditions:
          - condition: trigger
            id: strict_fast
        sequence:
          - service: input_text.set_value
            data:
              value: Strict Fast
            target:
              entity_id: input_text.fasting_today
      - conditions:
          - condition: trigger
            id: wine_and_oil
        sequence:
          - service: input_text.set_value
            data:
              value: Fast Day (Wine and Oil Allowed)
            target:
              entity_id: input_text.fasting_today
      - conditions:
          - condition: trigger
            id: fish_allowed
        sequence:
          - service: input_text.set_value
            data:
              value: Fast Day (Fish Allowed)
            target:
              entity_id: input_text.fasting_today
      - conditions:
          - condition: trigger
            id: dairy_fish_eggs_allowed
        sequence:
          - service: input_text.set_value
            data:
              value: Fast Day (Dairy Allowed)
            target:
              entity_id: input_text.fasting_today
mode: parallel
max: 10

Using a template sensor; this’ll help me in automations and announcements:

    fasting_today:
      value_template: "{{ states.input_text.fasting_today.state }}"  
      friendly_name: "Fasting Today"
      unique_id: sensor.fasting_today
    fasting_tomorrow:
      value_template: "{{ states.input_text.fasting_tomorrow.state }}"  
      friendly_name: "Fasting Tomorrow"
      unique_id: sensor.fasting_tomorrow

Any suggestions as to how use an offset in the template trigger for my automation?

A calendar’s state object doesn’t have data about future events, so you will need to use Calendar event triggers if you need to use offsets. Instead of using an automation to set an text helper to provide the value for a state-based template sensor… you should be able to use a trigger-based template sensor, it may take a day to set itself the first time.

template:
  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
        offset: "0:0:0"
    sensor:
      name: Today Fasting
      state: |
        {% set description = trigger.calendar_event.description %}
        {% if 'Fast Free' in description %}
          Fast Free
        {% elif 'Strict Fast' in description %}
          Strict Fast
        {% elif 'Fast Day (Wine and Oil Allowed)' in description %}
          Fast Day (Wine and Oil Allowed)
        {% elif 'Fast Day (Fish Allowed)' in description %}
          Fast Day (Fish Allowed)
        {% elif 'Fast Day (Dairy\, Eggs\, and Fish Allowed)' in description %}
          Fast Day (Dairy Allowed)
  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
        offset: "-24:0:0"
    sensor:
      name: Tomorrow Fasting
      state: |
        {% set description = trigger.calendar_event.description %}
        {% if 'Fast Free' in description %}
          Fast Free
        {% elif 'Strict Fast' in description %}
          Strict Fast
        {% elif 'Fast Day (Wine and Oil Allowed)' in description %}
          Fast Day (Wine and Oil Allowed)
        {% elif 'Fast Day (Fish Allowed)' in description %}
          Fast Day (Fish Allowed)
        {% elif 'Fast Day (Dairy\, Eggs\, and Fish Allowed)' in description %}
          Fast Day (Dairy Allowed)
1 Like

Wow, that’s obviously a whole new chapter of templates that I hadn’t seen before. Thanks!
Visual Studio kept on complaining, so I made some minor alterations and now will see and wait for it to set itself:

- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
      offset: "00:00:00"
  sensor:
    name: Today Fasting Instructions
    unique_id: sensor.today_fasting_instructions
    state: >-
      {% set description = trigger.calendar_event.description %}
      {% if 'Fast Free' in description %}
        Fast Free
      {% elif 'Strict Fast' in description %}
        Strict Fast
      {% elif 'Fast Day (Wine and Oil Allowed)' in description %}
        Fast Day (Wine and Oil Allowed)
      {% elif 'Fast Day (Fish Allowed)' in description %}
        Fast Day (Fish Allowed)
      {% elif 'Fast Day (Dairy\, Eggs\, and Fish Allowed)' in description %}
        Fast Day (Dairy Allowed) {%endif%}
- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
      offset: "-24:00:00"
  sensor:
    name: Tomorrow Fasting Instructions
    unique_id: sensor.tomorrow_fasting_instructions
    state: >-
      {% set description = trigger.calendar_event.description %}
      {% if 'Fast Free' in description %}
        Fast Free
      {% elif 'Strict Fast' in description %}
        Strict Fast
      {% elif 'Fast Day (Wine and Oil Allowed)' in description %}
        Fast Day (Wine and Oil Allowed)
      {% elif 'Fast Day (Fish Allowed)' in description %}
        Fast Day (Fish Allowed)
      {% elif 'Fast Day (Dairy\, Eggs\, and Fish Allowed)' in description %}
        Fast Day (Dairy Allowed) {%endif%}

If you create an Input Select containing all five fasting instructions (input_select.all_fasting_instructions), your two Trigger-based Template Sensors can be reduced to this:

- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
      offset: "00:00:00"
  sensor:
    name: Today Fasting Instructions
    unique_id: sensor.today_fasting_instructions
    state: >-
      {% for x in state_attr('input_select.all_fasting_instructions', 'options') if x in trigger.calendar_event.description %}
        {{ x }}
      {% endfor %}
- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.orthodox_ecclesiastical_calendar_office_365
      offset: "-24:00:00"
  sensor:
    name: Tomorrow Fasting Instructions
    unique_id: sensor.tomorrow_fasting_instructions
    state: >-
      {% for x in state_attr('input_select.all_fasting_instructions', 'options') if x in trigger.calendar_event.description %}
        {{ x }}
      {% endfor %}

Should you ever need to add a fasting instruction, or modify the text of an existing one, it can be done in the Input Select and then both Trigger-based Template Sensors will automatically use the updated information (the next time they’re triggered).

2 Likes

Worked like a charm, thanks again.

Thank you too, I will definitely use this in another automation. The fasting instructions are pretty set, and I look them up in the O365 calendar that is the result of an Internet Calendar that I’ve subscribed to.