I have built an device that use lumen sensor BH1750, esp32 and relays. I have several values like 10k, 20k lumen, 40k, 70k and so on that trigger to toggle a relay (e.g 45k lumen, 3 relays on, 39k 2 relay on, 9k with no relay on). Previously I have used on_range_value
with below
and above
value, but the triggers were not working correctly when covered the sensor, or when values was decreasing, relays state did not changed.
Up now I am using script to trigger all relays with their specific values which should be when in a range, but it seems that it is triggering few times per second. I am concerned if the relays stays on when the script trigger a specific relays e.g: relay 1 is already on, but script trigger relay1 ON few times a second. Visually I am not sseeing led of relay blinking or glaring, nor relay doing noise as toggle itself.
Also, I may have to add some logic to restart sensor when value it is not provided by sensor due only way it’s working now it’s to restart the esp32.
substitutions:
name: lumen-lights-1
friendly_name: Lumen_lights_1
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.10.0
name_add_mac_suffix: false
project:
name: esphome.web
version: '1.0'
on_boot:
then:
# - delay: 120s
# - wifi.disable:
- lambda: 'id(lumen_1_sensor).publish_state(1.0);'
esp32:
board: esp32dev
framework:
type: esp-idf
version: recommended
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "xxx"
# Allow Over-The-Air updates
ota:
- platform: esphome
password: "xxx"
# Allow provisioning Wi-Fi via serial
improv_serial:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Lumen-Lights-1 Fallback Hotspot"
password: "xxxx"
# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:
dashboard_import:
package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
import_full_config: true
# To have a "next url" for improv serial
web_server:
i2c:
sda: 21
scl: 22
scan: False
sensor:
- platform: wifi_signal
name: "Lumen_lights WiFi Signal"
id: lumen_1_sensor_wifi
update_interval: 15s
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 15
send_first_at: 1
on_value:
if:
condition:
not:
wifi.connected:
then:
- delay: 120s
- logger.log: WiFi is not connected!
- wifi.disable:
- platform: bh1750
name: ${friendly_name}
address: 0x23
update_interval: 1s
id: lumen_1_sensor
filters:
- throttle: 100ms
- delta: 0.1
- skip_initial: 3
- timeout:
timeout: 300s #reset value after 5 min
value: 0
script:
- id: relay_all_off
then:
- switch.turn_off: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- id: ssr_1
then:
- switch.turn_on: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- id: ssr_2
then:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_off: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- id: ssr_3
then:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_on: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- id: ssr_4
then:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_on: relay3
- switch.turn_on: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- id: ssr_5
then:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_on: relay3
- switch.turn_on: relay4
- switch.turn_on: relay5
- switch.turn_off: relay6
- id: ssr_6
then:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_on: relay3
- switch.turn_on: relay4
- switch.turn_on: relay5
- switch.turn_on: relay6
# 8 relay outputs, exposed as switches in Home Assistant
switch:
- platform: gpio
pin:
number: GPIO16
inverted: True
name: Relay1
id: relay1
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
pin:
number: GPIO14
inverted: True
name: Relay2
id: relay2
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
pin:
number: GPIO27
inverted: True
name: Relay3
id: relay3
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
pin:
number: GPIO26
inverted: True
name: Relay4
id: relay4
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
pin:
number: GPIO25
inverted: True
name: Relay5
id: relay5
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
pin:
number: GPIO33
inverted: True
name: Relay6
id: relay6
restore_mode: RESTORE_DEFAULT_OFF
- platform: template
name: Logic controller
optimistic: True
lambda: |-
if (id(lumen_1_sensor).state < 9999.9) {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(relay_all_off).execute();
return true;
}
else if (id(lumen_1_sensor).state > 10000.0 && id(lumen_1_sensor).state < 19999.9) {
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(ssr_1).execute();
return true;
}
else if (id(lumen_1_sensor).state > 20000.0 && id(lumen_1_sensor).state < 39999.9) {
id(ssr_1).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(ssr_2).execute();
return true;
}
else if (id(lumen_1_sensor).state > 40000.0 && id(lumen_1_sensor).state < 69999.9) {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(ssr_3).execute();
return true;
}
else if (id(lumen_1_sensor).state > 70000.0 && id(lumen_1_sensor).state < 89999.9) {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(ssr_4).execute();
return true;
}
else if (id(lumen_1_sensor).state > 90000.0 && id(lumen_1_sensor).state < 109999.9) {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_6).stop();
id(ssr_5).execute();
return true;
}
else if (id(lumen_1_sensor).state > 110000.0) {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).execute();
return true;
}
else {
id(ssr_1).stop();
id(ssr_2).stop();
id(ssr_3).stop();
id(ssr_4).stop();
id(ssr_5).stop();
id(ssr_6).stop();
id(relay_all_off).execute();
return false;
}