Automations not saving correctly

I’m rewriting some of my automations to make them more efficient and do some consolidation. I’m trying to use variables to store things like tts message and when I go to save the automation, it wipes out the code. Am I missing something? Do I need to do a separate file for these type of automations?
I should mention, the offending automation is using a calendar trigger.

Here is the code i’m trying to use.

trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.chores
    id: first
    variables:
      tts_message: {{ state_attr('calendar.chores', 'description') }}, it is your week to wash and dry the towels, and the fold them and put them away.
      e_message: [CHORE NOTIFICATION] {{ state_attr('calendar.chores', 'description') }}, it's your week to do {{ state_attr('calendar.chores', 'message') }}
      e_message_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% elif state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% endif %}
      mobile_app_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}notify.mobile_app_xxx_ha_app{% else %}notify.mobile_app_xxxx{% endif %}
  - platform: calendar
    event: start
    offset: "1:0:0"
    entity_id: calendar.chores
    id: second
    variables:
      tts_message: {{ state_attr('calendar.chores', 'description') }}, it is your week to wash and dry the towels, and the fold them and put them away.
      e_message: [CHORE NOTIFICATION] {{ state_attr('calendar.chores', 'description') }}, it's your week to do {{ state_attr('calendar.chores', 'message') }}
      e_message_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% elif state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% endif %}
      mobile_app_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}notify.mobile_app_xxx_ha_app{% else %}notify.mobile_app_xxxx{% endif %}
  - platform: calendar
    event: start
    offset: "2:0:0"
    entity_id: calendar.chores
    id: third
    variables:
      tts_message: {{ state_attr('calendar.chores', 'description') }}, it is your week to wash and dry the towels, and the fold them and put them away.
      e_message: [CHORE NOTIFICATION] {{ state_attr('calendar.chores', 'description') }}, it's your week to do {{ state_attr('calendar.chores', 'message') }}
      e_message_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% elif state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% endif %}
      mobile_app_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}notify.mobile_app_xxx_ha_app{% else %}notify.mobile_app_xxxx{% endif %}
  - platform: calendar
    event: start
    offset: "4:0:0"
    entity_id: calendar.chores
    id: final
    variables:
      tts_message: {{ state_attr('calendar.chores', 'description') }}, it is your week to wash and dry the towels, and the fold them and put them away.
      e_message: [CHORE NOTIFICATION] {{ state_attr('calendar.chores', 'description') }}, it's your week to do {{ state_attr('calendar.chores', 'message') }}
      e_message_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% elif state_attr('calendar.chores', 'description') == "kidsname" %}xxxxxxxxxx{% endif %}
     mobile_app_target: {% if state_attr('calendar.chores', 'description') == "kidsname" %}notify.mobile_app_xxx_ha_app{% else %}notify.mobile_app_xxxx{% endif %}
condition:
  - condition: template
    value_template: "{{  state_attr('calendar.chores', 'message') == 'Towels' }}"
  - condition: state
    entity_id: input_boolean.audible_notifications
    state: "on"
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.quiet_time_weekday
            state: "off"
          - condition: time
            weekday:
              - sun
              - mon
              - tue
              - wed
              - thu
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.quiet_time_weekend
            state: "off"
          - condition: time
            weekday:
              - fri
              - sat
action:
  - if:
      - condition: template
        value_template: "{{  state_attr('calendar.chores', 'description') == \"kidsname\" }}"
    then:
      - service: notify.textbelt
        data:
          target: xxxxxxxxxx
          message: >-
            [CHORE NOTIFICATION] {{  state_attr('calendar.chores',
            'description') }}, it is your week to {{ 
            state_attr('calendar.chores', 'message') }}
    alias: Notify kidsname via Text
mode: single

Usually when that happens it means your yaml isn’t properly formatted, often it’s just messed up indentation.

If your variables are the same for all the triggers there no need to use trigger-attached variables. It would be less cluttered to just use a single variable block for the whole script/automation.

As shown in the Calendar integration examples it is best practice to not use the state object of the calendar entity with Calendar event triggers.

After tinkering with the code a little more, i finally got it to save. moving indentions around is what fixed it. As for the variables, after looking at it, i could simplify my variable and use a single code block. Is there a recommended/better place to put code blocks rather than in the trigger section? I’m using state object for parts of the variables, but not for triggering. I just put the variable block in the trigger section cause in the moment it made sense to keep them together. :man_shrugging:
Thanks for the feedback. I do appreciate it.

The issue I was commenting on is the fact that the state object of a calendar entity is not a reliable source for the data you need, especially when you use offsets. (see below for examples using the trigger variable)

Where you should place your variable definitions depends on where/how they will be used. Your original post doesn’t actually show the variables be used, and appears to be incomplete … so it’s hard to offer specific guidance. From what you have posted it looks like the variables will be used almost exclusively in the Actions block, so one way to approach it would be to declare the variables in that block something like the following:

trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.chores
    id: first
  - platform: calendar
    event: start
    offset: "1:0:0"
    entity_id: calendar.chores
    id: second
  - platform: calendar
    event: start
    offset: "2:0:0"
    entity_id: calendar.chores
    id: third
condition:
  - condition: state
    entity_id: input_boolean.audible_notifications
    state: "on"
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.quiet_time_weekday
            state: "off"
          - condition: time
            weekday:
              - sun
              - mon
              - tue
              - wed
              - thu
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.quiet_time_weekend
            state: "off"
          - condition: time
            weekday:
              - fri
              - sat
action:
  - variables:
      cal_description:  "{{ trigger.calendar_event.description }}"
      cal_message: "{{ trigger.calendar_event.summary }}"
      tts_chores:
        towels: "wash and dry the towels, and the fold them and put them away"
        chore2: "do Chore 2..."
        chore3: "do Chore 3..." 
      tts_message: >
        {{ cal_description }}, it is your week to {{ tts_chores.get(cal_message | lower) }}.
      e_message: >
        [CHORE NOTIFICATION] {{ cal_description }}, it's your week to do {{ cal_summary }}
      e_message_target: >
        {% if cal_description == 'kidsname' %}xxxxxxxxxx
        {% elif cal_description == 'kidsname' %}xxxxxxxxxx
        {% endif %}
      mobile_app_target: >
        {% if cal_description == 'kidsname' %}notify.mobile_app_xxx_ha_app
        {% else %}notify.mobile_app_xxxx{% endif %}
  - if:
      - condition: template
        value_template: "{{  cal_description' == 'kidsname' }}"
    then:
      - alias: Notify kidsname via Text
        service: notify.textbelt
        data:
          target: xxxxxxxxxx
          message: "{{ e_message }}"
mode: single

Thanks for the feedback. I ended up moving the variables to the action block as you suggested and cleaned them up a little bit. Seeing it in this manner makes more sense, especially when i come back to this later to modify it.

Thanks