Thank you very much, the whole thing works perfectly.
Following is my final code, hoping that someone in the community will benefit form it.
# ESPHome program to test how to control WS8212 LED strip
# WS8212 LED Strip can be divided in segment, each one can be address to Turn On/Off as well as control Brightness and Flash_length
# Credit and many thanks goes to CK Smart (home assistant community) for designing the Script and how to use it
# Gaston Paradis June 17, 2023
substitutions:
devicename: esp32-04
friendly_name: "Testing LED"
comment: "Testing WS2812LED"
esp_id: ESP32_04
board_type: esp32doit-devkit-v1
led_segment_1 : l1 # Range 1 of WS8212 LED use in SCRIPT
led_segment_2 : l2 # Range 2 of WS8212 LED in SCRIPT
esphome:
friendly_name: ${friendly_name}
comment: ${comment}
on_boot:
priority: -10
then:
- wait_until:
api.connected:
- logger.log: API is connected!
- lambda: |-
std::vector<float> ws2812{1, .5, 0,0,1,3000};
id(state_on_off)->execute(ws2812);
- switch.turn_on: led_enabled
esp32:
board: ${board_type}
framework:
type: arduino
packages:
base: !include
file: package/basic.yaml
vars:
Uptime_update_inverval: 10min
light:
- platform: neopixelbus # Configure WS2812 LED
# name: "Status LED" # Uncomment to make visable in Home Assistant and local WEB
id: activity_led
variant: WS2812
pin: GPIO25
num_leds: 2
flash_transition_length: 1500ms
type: GRB
restore_mode: ALWAYS_OFF
- platform: partition # Use LED [0] from the light with ID activity_led
# name: "Partition Light 1" # Uncomment to make visable in Home Assistant and local WEB
id: ${led_segment_1}
segments:
- id: activity_led
from: 0
to: 0
- platform: partition # Use LED [1] from the light with ID activity_led
# name: "Partition Light 2" # Uncomment to make visable in Home Assistant and local WEB
id: l2
segments:
- id: ${led_segment_1}
from: 1
to: 1
switch:
- platform: template # Define switches to control LED from HA # LED enabled
name: "${friendly_name} LED enabled"
id: led_enabled
icon: mdi:alarm-light-outline
optimistic: true
restore_state: true
entity_category: config
button:
- platform: template # Define buttons for turning LED On/Off via HA
name: Turn On/Off LED
id: turn_led
# Optional variables:
icon: "mdi:button"
on_press:
then:
if:
condition:
- light.is_on: l1
then:
- light.turn_off: l1
else:
if:
condition:
- switch.is_on: led_enabled
then: # Array [id, Brightness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{1, .75, 0,0,1,2000};
id(state_on_off)->execute(ws2812);
sensor:
- platform: homeassistant # Temperature Salon
name: "Sensor From Home Assistant"
entity_id: sensor.esp_15_temperature
internal: false
on_value_range:
- below: 22.0 # Array [id, Brightness, red, green, blue, flash_length]
then: # Array [id, Brightness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{1, .5, 0, 0, 1, 2000};
id(state_on_off)->execute(ws2812);
- above: 22.0 # Array [id, Brightness, red, green, blue, flash_length]
below: 30.0
then: # Array [id, Brightness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{1, .5, 0, 1, 0, 2000};
id(state_on_off)->execute(ws2812);
- above: 30.0 # Array [id, Brightness, red, green, blue, flash_length]
then:
- lambda: |-
std::vector<float> ws2812{1, .5, 1, 0, 0, 2000};
id(state_on_off)->execute(ws2812);
- platform: dht # DHT Temperature/Humidity
model: AUTO_DETECT
pin: GPIO21 # SDA
temperature:
name: ${esp_id} Temperature
humidity:
name: ${esp_id} Humidity
update_interval: 15s
binary_sensor:
- platform: homeassistant
id: power_led
entity_id: input_boolean.msg_flag_contact_ss_porte_av
on_press: # Array [id, Brightness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{2, .5, 1, 0, 0, 2000};
id(state_on_off)->execute(ws2812);
on_release: # Array [id, Brightness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{2 ,1 , 0, 1, 0, 4000};
id(state_on_off)->execute(ws2812);
script:
- id: state_on_off
parameters:
ws2812: float[]
then:
- lambda: |-
// Extend this map with all the lights that you have
static std::map<float, light::LightState*> light_map {
{ 1, id(${led_segment_1}) },
{ 2, id(${led_segment_2}) },
};
auto iterator = light_map.find(ws2812[0]);
if (iterator != light_map.end()) {
auto light = iterator->second;
light->turn_on()
.set_brightness(ws2812[1])
.set_red(ws2812[2])
.set_green(ws2812[3])
.set_blue(ws2812[4])
.set_flash_length(ws2812[5])
.perform();
}
time:
- platform: sntp
id: timer
on_time:
# Every minute
- seconds: 0
minutes: /1
then:
- repeat:
count: 5
then: # Array [id, brigthness, red, green, blue, flash_length]
- lambda: |-
std::vector<float> ws2812{2, .25, 1, 0, 0, 500};
id(state_on_off)->execute(ws2812);
- delay: 1s