Holiday light automation & Message malformed: extra keys error

I have been trying to piece this together form a couple of sources. The goal is to look at a calendar and if there is a specific holiday set 1 to 3 different sets of lights to a certain color 15 minutes before sunset. Otherwise set 2 sets of lights to a different random color.

When I go to save I get this error: Message malformed: extra keys not allowed @ data[‘automation’]

automation:
  - alias: Outside lights
    description: Sets lights to different colors on a holiday and randoms colors on non-holidays
    trigger:
      - event: sunset
        offset: "-00:15:00"
        trigger: sun
    condition:
      - condition: template
        value_template: >-
          {% set calendar1 = state_attr('calendar.holidays_in_united_states', 'all_day') %}  
          {% if calendar1 %}
            {% for event in calendar1 %}
              {% set holiday = event.summary %}
                {% if holiday == 'New Year\u0027s Eve' or holiday == 'Valentine\u0027s Day' or holiday == 'Saint Patrick\u0027s Day' or holiday == 'Cinco de Mayo' or holiday == 'Memorial Day' or holiday == 'Juneteenth' or holiday == 'Independence Day' or holiday == 'Halloween' or holiday == 'Veterans Day' %}
                  {{ true }}
                {% endif %}
              {% endfor %}
            {% else %}
              {{ false }}
            {% endif %}
    action:
      - choose:
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'New Year\u0027s Eve') | list | count >
                  0 }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.walkway_entry_light, light.walkway_garage_light,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: gold
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Valentine\u0027s Day') | list | count
                  > 0 }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: hotpink
              - service: light.turn_on
                target:
                  entity_id: light.walkway_entry_light, light.walkway_garage_light
                data:
                  color_name: red
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') | selectattr('summary', 'eq', 'Saint Patrick\u0027s Day') | list | count > 0 }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.walkway_entry_light, light.walkway_garage_light,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: lawngreen
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Cinco de Mayo') | list | count >
                  0 }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: green
              - service: light.turn_on
                target:
                  entity_id: light.walkway_entry_light, light.walkway_garage_light
                data:
                  color_name: white
              - service: light.turn_on
                target:
                  entity_id: light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: red
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Memorial Day') | list | count > 0
                  or state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Independence Day') | list | count
                  > 0 or state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Veterans Day') | list | count > 0
                  }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: red
              - service: light.turn_on
                target:
                  entity_id: light.walkway_entry_light, light.walkway_garage_light
                data:
                  color_name: white
              - service: light.turn_on
                target:
                  entity_id: light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: mediumblue
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') |
                  selectattr('summary', 'eq', 'Juneteenth') | list | count > 0
                  }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: firebrick
              - service: light.turn_on
                target:
                  entity_id: light.walkway_entry_light, light.walkway_garage_light
                data:
                  color_name: orange
              - service: light.turn_on
                target:
                  entity_id: light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: darkgreen
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr('calendar.calendar1', 'all_day') | selectattr('summary', 'eq', 'Halloween') | list | count > 0 }}
            sequence:
              - service: light.turn_on
                target:
                  entity_id: >-
                    light.entryway_wall_2, light.entryway_window_2,
                    light.garage_far_2, light.garage_walkway_2
                data:
                  color_name: purple
              - service: light.turn_on
                target:
                  entity_id: light.walkway_entry_light, light.walkway_garage_light
                data:
                  color_name: orange
        default:
          - service: light.turn_on
            target:
              entity_id: light.entryway_wall_2, light.entryway_window_2, light.garage_far_2, light.garage_walkway_2
            data_template:
              rgb_color: >-
                [
                  {{ range(0, 255) | random }},
                  {{ range(0, 255) | random }},
                  {{ range(0, 255) | random }}
                ]
              brightness_pct: 75
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data_template:
              rgb_color: >-
                [
                  {{ range(0, 255) | random }},
                  {{ range(0, 255) | random }},
                  {{ range(0, 255) | random }}
                ]
              brightness_pct: 75

Where is the code you posted located?

Because the error message implies it’s in a place where you shouldn’t include automation: in the first line.

The only place the first line of that code would be valid is if it’s in your configuration.yaml file (and there’s no other instance of automation: in it).

I am editing the yaml directly in the new automation window. I removed that and still have that error.

In that case, the line containing automation: should definitely NOT be anywhere in your automation’s code.

Because there’s another mistake.

The example you posted has the structure you would expect to find for an automation located in the configuration.yaml file. It is not the structure used in the Automation Editor’s window.

Replace it with this:

alias: Outside lights
description: Sets lights to different colors on a holiday and randoms colors on non-holidays
trigger:
  - event: sunset
    offset: "-00:15:00"
    trigger: sun
condition:
  - condition: template
    value_template: >-
      {% set calendar1 = state_attr('calendar.holidays_in_united_states', 'all_day') %}  
      {% if calendar1 %}
        {% for event in calendar1 %}
          {% set holiday = event.summary %}
            {% if holiday == 'New Year\u0027s Eve' or holiday == 'Valentine\u0027s Day' or holiday == 'Saint Patrick\u0027s Day' or holiday == 'Cinco de Mayo' or holiday == 'Memorial Day' or holiday == 'Juneteenth' or holiday == 'Independence Day' or holiday == 'Halloween' or holiday == 'Veterans Day' %}
              {{ true }}
            {% endif %}
          {% endfor %}
        {% else %}
          {{ false }}
        {% endif %}
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'New Year\u0027s Eve') | list | count >
              0 }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.walkway_entry_light, light.walkway_garage_light,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: gold
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Valentine\u0027s Day') | list | count
              > 0 }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: hotpink
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data:
              color_name: red
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') | selectattr('summary', 'eq', 'Saint Patrick\u0027s Day') | list | count > 0 }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.walkway_entry_light, light.walkway_garage_light,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: lawngreen
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Cinco de Mayo') | list | count >
              0 }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: green
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data:
              color_name: white
          - service: light.turn_on
            target:
              entity_id: light.garage_far_2, light.garage_walkway_2
            data:
              color_name: red
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Memorial Day') | list | count > 0
              or state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Independence Day') | list | count
              > 0 or state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Veterans Day') | list | count > 0
              }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: red
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data:
              color_name: white
          - service: light.turn_on
            target:
              entity_id: light.garage_far_2, light.garage_walkway_2
            data:
              color_name: mediumblue
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') |
              selectattr('summary', 'eq', 'Juneteenth') | list | count > 0
              }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: firebrick
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data:
              color_name: orange
          - service: light.turn_on
            target:
              entity_id: light.garage_far_2, light.garage_walkway_2
            data:
              color_name: darkgreen
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('calendar.calendar1', 'all_day') | selectattr('summary', 'eq', 'Halloween') | list | count > 0 }}
        sequence:
          - service: light.turn_on
            target:
              entity_id: >-
                light.entryway_wall_2, light.entryway_window_2,
                light.garage_far_2, light.garage_walkway_2
            data:
              color_name: purple
          - service: light.turn_on
            target:
              entity_id: light.walkway_entry_light, light.walkway_garage_light
            data:
              color_name: orange
    default:
      - service: light.turn_on
        target:
          entity_id: light.entryway_wall_2, light.entryway_window_2, light.garage_far_2, light.garage_walkway_2
        data_template:
          rgb_color: >-
            [
              {{ range(0, 255) | random }},
              {{ range(0, 255) | random }},
              {{ range(0, 255) | random }}
            ]
          brightness_pct: 75
      - service: light.turn_on
        target:
          entity_id: light.walkway_entry_light, light.walkway_garage_light
        data_template:
          rgb_color: >-
            [
              {{ range(0, 255) | random }},
              {{ range(0, 255) | random }},
              {{ range(0, 255) | random }}
            ]
          brightness_pct: 75

The differences are:

  1. There’s no automation: key word.
  2. The indentation is different. There’s no hyphenation of the automation’s first line.

Thank you!

1 Like

You’re welcome!

On a different but related subject, what is the calendar integration you are using?

The reason why I ask is because I am using the Google Calendar integration and the calendar entity it produces has an all_day attribute but its value is normally just a boolean (true/false).

In your case, your all_day attribute appears to contain more than merely a boolean value because you’re using templates like this to extract specific information from it.

{{ state_attr('calendar.calendar1', 'all_day')
  | selectattr('summary', 'eq', 'New Year\u0027s Eve')
  | list | count > 0 }}

Google and I pulled my code from Gemini, so there could be extra stuff that is not needed.

If you have a moment, copy-paste the following template into the Template Editor (Developer tools → Template).

{{ state_attr('calendar.calendar1', 'all_day') }}

What does it report? You don’t have to post the results here if it contains private information.

None

is what returns.

A result of none means that either calendar.calendar1 doesn’t exist or it does exist but it doesn’t have an all_day attribute.

Assuming that calendar.calendar1 exists, the implication is that today there are no all day events. Is that true?

The reason why I am asking these questions is because what the code is doing with a standard calendar entity seems unusual (such as what it’s doing with the all_day attribute). You said you used Gemini to produce the automation which only adds to my suspicions that the code may be faulty.

No all day event today. Let me test adding an event.

I suggest using Developer tools → States to examine calendar.calendar1, especially its attributes and their values.

The code you posted appears to be a mish-mash of techniques. For example, there’s normally no summary information in a calendar entity. You get summary only after using the calendar.get_events action.

1 Like