Esphomeyaml: blinking switch

I’d like to implement a blinking switch for a buzzer to chirp continuously until turned off, which I assume could be done with an output component and a template somewhat like below, but it would have a specified number of chirps rather than continuous.

I was able to do this using an output and a light component with a strobe effect, but then the component shows up as a light in HA. This is probably acceptable to me as I will 99% of the time utilize this in automations, but it isn’t satisfying to my perfectionist side.

Is there a way to accomplish what I want to do?

PS: I LOVE Esphomeyaml/lib

Matt

a) Mark the light as internal by only giving an id, not a name. Then it will no longer show up in HA and you can use a template switch to control the light internally.

b) Use a template switch directly, this is a bit hackish but I think it will work (not tested):

switch:
- platform: gpio
  pin: D1
  id: chirp_pin
- platform: template
  name: Chirp
  id: chirp
  optimistic: true
  turn_on_action:
  - switch.turn_on: chirp_pin
  - delay: 500ms 
  - switch.turn_off: chirp_pin
  - delay: 500ms
  # turn self on again if still enabled
  - if:
      condition:
        lambda: 'return id(chirp).state;'
      then:
      - switch.turn_on: chirp
  turn_off_action:
    - switch.turn_off: chirp_pin
1 Like

This made me think… Let’s make esphomeyaml’s automation syntax touring complete by adding loops:

switch:
- platform: gpio
  pin: D1
  id: chirp_pin
- platform: template
  name: Chirp
  id: chirp
  optimistic: true
  turn_on_action:
  - while:
      condition:
        switch.is_on: chirp
      then:
      - switch.turn_on: chirp_pin
      - delay: 500ms 
      - switch.turn_off: chirp_pin
      - delay: 500ms

:arrow_right:1.10.0

1 Like

I’m going to try the first option a), and can report back. The second option seems like it is infinitely self-recursive, but I have no experience to know whether this is is an issue at the lower level of logic.

I was 75% to this solution, but I think my wish for your second post was clouding my thinking. More complete automations would be wonderful!

It’s not self-recursive because the delay action is asynchronous. During the delay other code will execute, if there were no delay action you would be correct, the code would never terminate. You can think of the delay action as a special kind of action that stores a small micro-stack internally to allow for this.