Led blinker yaml

Hi All,

The code below is working fine, once i turn on the switch in the UI. I would like to modify it so that the blinking starts on boot up of the esp32.

Also I’m trying to learn if there is a way to make the led blink while motion is detected? ([binary_sensor:036]: ‘PIR Sensor’: Sending state ON). or even based on the light (light intensity’: Sending state [somevalue] % )

Thanks for any inputs.

binary_sensor:
- platform: gpio
  pin: 5
  name: "PIR Sensor"
  id: PIR
  device_class: motion

sensor:
  - platform: adc                         
    pin: A0
    id: lux
    name: light intensity
    update_interval: 1s
    unit_of_measurement: "%"
    filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 1
    - calibrate_linear:
        - 1.00000 -> 100.00
        - 0.00100 -> 0.00
    accuracy_decimals: 2

switch:
  - platform: gpio
    pin: 0
    inverted: True
    name: "led"
    id: ledblink

  - platform: template
    name: "led blinker"
    optimistic: yes
    id: blinker
    turn_on_action:
    - while:
       condition:
         lambda: 'return true;'
       then:
       - switch.turn_on: ledblink
       - delay: 10ms 
       - switch.turn_off: ledblink
       - delay: 1000ms
    turn_off_action:
    - switch.turn_off: ledblink

Try with this on your blinker. I have never tried restore_mode with template though.
restore_mode: ALWAYS_ON.
If it doesn’t work, you can turn it on at boot:

esphome:
  # ...
  on_boot:
    - priority: 600
      then:
        - switch.turn_on: blinker

But if you want to make universal blinker that you can trigger from other components as well, maybe

output:
    platform: slow_pwm
    pin: GPIO0
    id: my_slow_pwm
    period: 1s

would be best choice

@Karosm you might wanna fix the typo in the above post:

restore_mode: ALLWAYS_ON should be restore_mode: ALWAYS_ON

Good pick.
I need to practice more with my english or just copy-paste everything I write… :smiling_face_with_tear:

1 Like

It’s fine mate. We all make mistakes and you’re the only one here who attempted to help the OP.

1 Like

I’m fine. Thank’s to you (and others), I’m actually getting good practice here :wink:

2 Likes

Thanks @Karosm, on_boot did the trick. Im still trying to figure out how to make it conditional for example when motion is detected. I have when through what is possible but does not seem the function. I tried binary_sensor.is_on: PIR.

binary_sensor:
- platform: gpio
  pin: 5
  name: "PIR Sensor"
  id: PIR
  device_class: motion

switch:
  - platform: gpio
    pin: 0
    inverted: True
    name: "motion detected"
    id: mDetected

- platform: template
    name: "led blinker"
    optimistic: yes
    id: blinker
    turn_on_action:
    - while:
       condition:
        #lambda: 'return true;'
        # if motion is detected
         binary_sensor.is_on: PIR
       then:
       - switch.turn_on: mDetected
       - delay: 10ms 
       - switch.turn_off: mDetected
       - delay: 5000ms
    turn_off_action:
    - switch.turn_off: mDetected

It doesn’t work like that.
It’s the binary sensor that has to do the action.
on_press: (or on_click depending on the length of the trigger), and copy the automation you have on blinker.

binary_sensor:
  - platform: gpio
    # ...
    on_press:
      then:

Or start from scratch with slow_pwm output.

@Karosm , thanks for pointing me in the right direction. I managed to get a led to blink on detection of motion, not sure if it’s the correct way as randomly the led doesn’t go out until the next motion gets triggered, but here it is. Regarding the pwm way I couldn’t find a way to customise the blink pattern as I have right now such as a short pulse on and a longer off.

binary_sensor:
- platform: gpio
  pin: 5
  name: "PIR Sensor"
  id: pir
  device_class: motion
  on_state: 
    then:
      - while: 
          condition: 
          - binary_sensor.is_on: pir
          then: 
             - switch.turn_on: detectLed
             - delay: 10ms 
             - switch.turn_off: detectLed
             - delay: 400ms       

 - platform: gpio
    pin: 1
    inverted: True
    name: "Detect Led"
    id: detectLed 

I’m sure you can tune it. Observe how long the PIR outputs high signal.

did you try to use level (not tested)?

- output.turn_on:
          my_slow_pwm  
      - output.set_level:
          id: my_slow_pwm
          level: "10%"

Thanks,

Just can’t seem to make is happen via slow pwm the led flickers dim with random bright pulses. However I managed to fix the issue where in some instances the led does not switch off. I added a default - switch.turn_off: detectLed to the binary sensor. and now it’s behaving good.

Sorry for asking basic questions as I have started on my esp journey and trying to grasp the basic building blocks.

binary_sensor:
- platform: gpio
  pin: 5
  name: "PIR Sensor"
  id: pir
  device_class: motion
  on_state: 
    then:
      - while: 
          condition: 
          - binary_sensor.is_on: pir
          then: 
             - switch.turn_on: detectLed
             - delay: 10ms 
             - switch.turn_off: detectLed
             - delay: 400ms
      - switch.turn_off:  detectLed 

 - platform: gpio
    pin: 1
    inverted: True
    name: "Detect Led"
    id: detectLed 

My next stop is flashing the led when the lumens is above a defined value.

Is that yaml above working? LED as gpio binary sensor switched with switch.turn_ command. Hard to believe…
Basically you are switching an input :open_mouth:

Basic things are sometimes tricky because of lacking/hard to find documentation.
Forum is great help!

Im sorry let me past the code. The led is configured in a switch which I didn’t paste earlier

binary_sensor:
- platform: gpio
  pin: 5
  name: "PIR Sensor"
  id: pir
  device_class: motion
  on_state: 
    then:
      - while: 
          condition: 
          - binary_sensor.is_on: pir
          then: 
             - switch.turn_on: detectLed
             - delay: 20ms 
             - switch.turn_off: detectLed
             - delay: 500ms  
      -  switch.turn_off:  detectLed
switch:
  - platform: gpio
    pin: 3
    inverted: True
    name: "detection led"
    id: detectLed 

It looked like impossible…

You can basically continue similar way with other triggers or try new ways.
There are so many ways of doing things, if slow pwm doesn’t work for some reason, you could try with light component for example.
Something like this (not tested).

output:
  - platform: gpio
    pin: GPIO3
    id: detectled
light:
  - platform: binary
    name: "LED"
    id: led
    output: detectled
    effects:
      - pulse:
          name: "Quick Blink"
          transition_length:
            on_length: 100ms
            off_length: 900ms
          update_interval: 1.0s
    on_turn_on:
      - light.turn_on: 
          id: led
          effect: "Quick Blink"

then you can turn the blinking led on anywhere with:
light.turn_on: led

ps. don’t use capital letters in id