Trying to Automate a LED based on temp need help

Hi all,

I have a sensor that I have made up in ESPhome in this I have a dht22 to give me temp and humidity, I have also added a RGB LED and what I am trying to do is have the colour of the LED change depending on the temp of the room.

here is what I have so far

sensor:
  - platform: dht
    pin: D2
    model: dht22
    temperature:
      name: "Bedroom Temperature"
      on_value_range:
        - below: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 0%
                green: 0%
                blue: 100%
        - above: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 0%
                green: 100%
                blue: 0%
        - above: 25
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 100%
                green: 0%
                blue: 0%
    humidity:
      name: "Bedroom Humidity"
    update_interval: 30s

but the LED does not light up.

I know the LED is working as I can control it within Home assistant but I am trying to get it to work without home assistant if possible.

this is the first time I am trying to do anything like this in ESPhome and I am probably missing something really simple but I cant for the life of me see it.

all help is greatly appretiated

thank you

Robin

I can’t help you with what the issue is, but if you can control it from HA, then you could transfer the automation to HA.
Not ideal, but it works.

Can you post your full yaml (specially the light: part in your case).

Also what does your esphome logs show? You can probably “force” a temperature change by covering the sensor with your hands to force it to heat up to the next value you have configured and monitor the logs at the same time…

Thank you for your reply.

the full Yaml is here

esphome:
  name: bedroom_sensor
  platform: ESP8266
  board: d1_mini
 

wifi:
  ssid: !secret ssid
  password: !secret password


    
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom sensor Fallback Hotspot"
    password: !secret appass

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Enable Web server.
web_server:
  port: 80


output:
  - platform: esp8266_pwm
    pin: D1
    id: redgpio
  - platform: esp8266_pwm
    pin: D7
    id: greengpio
  - platform: esp8266_pwm
    pin: D3
    id: bluegpio

light:
  - platform: rgb
    name: "Bedroom LED"
    id: bedroom_led
    red: redgpio
    green: greengpio
    blue: bluegpio


sensor:
  - platform: dht
    pin: D2
    model: dht22
    temperature:
      name: "Bedroom Temperature"
      on_value_range:
        - below: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 0%
                green: 0%
                blue: 100%
        - above: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 0%
                green: 100%
                blue: 0%
        - above: 25
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 50%
                red: 100%
                green: 0%
                blue: 0%
    humidity:
      name: "Bedroom Humidity"
    update_interval: 30s

  - platform: wifi_signal
    name: "Bedroom WiFi signal"
    update_interval: 600s

  - platform: uptime
    name: "Bedroom uptime do not use"
    id: bedroom_uptime
    internal: true
    update_interval: 600s

text_sensor:
  - platform: version
    name: "Bedroom ESPHome version"
    
  - platform: template
    name: "Bedroom Uptime"
    lambda: |-
      uint32_t dur = id(bedroom_uptime).state;
      int dys = 0;
      int hrs = 0;
      int mnts = 0;
      if (dur > 86399) {
        dys = trunc(dur / 86400);
        dur = dur - (dys * 86400);
      }
      if (dur > 3599) {
        hrs = trunc(dur / 3600);
        dur = dur - (hrs * 3600);
      }
      if (dur > 59) {
        mnts = trunc(dur / 60);
        dur = dur - (mnts * 60);
      }
      char buffer[17];
      sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
      return {buffer};
    icon: mdi:clock-start
    update_interval: 600s

switch:
  - platform: restart
    name: Bedroom Restart


I got a lot of help to create this and have used some stuff I found on the forums so I can’t take credit for the work you see, I’ve also removed some wifi details but all the rest is there.

hope it helps.

Robin

did you ever find a way of doing this??? Im trying to do the same thing with a pixel of a WS2812

You know what… any back seat driver can up thier reply posts by just typing “Light Component”

This forum is for help.

The OP had a problem - it wasnt solved and now I have a similar situation.

If you can assist in giving any CONSTRUCTIVE feedback - please do.

interval:
  - interval: 1s
    then:
      - lambda: |-
          float temp = id(temp1).state;
          float setpoint = id(controller).current_temperature;
          float diff = temp - setpoint;
          float red_percent = 0.0f;
          float green_percent = 0.0f;
          float blue_percent = 0.0f;
          if (diff <= -2.0) {
            red_percent = 0.0f;
            green_percent = 0.0f;
            blue_percent = 100.0f;
          } else if (diff >= 2.0) {
            red_percent = 100.0f;
            green_percent = 0.0f;
            blue_percent = 0.0f;
          } else if (diff > 0) {
            red_percent = (100.0f * (diff / 2.0f));
            green_percent = (100.0f * (1 - (diff / 2.0f)));
            blue_percent = 0.0f;
          } else {
            red_percent = 0.0f;
            green_percent = (100.0f * ((diff + 2.0f) / 2.0f));
            blue_percent = (100.0f * (1 - ((diff + 2.0f) / 2.0f)));
          }
          id(sensor_check).turn_on()
                .set_red(red_percent / 100.0f)
                .set_green(green_percent / 100.0f)
                .set_blue(blue_percent / 100.0f);

I did this today using Claude AI, here’s working yaml that blends from blue to red for 15c - 28c, adjust as needed. You’ll need to change id, entity names, etc.

- id: '<insert your id here>'
  alias: Change WLED Color Based on Temperature
  description: Changes WLED color based on room temperature reading
  trigger:
  - platform: state
    entity_id: sensor.mydht22_temperature
  - platform: time_pattern
    seconds: /10
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.mydht22_temperature
        below: 15
      sequence:
      - target:
          entity_id: light.wled
        data:
          effect: solid
          brightness: 128
          rgb_color:
          - 0
          - 0
          - 255
        action: light.turn_on
    - conditions:
      - condition: numeric_state
        entity_id: sensor.mydht22_temperature
        above: 28
      sequence:
      - target:
          entity_id: light.wled
        data:
          effect: solid
          brightness: 128
          rgb_color:
          - 255
          - 0
          - 0
        action: light.turn_on
    default:
    - target:
        entity_id: light.wled
      data:
        effect: solid
        brightness: 128
        rgb_color: '{% set temp = states(''sensor.mydht22_temperature'')
          | float %} {% set normalized = (temp - 15) / (28 - 15) %} {% set red = (normalized
          * 255) | int %} {% set blue = ((1 - normalized) * 255) | int %} [{{ red
          }}, 0, {{ blue }}]

          '
      action: light.turn_on

I hope that helps.

The problem is that this is an esphome group where people are typically seeking esphome based solutions.

You may say, “well, this HA automation works so who cares?” and thats true, it may work fine but, the problem is the temp sensor and RGB light are both running from the esp board while light effects and automations are somewhere else.

Its generally not the best idea to split things up like that, especially when the effects/automations can be run from the esp board and you can keep the esp board as a standalone device that’s not dependent on HA.

Sorry for the delay in replying to you, I did get it working and posted the full working Yaml file but the part that does the colour changing (that suits my needs) is this

light:
  - platform: rgb
    name: "Bedroom LED"
    id: bedroom_led
    red: redgpio
    green: greengpio
    blue: bluegpio


sensor:
  - platform: dht
    pin: D2
    model: dht22
    temperature:
      name: "Bedroom Temperature"
      on_value_range:
        - below: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 20%
                red: 0%
                green: 0%
                blue: 100%
        - above: 16
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 20%
                red: 0%
                green: 100%
                blue: 0%
        - above: 25
          then:
            - light.turn_on:
                id: bedroom_led
                brightness: 20%
                red: 100%
                green: 0%
                blue: 0%

Hope it helps you :slight_smile: