Hello,
I am running a esp32 as standalone device without wifi and HA which it is controlled by a lumen sensor BH1750 and at some intensity light should open or turn off a relay. The problem occurs now is that once reach all 6 relays on and light intensity drop under a certain threshold, the relays remains on instead turning off if out of their operating range. Cable length used for sensor is RJ45 at 10m long (blue/green for SDA/SCL, orange/brown for 5V/GND). ESP32 is mounted in a ESP32 socket an RJ45 cables connected to screws, well tightened.
Previous problems was that it was restarting often due no wifi, but I read that need to deactivated (I have just commented wifi module) and somehow this went fixed. Also sometimes esp32 enter in safe mode and it restarts 10 times which may trigger relays and blow my devices pcb.
A poor solution is to restart device itself at 20 minutes, but every relay is connected to a device which may got damaged when restart occurs 3-4 times an hour, but may be a code problem, an electrical connection, esp32 itself or a bit of everything.
Below I have attached entire code which runs on_value_range
, and previously tested using scripts, but unsuccessful as well.
name: lumen-lights-3
friendly_name: Lumen_lights_3
name_sensor: lumen_3_sensor
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.12.0
name_add_mac_suffix: false
project:
name: esphome.web
version: '2.3'
on_boot:
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
- delay: 10s
- wait_until:
condition:
lambda: return id(${name_sensor}).state > 5;
esp32:
board: esp32dev
framework:
type: esp-idf
#type: arduino
version: recommended
# Enable logging
logger:
# Enable Home Assistant API
# api:
# encryption:
# key: ""
# # Allow Over-The-Air updates
# ota:
# - platform: esphome
# password: ""
# # 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: !secret hotspot_ssid
# password: !secret hotspot_password
# # 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:
# port: 80
# auth:
# username: admin
# password: admin
i2c:
- id: bus_a #bh1750
sda: GPIO21
scl: GPIO22
scan: true
frequency: 400kHz
sensor:
# - platform: wifi_signal
# name: "${name_sensor} WiFi Signal"
# id: ${name_sensor}_wifi
# update_interval: 15s
# filters:
# - sliding_window_moving_average:
# window_size: 10
# send_every: 10
# send_first_at: 4
# on_value:
# if:
# condition:
# not:
# wifi.connected:
# then:
# - delay: 120s
# - logger.log: WiFi is not connected!
# - wifi.disable:
- platform: bh1750
i2c_id: bus_a
name: ${friendly_name}
address: 0x23
update_interval: 1s
id: ${name_sensor}
accuracy_decimals: 0
filters:
- skip_initial: 3
- median:
window_size: 9
send_every: 5
send_first_at: 3
# - delta: 0.1
- debounce: 500ms
##- throttle: 500ms
##- heartbeat: 2s
on_value_range:
- below: 9999.9
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
- below: 19999.9
above: 10000.0
then:
- delay: 0.5s
- switch.turn_on: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- below: 39999.9
above: 20000.0
then:
- delay: 0.5s
- switch.turn_on: relay1
- delay: 0.5s
- switch.turn_on: relay2
- switch.turn_off: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- below: 69999.9
above: 40000
then:
- delay: 0.5s
- switch.turn_on: relay1
- delay: 0.5s
- switch.turn_on: relay2
- delay: 0.5s
- switch.turn_on: relay3
- switch.turn_off: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- below: 89999.9
above: 70000.0
then:
- delay: 0.5s
- switch.turn_on: relay1
- delay: 0.5s
- switch.turn_on: relay2
- delay: 0.5s
- switch.turn_on: relay3
- delay: 0.5s
- switch.turn_on: relay4
- switch.turn_off: relay5
- switch.turn_off: relay6
- below: 109999.9
above: 90000.0
then:
- delay: 0.5s
- switch.turn_on: relay1
- delay: 0.5s
- switch.turn_on: relay2
- delay: 0.5s
- switch.turn_on: relay3
- delay: 0.5s
- switch.turn_on: relay4
- delay: 0.5s
- switch.turn_on: relay5
- switch.turn_off: relay6
- above: 110000.0
then:
- delay: 0.5s
- switch.turn_on: relay1
- delay: 0.5s
- switch.turn_on: relay2
- delay: 0.5s
- switch.turn_on: relay3
- delay: 0.5s
- switch.turn_on: relay4
- delay: 0.5s
- switch.turn_on: relay5
- delay: 0.5s
- switch.turn_on: relay6
# 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_off: relay2
# - switch.turn_off: relay3
# - switch.turn_off: relay4
# - switch.turn_off: relay5
# - switch.turn_off: relay6
# - delay: 0.5s
# - switch.turn_on: relay1
# - id: ssr_2
# then:
# - switch.turn_off: relay3
# - switch.turn_off: relay4
# - switch.turn_off: relay5
# - switch.turn_off: relay6
# - delay: 0.5s
# - switch.turn_on: relay1
# - delay: 0.5s
# - switch.turn_on: relay2
# - id: ssr_3
# then:
# - switch.turn_off: relay4
# - switch.turn_off: relay5
# - switch.turn_off: relay6
# - delay: 0.5s
# - switch.turn_on: relay1
# - delay: 0.5s
# - switch.turn_on: relay2
# - delay: 0.5s
# - switch.turn_on: relay3
# - id: ssr_4
# then:
# - switch.turn_off: relay5
# - switch.turn_off: relay6
# - delay: 0.5s
# - switch.turn_on: relay1
# - delay: 0.5s
# - switch.turn_on: relay2
# - delay: 0.5s
# - switch.turn_on: relay3
# - delay: 0.5s
# - switch.turn_on: relay4
# - id: ssr_5
# then:
# - switch.turn_off: relay6
# - delay: 0.5s
# - switch.turn_on: relay1
# - delay: 0.5s
# - switch.turn_on: relay2
# - delay: 0.5s
# - switch.turn_on: relay3
# - delay: 0.5s
# - switch.turn_on: relay4
# - delay: 0.5s
# - switch.turn_on: relay5
# - id: ssr_6
# then:
# - delay: 0.5s
# - switch.turn_on: relay1
# - delay: 0.5s
# - switch.turn_on: relay2
# - delay: 0.5s
# - switch.turn_on: relay3
# - delay: 0.5s
# - switch.turn_on: relay4
# - delay: 0.5s
# - switch.turn_on: relay5
# - delay: 0.5s
# - switch.turn_on: relay6
# 8 relay outputs, exposed as switches in Home Assistant
switch:
- platform: gpio
pin:
number: GPIO32
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: gpio
## pin:
## number: GPIO19
## inverted: True
## name: Relay7
## id: relay7
## restore_mode: RESTORE_DEFAULT_OFF
## - platform: gpio
## pin:
## number: GPIO18
## inverted: True
## name: Relay8
## id: relay8
## restore_mode: RESTORE_DEFAULT_OFF
# - platform: template
# name: Logic controller
# optimistic: True
# lambda: |-
# if (id(${name_sensor}).state < 9999.9) {
# id(relay_all_off).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 10000.0 && id(${name_sensor}).state < 19999.9) {
# id(ssr_1).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 20000.0 && id(${name_sensor}).state < 39999.9) {
# id(ssr_2).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 40000.0 && id(${name_sensor}).state < 69999.9) {
# id(ssr_3).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 70000.0 && id(${name_sensor}).state < 89999.9) {
# id(ssr_4).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 90000.0 && id(${name_sensor}).state < 109999.9) {
# id(ssr_5).execute();
# return true;
# }
# else if (id(${name_sensor}).state > 110000.0) {
# id(ssr_6).execute();
# return true;
# }
# else {
# id(relay_all_off).execute();
# return false;
# }