Keep looping while condition is true

Hi. I have two leds (green & red) set up as switches in ESPhome (ESP8266). They are supposed to turn on and off depending on two factors according to:

  1. Temperature level
  2. Connection to wifi

My ESP-home config is as follows:


sensor:
  - platform: dallas
    address: 0x
    name: "test"
    unit_of_measurement: "°C"
    on_value_range:
        - above: 25
          then:
            - switch.turn_on: "red_led"
            - switch.turn_off: "green_led"
        - below: 25.0
          then:
            - switch.turn_off: "red_led"
            - switch.turn_on: "green_led"

This works fine. However, I want it to blink instead of a solid red. I was thinking of repeat but then you have to add integers. So my thought was to use some form of wait_until condition but I can’t get it to work.

Any ideas to solve this as elegantly as possible?

Also, how would I incorporate wifi online/offline into this? I’m thinking to use the code below in some form. However, the code is for when wifi is connected. I wanted to signal when it’s not connected :


on_...:
  if:
    condition:
      wifi.connected:
    then:
      - switch.turn_on: "green_led"
      - switch.turn_on: "red_led"

See “while” and “repeat

Thanks. I did already visit those pages and copy some code from there. However, I haven’t been able to get it working. I don’t understand the syntax well enough to combine several blocks. My first idea was to use

on_value_range as above, and combine with repeat & while true. But like I said, repeat needs a predetermined number of repeats, something that I can’t write seeing as I want it to be active indefinitely for as long as it’s above a certain threhshold.

Then make 999999?

I’m in the same boat, but keep at it you’ll figure it out eventually.

Or, how about the following. :exploding_head:NOT TESTED

“Option 1”

Create a “GPIO Output” instead of a switch, then create a “Binary Light”, with the “Pulse Effect” for the light.

output:
  - id: red_led
    platform: gpio
    pin: GPIO99

light:
  - platform: binary
    name: "High Temp Led"
    output: red_led
    effects:
      - pulse:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s

“Option 2”

Slow PWM Output

output:
    platform: slow_pwm
    pin: GPIO99
    id: red_led
    period: 100ms

on_value_range:
        - above: 25
          then:
            - output.turn_on: red_led

Thanks for the help. I’m struggling a lot with indentations and such.

I’ve tried both options and encountered two problems:

With the first one I get the error that “effect pulse is not allowed for this light type”.

For the second one I configured as such:

sensor:
  - platform: dallas
    address: 0x
    name: "test"
    unit_of_measurement: "°C"
    on_value_range:
        - above: 25
          then:
            - output.turn_on: red_led_2
output:
    platform: slow_pwm
    pin: GPIO5
    id: red_led_2
    period: 100ms

However, with this configuration the red led is constantly on irrespective of temperature.

Note

  • If pin is defined the GPIO pin state is writen before any action is executed.
  • state_change_action and turn_on_action/turn_off_action can be used togther. state_change_action is called before turn_on_action/turn_off_action. It’s recommended to use either state_change_action or turn_on_action/turn_off_action to change the state of an output. Using both automations together is only recommended for monitoring.

Try adding the.

output:
    platform: slow_pwm
    pin: GPIO5
    id: red_led_2
    period: 100ms
    turn_on_action:
      - lambda: |-
          auto *out1 = id(red_led_2);
          out1->turn_on();
    turn_off_action:
      - output.turn_off: red_led_2

" Option 3"

Status LED Light

light:
  - platform: status_led
    name: "Red LED"
    pin: GPIO5
    id: red_led_2
     effects:
       - pulse:
           name: "Fast Pulse"
           transition_length: 0.5s
           update_interval: 0.5s

**Update, correct indentation from "id’ down.

Thanks. Same error as above. The “effect pulse is is not allowed for this light type”. Is it because the output is defined as binary and thus can only assume one of two states?

:man_facepalming: :man_facepalming: :man_facepalming:
What about the following for the PWM Putput?

    turn_on_action:
      - lambda: |-
          auto *out1 = id(red_led_2);
          out1->turn_on();
    turn_off_action:
      - output.turn_off: red_led_2

DOCS DEFINITELY NEED SOME WORK

“Option 4”

Strobe Effect

light:
  - platform: status_led
    name: "Red LED"
    pin: GPIO5
    id: red_led
    effects:
      - strobe:
      - strobe:
          name: Strobe Effect
          colors:
            - state: true
              duration: 500ms
            - state: false
              duration: 500ms

I actually just tried the exact same thing. Not getting any errors but doesn’t seem to do anything either:

light:
  - platform: status_led
    name: "Red LED"
    pin: GPIO5
    id: red_led
    effects:
      - strobe:
      - strobe:
          name: Strobe Effect
          colors:
            - state: true
              duration: 500ms
            - state: false
              duration: 500ms
sensor:
  - platform: dallas
    address: 0x
    name: "test"
    unit_of_measurement: "°C"
    on_value_range:
        - above: 25
          then:
            - light.turn_on: "red_led"
            - switch.turn_off: "green_led"
        - below: 25
          then:
            - switch.turn_on: "green_led"
            - light.turn_off: "red_led"

After installation both leds are off. When temp goes above both turn off. When temp is below, both are on without any strobe effect on the red.

“Option 5” Tested and confirmed working :partying_face: :joy: :partying_face:.

output:
  - platform: esp8266_pwm
    pin: D1
    frequency: 500 Hz
    id: red_led_output

light:
  - platform: monochromatic
    output: red_led_output
    name: "Red LED"
    id: red_led
    effects:
      - pulse:
      - pulse:
          name: "Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
    on_turn_on:
      - light.turn_on: 
          id: red_led
          effect: "Pulse"

sensor:
  - platform: dallas
    address: 0x
    name: "test"
    unit_of_measurement: "°C"
    on_value_range:
        - above: 25
          then:
            - light.turn_on:
                id: "red_led"
            - switch.turn_off: "green_led"

Strobe works as well.

output:
  - platform: esp8266_pwm
    pin: D1
    frequency: 500 Hz
    id: red_led_output

light:
  - platform: monochromatic
    output: red_led_output
    name: "Red LED"
    id: red_led
    effects:
      - strobe:
      - strobe:
          name: "Strobe1"
          colors:
            - state: true
              duration: 500ms
            - state: false
              duration: 500ms
    on_turn_on:
      - light.turn_on: 
          id: red_led
          effect: "Strobe1"
1 Like

Much appreciated. I’m almost there. However, for some reason the red is always on below 25. I added the following bit but nothing happens:

        - below: 25
          then:
            - light.turn_off:   
                id: "red_led"
            - switch.turn_on: "green_led"

and to the light

    restore_mode: RESTORE_AND_OFF

Still no cigar. The red keeps staying on all the time, but at least correctly pulses thanks to your script

output:
  - platform: esp8266_pwm
    pin:
      number: D1
      inverted: true
    frequency: 500 Hz
    id: red_led_output
1 Like

Hats off my man. You just made my day.

The ESPhome documentation is good but it doesn’t really offer a good explanation of all the basic concepts. Any recommendation? I will attempt to do something similar with the wifi connection next, to have it signal if my ESP should lose connection.

To what?, the WiFi Sensor?

No, some sort guide/handbook that explains everything from a very basic level and up.

Not that I am aware of sorry, I am below basic, so just as bad as you, I just keep reading, re-reading, and with some trial and error success, less than 50% of the time :rofl:., for example, been ripping my here out for the past couple days trying to get touch screen working with a ILI9341.