Do an action "on boot" - turn off LEDs

I am new to ESPHome and am trying to migrate form using C++ (or Python!).

I want to make sure that an RGB LED is off when I boot.

This code doesn’t work as it confuses the switches. How do I identify each LED
colour independently?

Here is what I tried …

  - platform: gpio
    pin: GPIO32
    name: "LED red"
switch:
  - platform: gpio
    pin: GPIO33
    name: "LED green"    
switch:
  - platform: gpio
    pin: GPIO14
    name: "LED blue"


on_boot:
  then:
    - switch.turn_off: LED red
    - switch.turn_off: LED green
    - switch.turn_off: LED blue```

By the way I tried your quote marks - without success!! 

Thanks
Bruce

I’ve never set up switches myself and I’m not sure what you mean by “it confuses the switches”, but I don’t know if I’ve ever seen a config that allows multiple of the same key like that. Could you confirm that you have the correct structure for multiple switches?

A quick search of “esphome switch configuration” here in the forums shows that people generally use something more like this:

switch:
  - platform: gpio
    pin: GPIO32
    name: "LED red"
  - platform: gpio
    pin: GPIO33
    name: "LED green"    
  - platform: gpio
    pin: GPIO14
    name: "LED blue"

If this doesn’t help, I’m sure someone more knowledgeable will come along.

Ah, OK. Let me try that! I am new to ESPHome and am struggling with the syntax!

OK, so the switch part seems to be accepted, but the “on_boot:” throws up an error!

You need to work with ids

OK Thanks for your reply, but the documentation still leaves me confused!
In my code I tried to use the “name”.
So what is the difference between a “name” and an “id”?
The documentation on ids is clear - the id becomes a variable name in C++ (so that is clear).
But what is a name then?

When should I use either name or id? Or are there other “types” lurking somewhere?

As always the documentation seems to give “boilerplate” sketches which are probably great once you understand the ESPHome concepts, but leave beginners in the dark!!!
To steal a phrase “A snippet of working code is worth a thousand words” …

Did you search a bit? There are tons of examples in this forum. But here you go (untested)…

switch:
  - platform: gpio
    pin: GPIO32
    name: "LED red"
    id: led_red

on_boot:
  then:
    - switch.turn_off: led_red

name is the friendly name that is exposed in HA.
id is the internal designation within ESPHome.

2 Likes

Thanks Jorg, yes I tried all of the usual Googling, and found a few hints - but mostly on esphome.io

Thanks for the hint on “name” - since I am not using HA, then I can drop that and just keep the “id” - I read that the name is used as a C++ variable, so the usual rules apply (eg no spaces).

The code is similar to your suggestion, but this is it …

# This bit inserted  ---------------------------------
switch:
  - platform: gpio
    pin: GPIO32
    name: "LEDred"
    id: LEDred
  - platform: gpio
    pin: GPIO33
    name: "LEDgreen"
    id: LEDgreen
  - platform: gpio
    pin: GPIO14
    name: "LEDblue"
    id: LEDblue

on_boot:
  then:
    - switch.turn_off: LEDred
    - switch.turn_off: LEDgreen
    - switch.turn_off: LEDblue

# End of insert ---------------------------------------------

Here is the error report …

INFO Reading configuration /config/esp32-e2-2.yaml…
Failed config

on_boot: [source /config/esp32-e2-2.yaml:30]

Component not found: on_boot.
then:
- switch.turn_off: LEDred
- switch.turn_off: LEDgreen
- switch.turn_off: LEDblue

It’s true. If you look on www.esphome.io their simply is no on_boot component :tada:

Sorry?
I found it here … ESPHome Core Configuration — ESPHome
It is the first entry, about 50 lines down the page.

What makes you say that the component doesn’t exist?
Thanks
Bruce

on_boot is an automation, your spacing is off

esphome:
  # ...
  on_boot:
    priority: 600
    # ...
    then:
      - switch.turn_off: LEDred

So you found the esphome “core” component which does mention a automation called on_boot:

on_boot
This automation will be triggered when the ESP boots up. By default, it is executed after everything else is already set up

I suggested to read a little more about esphome, the website does really has lot’s of useful stuff to offer:

Thanks orange-assistant, so I have done quite a bit of reading over the last week or so!
The automations part was where I found “on_boot”.

The issue I am having is at COMPILE time! I don’t get as far as rebooting the machine!

Still not convinced “the website does really has lot’s of useful stuff to offer” , I agree lot’s of stuff, just not convinced that it is “useful” in getting code running!

The error at the compile time is a result of your wrong yaml. @cyn actually was so nice to correct the code for you. The only transfer you actually need to do now is a copy and paste.

The error you got:

You might want to use a editor with syntax highlighting so you can correct your errors before even try compiling :wink:

Can you suggest an editor with syntax highlighting that I could use?
Currently I am using the ESPHome web page and the edit function from there.
Thanks

Me and surely many others here use Visual Studio Code with a YAML extension.

I use this:

esphome:
  name: shelly-nappali-lampa
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: -100.0
    then:
      - delay: 40s
      - if:
          condition:
            - api.connected:
          then:
              - homeassistant.service:
                  service: light.turn_off
                  data:
                    entity_id: light.nappali_led

-100.0 because at this point the API is connected for sure.