Help driving a traffic light

Hi
I have a air quality monitor with 3 coloured LED to show the status: red poor, yellow, medium, green good. But I cannot get the light to light up. I have resistors and the direction of the LED is correct, but I cannot seem to drive them. I have an output block in yaml:

output:
  - id: light_RED
    platform: gpio
    pin: GPIO27
  - id: light_YELLOW
    platform: gpio
    pin: GPIO9
  - id: light_GREEN
    platform: gpio
    pin: GPIO10

and a couple of commands to set the value of the light:

text_sensor:  
  - platform: template
    name: "Livingroom IAQ"
    icon: "mdi:air-filter"
    lambda: |-
      id(iaq_index) = 5;  
      
.../...
      
      /*
       * Transform IAQ index to human readable text according to Indoor Air Quality UK: 
       * http://www.iaquk.org.uk/
       */
      ESP_LOGD("main", "Current IAQ index %d", id(iaq_index));
      
      if (id(iaq_index) <= 6) {
        id(light_RED).turn

.../...


      else if (id(iaq_index) <= 14) {
        id(light_RED).turn_off();
        id(light_YELLOW).turn_on();
        id(light_GREEN).turn_on();
        return {"Good"};
      }
      else if (id(iaq_index) > 14) {
        id(light_RED).turn_off();
        id(light_YELLOW).turn_off();
        id(light_GREEN).turn_on();

        return {"Excellent"};
      }
      
      return {};
    update_interval: 2min

Is there anything obviously wrong in this code?
The return value of Good, Excellent etc, I do see in HA.

– Paul

First glance, I don’t see where you’ve setup those outputs as lights.

1 Like

I would:

  • Get the lights functioning by themselves (as Esphome lights)
  • Set up a template text sensor that shows the poor, medium, good as states.
  • Use the state of the text sensor to drive the state of the lights.

This should make it a bit easier to set-up, test and maintain. Anything you don’t want to expose to HA you can just mark as internal.

You can avoid lambda’s for a lot of it which should make it easier to debug. The text sensor would need one.

Hi,

I solved it. It did not like the GPIO9 and 10. When I switch to GPIO5 and 2 and indeed used the lights function, I first got a method to check if I can indeed switch the leds red , yellow and green. Once I switch then on or off the algorithm takes it over.

– Paul