From f94bacd3008027789008f84704db77caa58899a1 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 22 Nov 2022 12:59:09 +0100 Subject: esphome: add bedlight --- esphome/config/bedlight.yaml | 173 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 esphome/config/bedlight.yaml (limited to 'esphome/config') diff --git a/esphome/config/bedlight.yaml b/esphome/config/bedlight.yaml new file mode 100644 index 0000000..81392b0 --- /dev/null +++ b/esphome/config/bedlight.yaml @@ -0,0 +1,173 @@ +esphome: + name: bedlight + platform: ESP8266 + board: esp01_1m + + # light default values + on_boot: + then: + - light.control: + id: bed + state: off + brightness: 50% + color_temperature: 3000 K + +# Enable logging +logger: + # make RXD / TXD available + baud_rate: 0 + +# Enable Home Assistant API +api: + password: !secret api_password + +# OTA +ota: + password: !secret ota_password + +# WiFi Credentials +wifi: + domain: .lan + ssid: !secret wifi_ssid + password: !secret wifi_password + + # Enable fallback hotspot (captive portal) in case wifi connection fails + ap: + ssid: "Bedlight Fallback Hotspot" + password: !secret wifi_ap_password + +captive_portal: + +light: + - id: bed + platform: cwww + name: "Bed" + cold_white: pwm_w_t + warm_white: pwm_b_t + cold_white_color_temperature: 6536 K + warm_white_color_temperature: 2000 K + restore_mode: RESTORE_AND_OFF + +output: + - id: gpio_r + platform: gpio + pin: GPIO12 + + - id: gpio_g + platform: gpio + pin: GPIO15 + + - id: pwm_b + platform: esp8266_pwm + pin: GPIO14 + + - id: pwm_b_t + platform: template + type: float + write_action: + - output.set_level: + id: pwm_b + level: !lambda |- + // limit min power + if (state > 0.0 && state < 0.0006) + return 0.0006; + if (state <= 0.0002) + return 0.0002; + return state; + + - id: pwm_w + platform: esp8266_pwm + pin: GPIO4 + + - id: pwm_w_t + platform: template + type: float + write_action: + - output.set_level: + id: pwm_w + level: !lambda |- + // limit min power + if (state > 0.0 && state < 0.0006) + return 0.0006; + if (state <= 0.0002) + return 0.0002; + return state; + + - id: gpio0 + platform: gpio + pin: GPIO0 + + - id: gpio_txd + platform: gpio + pin: GPIO1 + + - id: gpio_rxd + platform: gpio + pin: GPIO3 + +globals: + - id: g_dim_level + type: float + initial_value: '0.02' + - id: g_min_dim_level + type: float + initial_value: '0.02' + +script: + - id: reset_dim_direction + mode: restart + then: + - delay: 10s + - lambda: 'id(g_dim_level) = abs(id(g_dim_level));' + +binary_sensor: + - id: user_button + platform: gpio + pin: GPIO5 + filters: + - delayed_on: 30ms + on_click: + min_length: 30ms + max_length: 350ms + then: + - logger.log: "on_click" + # TODO: if brightness is low, turn on + transition is also slow + - light.toggle: bed + # dim up per default unless brightness is 95% + - lambda: |- + id(g_dim_level) = abs(id(g_dim_level)); + if (id(bed).current_values.get_brightness() >= 0.95) + id(g_dim_level) *= -1; + on_press: + then: + - delay: 500ms + - if: + condition: + # make sure button is still pressed + binary_sensor.is_on: user_button + then: + - while: + condition: + binary_sensor.is_on: user_button + then: + - logger.log: "on_press" + - light.dim_relative: + id: bed + relative_brightness: !lambda |- + auto brightness = id(bed).current_values.get_brightness(); + // start dimming at this value + if (!id(bed).current_values.is_on()) + return abs(id(g_min_dim_level)); + // don't dim to zero + if (id(g_dim_level) < 0 && brightness + id(g_dim_level) <= id(g_min_dim_level)) + return 0.0; + return id(g_dim_level); + transition_length: 100ms + - delay: 0.1s + # reverse dim direction + - lambda: 'id(g_dim_level) = id(g_dim_level) * -1;' + - script.execute: reset_dim_direction + on_double_click: + then: + - homeassistant.event: + event: esphome.double_click -- cgit v1.2.3