Blink. How?

I just want to blink an LED indefinitely. No buttons or switches or sensor, just blink. Not the status component, just blink.
Advice would be appreciated.

1 Like

Define a Slow PWM output and turn it on at startup.

Or define an interval and use it to toggle a GPIO switch.

How do I “turn it on”? Apparently this is not enough by itself:

output:
  - platform: slow_pwm
    pin: D3
    id: my_slow_pwm
    period: 500ms

Thanks, this works exactly as wanted…

  # Blink the green LED
switch:
  - platform: gpio
    pin:
      number: D3
      mode: output
    id: greenLED

interval:
  - interval: 500ms
    then:
      - switch.toggle: greenLED
esphome:
  on_boot:
    priority: -100
    then:
      - output.turn_on: my_slow_pwm

This will probably use less CPU cycles than the other method, but either willl work.

Still not working…

esphome:
  name: wemosswitch
  platform: ESP8266
  board: d1_mini
  
  on_boot:
    priority: -100
    then:
    - output.turn_on: my_slow_pwm

All it does it turn on the LED. I am using the Wemos D1 Mini, and I am pretty sure that all sixteen GPIO pins can be PWM.

Any other thoughts?

This is Slow PWM so it is software PWM not hardware based. That means it doesn’t matter what pin as long as it supports digital out.

I have used this method before but it’s been a while - if I get a chance later I’ll breadboard it and see if I can get it to work myself.

Thanks. I appreciate the help as I am still struggling with learning ESPHome. (I barely grok Home Assistant and YAML).

Using PWM is definitely interesting as a concept. But I don’t see a reason why the processor load of a software PWM should be lower than that of the interval code of ESPHome. Both will work using timer interrupts.

True @Jpsy - the only way of knowing would be to dig into the source which I am not interested in doing.

The only reason I suggested it is that I have used it before - but it’s behaving differently I think - from memory as that project is long gone.

This works as I expect:

esphome:
  name: wemosswitch
  platform: ESP8266
  board: d1_mini
  
  on_boot:
    priority: -100
    then:
      output.turn_on: my_slow_pwm

output:
    platform: slow_pwm
    pin: GPIO5
    id: my_slow_pwm
    period: 1000ms
    inverted: true
    min_power: 0.01
    max_power: 1.00

Which is longer to type than your method. :slight_smile:

1 Like

This does work- sort of. “Duty” does not show in the docs for slow_pwm or output, but I discovered through googling that the output.set_level action is what I was looking for.

I did not need:

min_power: 0.00
max_power: 1.00

because 0 and 1 are the default values.
Also inverted: true is redundant with a 50% duty cycle.

Here is my working code, in case someone wants to simply blink an LED. (AKA, “Hello World”). [I could not get here without the help of everyone who replied to my query.]

esphome:
  name: wemosswitch
  platform: ESP8266
  board: d1_mini

  #For blinking the greenLED
  on_boot:
    priority: -100
    then:
      - output.turn_on:
          my_slow_pwm  
      - output.set_level:
          id: my_slow_pwm
          level: "50%"

output:
    platform: slow_pwm
    pin: D3
    id: my_slow_pwm
    period: 100ms

ALSO:

This also works and is easier to write. The only downside is that the log posts every time the LED blinks.

2 Likes

The logger component can be deactivated, but I guess this does not drain any major resources as long as the ESPHome add-on is not connected and listening to the log, which it normally is not.

I have to correct myself:
Just days after your questions I needed a “blink” in one of my own projects. And I realized that the switch component does not only write to the log but also creates a HA entity that will generate a lot of history data when changing every second or so.

The better solution to blink a LED without creating a HA entity is the output component. As this component does not have a toggle action, you must use a turn_on / delay / turn_off automation.

Here is the code:

# Blink the green LED
output:
  - platform: gpio
    pin:
      number: D3
      mode: output
    id: greenLED

interval:
  - interval: 500ms
    then:
      - output.turn_on: greenLED
      - delay: 250ms
      - output.turn_off: greenLED

By varying the delay you can even change the pulse width of this solution.

3 Likes

Thanks for the tip. I hadn’t thought of the log since I am running on an Intel NUC with 250gb of SSD, so the size of the log file won’t be an issue for years.

Out of curiosity, and for my education, what in my code above creates an HA entity? (Remember, I am a total novice in HA, let along ESPHome).

The switch component automatically creates corresponding HA entities for each ESP entity and it syncs all changes back and forth with the associated GPIOs.

The output component does not create any corresponding HA entities. So with output you create access to the GPIOs that is only meant to be used internally by ESP logic but not mirrored to HA.

The relevant change was simply to replace “switch” by “output”.

1 Like

Thanks. That is good to know and it goes into my notebook.

What else will create an entity?

Most components do. But you will have to read the docs.

If a component always creates a HA entity, you often find a Lovelace screenshot of the resulting entity.
Example: GPIO Switch — ESPHome

For other components the creation of a HA entity can be suppressed by configuration.
Example: Sensor Component — ESPHome (with config param internal)

1 Like

Pulse Effect :point_down:

1 Like