Blinking LED with esphomeyaml

Continuing the discussion from Esphomeyaml: blinking switch:

I’m trying to make an LED light blink with esphomeyaml. With the following yaml I can turn the blinking LED on once, but after I turn it off it cannot be turned on again. I’m using mqtt and on the state topic i get “ON” but the LED does not turn on. I just upgraded the hass.io esphomeyaml addon to 1.10.0.
My hardware is a wemos D1 mini and the led is connected with a 330Ohm resistor.

switch:
- platform: gpio
  pin: D8
  id: red_pin

- platform: template
  name: "Red LED"
  optimistic: yes
  id: redtemp
  turn_on_action:
  - while:
      condition:
        switch.is_on: redtemp
      then:
      - switch.turn_on: red_pin
      - delay: 500ms 
      - switch.turn_off: red_pin
      - delay: 500ms
  turn_off_action:
  - switch.turn_off: red_pin
1 Like

With some of the other changes in that release you can also write:

switch:
- platform: gpio
  pin: D8
  id: red_pin

- platform: template
  name: "Red LED"
  optimistic: yes
  id: redtemp
  turn_on_action:
  - while:
      condition:
        lambda: 'return true;'
      then:
      - switch.turn_on: red_pin
      - delay: 500ms 
      - switch.turn_off: red_pin
      - delay: 500ms
  turn_off_action:
  - switch.turn_off: red_pin

That hopefully will work. I think the reason the condition doesn’t work is because the switch is only turned on after the turn on action is done.

1 Like

Thank you! I’ll try this when I get home tonight. :slight_smile:

Just tried this out and tl;dr it worked!

Weirdly enough, the first time I tried it (once HA recognized the switch, I had the same problem as @malgoe, where it would turn on once, then nothing. After a hard reboot/power cycle, it magically worked! I… don’t know why but it did, and has continued to since.

Big thanks, this was actually kind of important because I have a bed shaker that’ll burn out if left running for > 30s intervals so yeah.

With the new API, is there a way to tell HASS the status of the switch so it just appears as a standard on/off toggle? With MQTT I would have set up a binary_sensor in EHY that gets turned on/off with the template, and just have manually configured the MQTT device in configuration.yaml. It’s not the most important but it’d be nice. Either way, thanks for all your help!

I spoke too soon! Consistent behavior with this config is now that it works to pulse very nicely, once. After you turn off the light/switch in HA, it does not turn on again. The debug logs show it turning on the template switch and sending an ON state, but not turning on the hidden actual output. Oddly enough, it does show the turning off action for the switch itself when it’s supposed to.

I’ll attach the logs below. Configuration is copy/pasted from yours, @OttoWinter, except I have sv_relay instead of red_pin and “Bed Shaker” is the exposed template switch.

# Turned on switch from HA
[03:54:33][D][switch:033]: 'Bed Shaker' Turning ON.
[03:54:33][D][switch:033]: 'sv_relay' Turning ON.
[03:54:33][D][switch:055]: 'sv_relay': Sending state ON
[03:54:33][D][switch:055]: 'Bed Shaker': Sending state ON
[03:54:36][D][switch:037]: 'sv_relay' Turning OFF.
[03:54:36][D][switch:055]: 'sv_relay': Sending state OFF
[03:54:39][D][switch:033]: 'sv_relay' Turning ON.
[03:54:39][D][switch:055]: 'sv_relay': Sending state ON
[03:54:42][D][switch:037]: 'sv_relay' Turning OFF.
[03:54:42][D][switch:055]: 'sv_relay': Sending state OFF
# Turned off switch from HA
[03:54:44][D][switch:037]: 'Bed Shaker' Turning OFF.
[03:54:44][D][switch:037]: 'sv_relay' Turning OFF.
[03:54:44][D][switch:055]: 'sv_relay': Sending state OFF
[03:54:44][D][switch:055]: 'Bed Shaker': Sending state OFF
# Tried to turn it on again
[03:55:02][D][switch:033]: 'Bed Shaker' Turning ON.
[03:55:02][D][switch:055]: 'Bed Shaker': Sending state ON
# Off in HA. Notice it _does_ set sv_relay state this time. On/Off another time for posterity.
[03:55:17][D][switch:037]: 'Bed Shaker' Turning OFF.
[03:55:17][D][switch:037]: 'sv_relay' Turning OFF.
[03:55:17][D][switch:055]: 'sv_relay': Sending state OFF
[03:55:17][D][switch:055]: 'Bed Shaker': Sending state OFF
[03:55:20][D][switch:033]: 'Bed Shaker' Turning ON.
[03:55:20][D][switch:055]: 'Bed Shaker': Sending state ON
[03:55:21][D][switch:037]: 'Bed Shaker' Turning OFF.
[03:55:21][D][switch:037]: 'sv_relay' Turning OFF.
[03:55:21][D][switch:055]: 'sv_relay': Sending state OFF
[03:55:21][D][switch:055]: 'Bed Shaker': Sending state OFF

https://github.com/OttoWinter/esphomelib/pull/378

1 Like

Quick question on this:
Is it possible to run the while loop on boot, but then break it when a different command comes in?

(In my case: a monochromatic light that toggles with a default_transition_length, but will then go in to “normal operation” when a command is received from homeassistant)

Hi, i just had the same error and found an solution:
Turn off the logging in esphome, because every blink will be written.
Since i deactivatet “logging”, my Alarm-Blinker runs and runs…

1 Like

hi,
I’m trying to use this therad in my project but with no success. I want this:
when water heater is working than indicator led blink constantly. here is a part of code:

switch:
  - platform: gpio
    pin:
      number: GPIO32
      inverted: false  
    name: heater
    id: heater_relay
  - platform: template
    name: "plavi led"
    id: blueled
    turn_on_action:
    - while:
          condition:
            lambda: 'return true;'
          then:
          - light.turn_on: HI
          - delay: 2s
          - light.turn_off: HI
binary_sensor:
  - platform: gpio  
    pin:
      number: GPIO33
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Heater on"
output:
  - platform: ledc
    pin: GPIO25
    id: gpio_25
light:
  - platform: monochromatic
    output: gpio_25
    id: HI
    name: "Heater indicator"

Welcome back @flega - long time no see :eyes:

Meanwhile we can just use a light effect to pulse or blink your led :bulb: check the docs: :point_down:

If you still have problems I would suggested to open a new thread rather then hanging on a 4 year old one… :writing_hand:

Thanks for you concern, wasn’t expecting that :grin:
I know about light effect but don’t know how to make led is light (or fashing or anything) when heather is activated :robot:

If light effect don’t work you should add a delay also after the light.turn_off. Now it loops back to turn_on directly.