How to turn on/off multiple outputs in one on_message?

Hello everyone,

I’m trying to use ESPHome on one of my ESP32s with a 4-channel relay in order to switch on/off the loudspeakers in my garden. Before I flashed ESPHome, I used a self made script which worked but was somewhat unreliable.

I defined 4 outputs, but as soon as I try to turn on the second output, ESPHome tells me that’s a duplicate key and won’t compile it.

This is how I tried it:

As you can see, the second “output.turn_on” already throws the error "Duplicate key: output.turn_on. What am I doing wrong or how can I achieve what I’m trying to do? This may be a dumb question, but I would really appreciate if someone could push me in the right direction.

Thanks a lot in advance :slight_smile:

Please don’t post pictures of text. Post the text and format it correctly.

That way we can help you by easily copying and editing the text.

Rather than describing a solution, like this:

You need to list the actions with a dash.

You*re absolutely right - I don’t know what I was thinking ^^ I tried to add a dash but I failed again, so now I post the text (hopefully formatted correctly), asking for your help again. Thanks!

mqtt:
  on_message:
    - topic: esp01/speakercontrol/set
      payload: "gardenOn"
      then:
        output.turn_on: left1
        output.turn_on: right1
        delay: 200ms
        output.turn_on: left2
        output.turn_on: right2 
    - topic: esp01/speakercontrol/set
      payload: "gardenOff"
      then:
        output.turn_off: left1
        output.turn_off: right1
        delay: 200ms
        output.turn_off: left2
        output.turn_off: right2

Nevermind - didn’t have in mind that I have to add a dash to the delay as well. Thanks again @tom_l - it’s working now :slight_smile:

Try this:

mqtt:
  on_message:
    - topic: esp01/speakercontrol/set
      payload: "gardenOn"
      then:
        - output.turn_on: left1
        - output.turn_on: right1
        - delay: 200ms
        - output.turn_on: left2
        - output.turn_on: right2 
    - topic: esp01/speakercontrol/set
      payload: "gardenOff"
      then:
        - output.turn_off: left1
        - output.turn_off: right1
        - delay: 200ms
        - output.turn_off: left2
        - output.turn_off: right2
1 Like

Working that way - thanks again :slight_smile:

1 Like