Automation to to alternate light red and blue. Message malformed error

I am trying to create an automation to turn my rope lings red for 1 second then blue for one second then red… etc.
This is throwing a Message malformed: extra keys not allowed @ data[‘seconds’] error

alias: Alternate Red Blue
description: ''
mode: single
trigger:
  platform: time
  minutes: 0
  seconds: 1
action:
  service: light.turn_on
  entity_id: light.rope_lights
    data:
      color_name: red

  service: light.turn_on
  entity_id: light.rope_lights
    data:
      color_name: blue

The automation uses a Time Trigger but supplies it with invalid options.

Perhaps you meant to use a Time Pattern Trigger?

Even if you did mean to use a Time Pattern Trigger like this:

trigger:
  platform: time_pattern
  seconds: '/1'

the automation may not produce the desired effect because the action issues two consecutive service calls with no delay between them.

In addition, the only control you have over this color-toggling operation is by enabling/disabling the automation.


EDIT

Here’s an another way of achieving the goal. Create an input_boolean to control the color-toggling operation. This automation starts color-toggling when the input_boolean is on and stops when it’s off.

alias: Alternate Red Blue
trigger:
- platform: state
  entity_id: input_boolean.alternate_colors
  to: 'on'
action:
- repeat:
    while: "{{ is_state('input_boolean.alternate_colors', 'on') }}"
    sequence:
    - service: light.turn_on
      data:
        entity_id: light.rope_lights
        color_name: red
    - delay: '00:00:01' 
    - service: light.turn_on
      data:
        entity_id: light.rope_lights
        color_name: blue
    - delay: '00:00:01'

Thanks!!
You’re right… Mine didn’t work even after I changed it to time_patten… No errors but did not cycle

I tried the the automation you posted. No errors but the lights would not cycle when I “Execute” the automation

Probably because you didn’t create input_boolean.alternate_colors and turn it on.

Look at the template in while. It refers to input_boolean.alternate_colors. If it doesn’t exist, the repeat will do absolutely nothing.

Ah… got it…
Once I replaced input_boolean.alternate_colors with a window sensor… It worked.
Can I use milliseconds?

According to the documentation, yes.

Half-second delay:

  - delay:
      milliseconds: 500

Thank you…
It is working as expected

1 Like

Glad to hear it’s working properly.

Please consider marking my first post with the Solution tag. By doing this, it will automatically place a check-mark next to the topic’s title. It signals to other users that this topic has an accepted solution. It also helps users find answers to similar questions.