Greetings… I wrote the code below quite some time back, and whilst I know it’s not perfect it has done the job in our 8 year old daughters bedroom.
Our daughter has been asking lately if I can tweak the program to have the lights come on at bedtime around 9pm at 20% brightness so the room is not dark. And when the PIR detects any movement during the night the lights increase to 70 or 80% brightness, and then go back to 20% brightness when the PIR doesn’t detect any movement.
I will admit I am not the greatest yaml programmer, so perhaps this is a good time to revamp the code, add the new features our daughter is seeking, and perhaps simplify the code based on some recommendations from the gurus in this forum.
Here’s the current code…
substitutions:
off_hour: '5'
esp8266:
board: d1_mini
framework:
version: recommended
esphome:
name: esp4-bdrmlights
on_boot:
- light.turn_off:
id: bdrm_led_lights
transition_length: 2.5s
status_led:
pin: GPIO2
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "<REDACTED>"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp4-Bdrmlights Fallback Hotspot"
password: "<REDACTED>"
captive_portal:
script:
- id: turn_off
mode: restart
then:
- delay: 90s
- light.turn_off:
id: bdrm_led_lights
transition_length: 7.5s
- logger.log: "LED lights are OFF"
binary_sensor:
- platform: gpio
pin: D5
id: bdrm_pir
name: "Bdrm PIR Sensor"
device_class: motion
on_press:
then:
- if:
condition:
and:
# If it's night time
- sun.is_below_horizon:
- lambda: |-
auto hour = id(my_time).now().hour;
return hour > 12 || hour < ${off_hour};
then:
#Turn on the LED lights
- light.turn_on:
id: bdrm_led_lights
brightness: 40%
effect: random
- logger.log: "LED lights are ON"
on_release:
- script.execute: turn_off
sun:
latitude: !secret latitude
longitude: !secret longitude
time:
platform: homeassistant
timezone: !secret timezone
id: my_time
on_time:
- hours: ${off_hour}
minutes: 0
seconds: 0
then:
- light.turn_off:
id: bdrm_led_lights
light:
- platform: neopixelbus
variant: WS2812
pin: D1
num_leds: 54
type: GRB
name: "FastLED WS2812B Light"
id: bdrm_led_lights
default_transition_length: 7.5s
effects:
# Use default parameters:
- random:
- random:
name: "Slow Random Effect"
transition_length: 30s
update_interval: 20s
- random:
name: "Fast Random Effect"
transition_length: 4s
update_interval: 5s
- pulse:
- pulse:
name: "Fast Pulse"
transition_length: 0.5s
update_interval: 0.5s
- pulse:
name: "Slow Pulse"
# transition_length: 1s # defaults to 1s
update_interval: 2s
- addressable_rainbow:
- addressable_rainbow:
name: "Rainbow Effect"
speed: 10
width: 50
- addressable_color_wipe:
- addressable_color_wipe:
name: "Color Wipe Effect"
colors:
- red: 10%
green: 90%
blue: 25%
num_leds: 5
- red: 35%
green: 15%
blue: 60%
num_leds: 1
add_led_interval: 100ms
reverse: false
- addressable_fireworks:
- addressable_fireworks:
name: "Fireworks Effect"
update_interval: 32ms
spark_probability: 10%
use_random_color: true
fade_out_rate: 120
- addressable_scan:
- addressable_scan:
name: "Scan Effect"
move_interval: 100ms
scan_width: 5
- addressable_random_twinkle:
- addressable_random_twinkle:
name: Random "Twinkle Effect"
twinkle_probability: 15%
progress_interval: 32ms
I was thinking I could just set a timer based on the position of the sun… i.e. so when the sun is below the horizon the lights come on at 20% brightness. But I am not sure how I might integrate that into the existing code?
Here’s something I thought I might be able to use…
sun:
latitude: !secret latitude
longitude: !secret longitude
on_sunset:
- elevation: -10°
then:
- light.turn_on:
id: bdrm_led_lights
I am sure there’s a much better way to do this… so any ideas/tips appreciated.