-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Door sensor deep sleep for bk7231n board #2467
Comments
I didn't know how to enable deep sleep too |
My temporary solution
|
ThanksMy iPad will find youOn Dec 24, 2023, at 08:44, Ivan Shevchenko ***@***.***> wrote:
My temporary solution
script:
- id: scr
then:
- wifi.enable:
- delay: 30s
- wifi.disable:
mode: restart
binary_sensor:
- platform: gpio
device_class: door
name: Door
pin: P22
on_state:
then:
- script.execute: scr
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Having functional deep sleep will help in a long run. in mean time, I've been trying to get up to speed with the project |
I've implemented your solution as I'm trying to find the deep sleep code for the exact same type of sensor... Below my running ESPHome config as it works now, quite well. Please comments are needed as I'm not really sure it is efficent enough!
|
Any updates? |
There is some work going on the LibreTiny to support it in ESPHome, but I believe it will take some time until we have Deep Sleep for these chips. |
Does the above script work? What is the power consumption? I have two water sensors operating with 2xAAA batteries and I'd really like to replace the tuya firmware with something else. |
grego1981's code works great. |
I have found a nice deep_sleep component for bk72xx / cb3s processor. Additionally added code for delayed deep sleep and forced manual mode as well. It took 2-3 days for me to fine tune this, pasting it here to share the info and love of bkn :D hope it helps someone. # PINOUT: CB3S Module Datasheet - https://developer.tuya.com/en/docs/iot/cb3s?id=Kai94mec0s076
# UART flash with: https://github.com/libretiny-eu/ltchiptool
# UART flashing: GND(pin9),VCC3V(pin8),TX(pin16),RX(pin15) / connect CEN(pin3) to GND(pin9)
# CB3S / tuya chip - https://community.home-assistant.io/t/tuya-door-sensor-configuration/673484
# MORE INFO on the board: https://www.elektroda.com/rtvforum/topic3960149.html
# MORE INFO on the board: https://www.elektroda.com/rtvforum/topic3914412-30.html#gallery-2
### TODO & FURTHER IDEAS
# - additional pins?
# - explore manual "stored" server side update switch
# - switch off w restore LED on activate
# - support Bluetooth LE.
bk72xx:
# board: generic-bk7231n-qfn32-tuya
board: cb3s # FAMILY_BK7231N
framework:
version: dev
esphome:
name: z-test-door
friendly_name: z-test-door
on_boot:
# https://esphome.io/components/esphome.html#on-boot
- priority: 600.0 # 600.0: This is where most sensors are set up.
then:
- component.update: VCC
- switch.turn_on: batt_dv # start measuring voltage
- priority: 100.0
then:
- output.turn_on: activity_led_output
- priority: -100 # 200.0: Network connections like MQTT/native API are set up at this priority.
then:
- component.update: VCC
- component.update: homeassistant_time
- component.update: update_time
- script.execute: scr
logger:
# level: info
api:
globals:
- id: first_connect_ms_int
type: uint32_t
initial_value: "0"
ota:
on_begin:
- output.turn_on: activity_led_output
# on_progress:
# - output.toggle: light_output
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# fast_connect: True
# manual_ip:
# gateway: 192.168.0.1
# static_ip: 192.168.0.212
# subnet: 255.255.255.0
on_connect:
- lambda: |-
if (id(first_connect_ms_int) == 0) id(first_connect_ms_int) = esphome::millis();
- component.update: first_connect_ms
web_server:
version: 3
### we need this reset as there is no reset action with deep_sleep
### see: https://github.com/esphome/feature-requests/issues/2467 - original idea for workaround
### see: https://github.com/esphome/feature-requests/issues/1653 - feature request
script:
- id: scr
then:
- deep_sleep.prevent
# - wifi.enable:
- delay: 10s
# - wifi.disable:
- script.execute: check_deep_sleep
mode: restart
- id: check_deep_sleep
then:
- deep_sleep.prevent
- if:
condition:
- switch.is_off: manual_mode
then:
- output.turn_off: activity_led_output
- deep_sleep.allow
mode: restart
binary_sensor:
- platform: gpio
device_class: door
name: Door
pin:
number: P8 # pin-13 hall sensor, GPIOP_8, which corresponds to P8 of the IC, PWM 2
allow_other_uses: true
on_state:
- if:
condition:
- switch.is_off: manual_mode
then:
- output.turn_on: activity_led_output
- delay: 1000ms
- output.turn_off: activity_led_output
else:
- light.turn_on:
id: activity_led
effect: pulse
- script.execute: scr
icon: mdi:door
- platform: gpio
id: bottom_button
pin:
number: P7 # pin-14 bottom button, GPIOP_7, which corresponds to P7 of the IC, PWM 1
inverted: true
allow_other_uses: true
name: "Bottom button"
icon: mdi:button-pointer
on_state:
- script.execute: check_deep_sleep
on_multi_click:
- timing:
- on for at least 1s
then:
- switch.toggle: manual_mode
switch:
- platform: gpio
# enables measuring voltage - see https://www.elektroda.com/rtvforum/topic3914412-30.html#gallery-2
id: batt_dv
pin: P14
- platform: template
# manual mode for OTA
optimistic: True
name: Manual mode
id: manual_mode
on_turn_off:
- script.execute: check_deep_sleep
on_turn_on:
- script.execute: check_deep_sleep
- light.turn_on:
id: activity_led
effect: pulse
entity_category: config
icon: mdi:sleep-off
light:
# - platform: binary
# name: "Activity LED"
# output: light_output
# id: activity_led_output
# icon: mdi:lamp
# effects:
# - strobe:
- platform: monochromatic
name: "Activity LED"
output: activity_led_output
id: activity_led
icon: mdi:lamp
effects:
- strobe:
- pulse:
transition_length:
on_length: 1s
off_length: 2s
update_interval: 3s
output:
# - id: light_output
# platform: gpio
# pin:
# number: P26 # P26=pin-5
# inverted: true
- id: activity_led_output
platform: libretiny_pwm
frequency: 1000 Hz
pin:
number: P26 # P26=pin-5
inverted: true
time:
- platform: homeassistant
id: homeassistant_time
on_time_sync:
then:
- component.update: update_time
sensor:
- platform: adc
pin: ADC3 # ADC3 = pin-2 (internally connnected), gives valid only if "batt_dv" is enabled (see more info there)
id: "VCC"
name: "Battery Level"
filters:
- multiply: 2.4
- clamp:
min_value: 0.0
max_value: 3.0
ignore_out_of_range: true
on_value:
then:
- component.update: battery_level
icon: mdi:battery
- platform: template
name: "Battery Level (%)"
unit_of_measurement: "%"
update_interval: 86400s
id: battery_level
lambda: |-
return ((id(VCC).state /3.0) * 100.00);
icon: mdi:battery
- platform: template
# temporary solution to measure startup time
name: first_connect_ms
id: first_connect_ms
lambda: |-
return id(first_connect_ms_int);
- platform: internal_temperature
name: Internal Temperature
icon: mdi:thermometer
text_sensor:
- platform: template
id: update_time
name: "Current time"
lambda: |-
char str[17];
time_t currTime = id(homeassistant_time).now().timestamp;
strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
return { str };
icon: "mdi:clock"
external_components:
- source: github://Xmister/libretuya-esphome@deep-sleep
components: [deep_sleep]
# - source: github://libretiny-eu/libretiny-esphome@dev
# components: [ deep_sleep ]
# - source:
# type: git
# url: https://github.com/bkaufx/esphome
# components: [ web_server_base ]
deep_sleep:
id: deep_sleep_control
sleep_duration: 24h
run_duration: 10s
wakeup_pins:
- pin:
number: P7 # pin-14 bottom button
allow_other_uses: true
wakeup_pin_mode: IGNORE
- pin:
number: P8 # pin-13 hall sensor
allow_other_uses: true
wakeup_pin_mode: INVERT_WAKEUP |
@afarago Welcome to the club 🥲 This was my initial PR to prepare the deep sleep for BKN: esphome/esphome#5739 |
Thanks for your awesome work! Is there anything missing for the PR to be merged? |
How fast does it use batteries with this code? |
I have not measured the deep_sleep current, yet it should be the same as the original tuya firmware. PIN depends on your board, mine is as above in the links. You could check the integrated circuit by eyes or by a manual multimeter. |
It seems that deep sleep is not working. |
Maybe my measurement is to bad, but I don't see much of an effect for disabling wifi for some time 1..2min.
|
Deep sleep is working, measured current consumption. The problem is that device keeps getting out of deep sleep for no reason (see led light). It should only wake every 24h and on pin change, but it isnt. Any ideas? V. |
We might spot the error seeing the complete yaml file. Would you add it to this discussion? Changing Wifi power save and transmit power does not make a sense imo, as ideally the device is like 10s awake every now and then, so if deep_sleep is operational you practically have zero wake time. |
I have perfomed two test scenarios with the deep_sleep component above.
Hope this helps, certainly impressive data for me that predicts >1y runtime. |
Want use by Rollerblind switch in energy saving way. So I used @afarago setup to adapt it. I could see status LED starts blinking (is it to get Wifi connection?). Usually it does it 13 times, than a little break, starting again. Than after few blinks the 3LEDs for the keys blink 2 times. This is from "on_boot" section to indicates a reboot (I did it because I have no serial connection and Wifi is off for logging). After few seconds the LEDs for the 3 keys are off. Guess this is the enery saving mode. I measure around 0.5W (but maybe that is the smalles number I can get with this device - it is a 220V device). What am I doing wrong?
|
@afarago @Xmister : It seems not doing anything but wake up and reboot. It is difficult to follow with no (serial log) attached.
|
No updates here? |
it looks like there is an never ending deep_sleep loop. this is my yaml: esphome: bk72xx: logger: api: ota:
wifi: captive_portal: web_server: light:
text_sensor:
button: binary_sensor:
sensor:
external_components:
deep_sleep: |
Hi, if anyone is interested I have a somewhat working external component that implements deep sleep for bk7231 boards. |
@Xmister , I can now see that you were the original poster of the PR I based my external component on. So, the cycle loops again :) |
Sorry for my begginers question but how to use it? |
Just follow the official guide for using external components. external_components:
- source:
type: git
url: https://github.com/szupi-ipuzs/esphome-external-components
ref: main
components: [ deep_sleep_libretiny ] And then follow the description. |
Yes, I'm sure. This should work. |
Ok, I can now see that you're right, the last curly brace is out of place. But your code snippet is also not correct (maybe due to white chars?). #ifdef USE_DEEP_SLEEP
namespace esphome::deep_sleep {
extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
}
#endif I will change it in the readme of my external component. |
@toughvj I've also noticed I haven't checked in all the needed changes before. I've just fixed it. Please download the external component locally again. It should work fine now. |
I must say everything works somewhat good, except it takes ~35s after open/close sensor for it to change status. Any ideas what could be wrong? Takes really long time to wake... |
I also use this with a door sensor and sadly I observe similar behavior. The device actually wakes up immediately, but it takes veery long time to connect to wifi. I know this because I told it to start blinking at boot and then turn on the light completely on HA connection. |
So what is the current state of deep sleep support on BK7231N ? Yes, they do take much longer to connect to wifi compared to the stock firmware, but with the 2 aaa batteries in there it lasts me for 6-12 months, so I'm not complaining. |
Describe the problem you have/What new integration you would like
Add door sensor deep sleep for bk7231n boardPlease describe your use case for this integration and alternatives you've tried:
ESPHOME deep sleep not working for bk72xx platformAdditional context
The text was updated successfully, but these errors were encountered: