Conditional Light Effect with WS2182b/NeoPixel Ringlight

I am just starting out with addressable LED-lights. I got a 12-LED-Ringlight and set it up with ESPHome (on a ESP8266, platform: neopixelbus). It shows up in HA and I can control the whole light via HA already.

I want to connect it to my CO2 sensor so that if CO2 levels are at 400ppm all LEDs are green and when CO2-level is rising, each of the LEDs turns red one by one until the room reaches 1000ppm and all LEDs are red. Like a progress bar so to speak

The problem is that I have no clue how to go about this problem. I know there are light effects pre-programmable effects with ESPHome, but how do I write an automation for this specific conditional lighting?

I searched already to the best of my knowledge, but came up empty. Does anybody have an idea?

You will want to put a condition on the sensor value, so that for a given range the light is turned on to a different colour - I believe something like the following should work (untested).

sensor:
  - platform: (your_co2_sensor_platform)
    # ...
    on_value_range:
       - above: 5
         below: 10
         then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%
       - above: 25
         below: 30
         then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 0%
                green: 0%
                blue: 100%
       - above: 45
         below: 50
         then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%
2 Likes

Does this code assume that both the light and sensor is on the same controller? Because I have mine on different. Or can I just add this to my sensor code, and it will work regardless of different devices?

Made some progress. This is the code I have so far:

esphome:
  name: ringlight
  platform: ESP8266
  board: d1_mini

...

light:
  - platform: neopixelbus
    type: GRB
    pin: GPIO3
    num_leds: 12
    name: "Ringlight"
    id: ringlight

sensor:
  - platform: homeassistant
    name: "CO2 Value"
    entity_id: sensor.mh_z19_co2_value
    on_value_range:
        above: 400.00000
        below: 9999.00000
        then:
          - light.addressable_set:
              id: ringlight
              red: 100%
              green: 0%
              blue: 0%

I see in the terminal that the device is getting the values from HA, but the lights do not turn on. Theoretically, all LEDs should be red. What did I wrong?

Made it work, but neither elegantly nor efficient :grimacing:

esphome:
  name: ringlight
  platform: ESP8266
  board: d1_mini

...

light:
  - platform: neopixelbus
    type: GRB
    pin: GPIO3
    num_leds: 12
    name: "ringlight"
    internal: true
    id: ringlight
          
  - platform: partition
    name: "LED0"
    id: LED0
    segments:
      - id: ringlight
        from: 0
        to: 0
  - platform: partition
    name: "LED1"
    id: LED1
    segments:
      - id: ringlight
        from: 1
        to: 1
  - platform: partition
    name: "LED2"
    id: LED2
    segments:
      - id: ringlight
        from: 2
        to: 2
  - platform: partition
    name: "LED3"
    id: LED3
    segments:
      - id: ringlight
        from: 3
        to: 3
  - platform: partition
    name: "LED4"
    id: LED4
    segments:
      - id: ringlight
        from: 4
        to: 4
  - platform: partition
    name: "LED5"
    id: LED5
    segments:
      - id: ringlight
        from: 5
        to: 5
  - platform: partition
    name: "LED6"
    id: LED6
    segments:
      - id: ringlight
        from: 6
        to: 6
  - platform: partition
    name: "LED7"
    id: LED7
    segments:
      - id: ringlight
        from: 7
        to: 7
  - platform: partition
    name: "LED8"
    id: LED8
    segments:
      - id: ringlight
        from: 8
        to: 8
  - platform: partition
    name: "LED9"
    id: LED9
    segments:
      - id: ringlight
        from: 9
        to: 9
  - platform: partition
    name: "LED10"
    id: LED10
    segments:
      - id: ringlight
        from: 10
        to: 10
  - platform: partition
    name: "LED11"
    id: LED11
    segments:
      - id: ringlight
        from: 11
        to: 11

Then created the corresponding scenes and automations. Each LED has now a 50ppm interval assigned to it.