logo elektroda
logo elektroda
X
logo elektroda

Tuya Water Sensor WiFi DY-SQ400B Flashing, Configuration for OBK Esphome/Tasmota style firmware

io2345 10599 113
Best answers

How to flash and configure the Tuya DY-SQ400B water sensor with its CB3S board for OpenBeken?

Flash it as a BK7231N/CB3S device, not TuyaMCU: make a 2 MB backup with BK7231GUIFlashTool first, then use the same tool’s Write option to flash OpenBeken [#21070016] [#21070089] [#21070895] The working pin map ended up as GPIO7 PWM for buzzer/LED on channel 5, GPIO8 DoorSnsrWSleep_nPup on channel 0, GPIO14 BAT_Relay on channel 1, GPIO23 BAT_ADC on channel 2, GPIO24 Btn_ScriptOnly, and GPIO26 LED on channel 5 [#21095640] For wake/sleep problems, the thread recommends DSEdge tuning (0/2 were both tried), and if the button causes unwanted actions, setting it to Btn_ScriptOnly; accidental childlock/disable-autostart flags should be cleared [#21072613] [#21086332] [#21403880] The final scripts used DSTime at the top of autoexec.bat for timed wakeups, with a loop that clears DSTime while $CH0==1 and an addChangeHandler to drive channel 5 to 80 when channel 0 goes high; DSTime must be the first line when used [#21089513] [#21091186] [#21095624] [#21095640] Battery reporting was calibrated with Battery_Setup values around 2800–2900 min, 3700–4100 max, 1.94 vdivider, plus Battery_cycle 20, and the new DSTime second argument can be used to force a periodic wakeup for battery updates [#21397320] [#21089513]
ADVERTISEMENT
  • #91 21383516
    samoswall
    Level 5  
    >>21380844
    Yes, thank you! Moving to 1 line of DSTime works!

    Added after 33 [minutes]:

    A general question!
    We use the DoorSensor, and logically, opening is an important event!
    For a water leak sensor, on the contrary, closing is an important event.
    A binary sensor is automatically published in the Home assistant mqtt discovery.
    
    {
      "dev": {
        "ids": [
          "Water_Leak_Posudomoika"
        ],
        "name": "Posudomoika",
        "sw": "1.18.12",
        "mf": "Beken Corporation",
        "mdl": "BK7231N",
        "cu": "http://192.168.1.23/index"
      },
      "name": "0",
      "~": "WaterLeakPosudomoika",
      "pl_on": "0",          <-- this
      "pl_off": "1",          <-- this
      "uniq_id": "Water_Leak_Posudomoika_binary_sensor_0",
      "qos": 1,
      "stat_t": "~/0/get"
    }
    

    It would be more correct to change the water leak sensor.
    
      "pl_on": "1",
      "pl_off": "0",
    

    And add the device class moisture.
    I've set it up temporarily using MQTT Explorer.
    Is there any way to configure this, in the configuration for automatic changes?
  • ADVERTISEMENT
  • #92 21397272
    florianreus
    Level 5  
    Do you have a final autoexec script?
    - using the last ones sounds the alarm immediately
    - STime or DSTime? or is this the same?
    I'm sorry, I'm a beginner at openbeken.
  • #93 21397320
    samoswall
    Level 5  
    >>21397272
    STime does not exist (DSTime is correct)
    My working autoexec.bat
    
    DSTime 40 43200
    Battery_Setup 2800 4100 1.94
    Battery_cycle 20
    delay_s 2
    again:
    delay_s 1
    if $CH0==1 then DSTime clear
    if $CH0==1 then setChannel 5 80
    goto again
    
  • #94 21397367
    florianreus
    Level 5  
    hmm.
    then the alarm sounds immediately, and can not be turned off.
    the input is configured as doorsnsrwsleep_npup from the template.
  • ADVERTISEMENT
  • #95 21397371
    samoswall
    Level 5  
    >>21397367
    As long as there is water, do not turn it off
  • #96 21397599
    florianreus
    Level 5  
    There's no water.
    With "old" autoexec alarm sounded when I connected the 2 pins. Now the alarm starts directly after boot
    And sleep did never work.
  • #97 21397688
    samoswall
    Level 5  
    florianreus wrote:
    Now the alarm starts directly after boot

    Flag 42 - off
  • #98 21401601
    florianreus
    Level 5  
    thank you!

    last problem - it is never entering sleep state. Any idea? anywhere I can debug?
  • ADVERTISEMENT
  • #99 21401937
    samoswall
    Level 5  
    >>21401601
    Insert DSEdge 0 into autoexec.bat in 2 lines
    DSEdge 0 was not activated in my configuration.
    But after several flashbacks, it was activated.
  • #100 21403880
    florianreus
    Level 5  
    and some trivial error - now I know I have to load the driver first.
    This is my working autoexec:

    startDriver DoorSensor
    DSTime 40 43200
    DSEdge 2
    Battery_Setup 2800 4100 1.94
    Battery_cycle 20
    delay_s 2
    again:
    delay_s 1
    if $CH0==1 then DSTime clear
    if $CH0==1 then setChannel 5 80
    goto again
  • ADVERTISEMENT
  • #101 21403900
    samoswall
    Level 5  
    >>21403880
    DSEdge 0
    The "startDriver DoorSensor" is not necessary, because it starts automatically during configuration pins "8": "DoorSnsrWSleep_nPup;0"
  • #102 21405342
    florianreus
    Level 5  
    I'm quite sure pin 8 is configured. But without explicitly loading the driver I get "unknown command" for DSxx commands.

    Thanks for the help, and maybe my settings help someone.
  • #103 21417444
    tobb
    Level 9  
    Does anyone have experience with a different power supply? I have some spare 3.6 - 3.7 volt batteries that I could use. Will they fit? Will the voltage not be too high for this device?
  • #104 21417458
    tobb
    Level 9  
    Does anyone have experience with different methods of powering this device? I have some spare rechargeable batteries (Li-Ion, Li-MH) 3.6-3.7V. Will it work, or will the voltage be too high?
  • #105 21417473
    LEDówki
    Level 43  
    You have a battery of 3 batteries connected in series. The minimum voltage is 3 V or 2.7 V when you charge this battery immediately. The maximum voltage of 3x1.54 V = 4.62 V can damage the circuits. You need to check what voltage they can be powered up to. A 2-battery battery may be a bit too low a voltage, as we have between 1.8 V and 3.08 V.
  • #106 21417489
    florianreus
    Level 5  
    I powered my first device using a Li-ion battery directly. It's not working anymore, but did work some time. It might be because of the too high voltage.
    My current device is powered with another 3.7 V battery, but I put in a Zener diode (I found in the house) in front so the voltage is limited to 3.3. This is working good.
  • #107 21417572
    LEDówki
    Level 43  
    LiFePO4 has a 3.65 V maximum. It does not need to be charged to the maximum voltage either. Discharge has to be finished at 2.5 V.
    NiMH 2 pieces connected in series will simulate a CR123A cell quite well Link .
  • #108 21417597
    tobb
    Level 9  
    Thank you for your help, I will try with 2xNiMH to start with
  • #109 21433583
    luki6991
    Level 2  
    Hello. Does anyone have a final autoexec.bat that is fully functional? I have a problem where my device cannot be awakened by high water after about 10 minutes. If someone has a completely working configuration, I would be grateful!
  • #110 21437976
    io2345
    Level 9  
    The latest working autoexec.bat is in thread #100
  • #111 21440314
    luki6991
    Level 2  
    It still doesn’t work. Here is my import text:
    {
    "vendor": "Tuya",
    "bDetailed": "0",
    "name": "Floor sitting Water Leak Flood Sensor Alarm",
    "model": "DY-SQ400B",
    "chip": "BK7231N",
    "board": "CB3S",
    "flags": "8",
    "keywords": [
    "CR123A",
    "battery",
    "buzzer",
    "detector"

    ],
    "pins": {
    "7": "PWM;5",
    "8": "DoorSnsrWSleep_nPup;0",
    "14": "BAT_Relay;1",
    "23": "BAT_ADC;2",
    "24": "Btn_ScriptOnly;1",
    "26": "LED;5"
    },
    "command": "DSEdge 0",
    "image": "https://obrazki.elektroda.pl/6240569600_1714757661.jpg",
    "wiki": "https://www.elektroda.com/rtvforum/topic4052199.html"
    }

    Here is my autoexec.bat:
    startDriver DoorSensor
    DSTime 40 43200
    DSEdge 2
    Battery_Setup 2800 4100 1.94
    Battery_cycle 20
    delay_s 2
    again:
    delay_s 1
    if $CH0==1 then DSTime clear
    if $CH0==1 then setChannel 5 80
    goto again

    My device falls into such a deep sleep after a few minutes that not even high water can wake it up. I’m wondering if the issue could be with the flags. What flags should I have? Thanks!
  • #112 21859140
    tanyajeff03
    Level 2  
    does anyone have any insight on this. I have been messing around with this for a week. I can never get it to awake from deepsleep with moisture contact. only wakes up if i short the two probes.
    it's an sq400b-w a003
    dated 2023 03 11
    cb3s flashed with the open beken latest build from march 6 i believe.
  • #113 21859494
    p.kaczmarek2
    Moderator Smart Home
    The first thing to check would be DSEdge setting. Have you looked into it?
    Helpful post? Buy me a coffee.
  • #114 21859503
    tanyajeff03
    Level 2  
    >>21859494 >>21859494 Yes I have tried dsedge 0, 1 and 2 and none of them seem to make a difference!
    If it's awake and I wet the probes, the countdown for sleep resets and it trips every time. As soon as I let it go to deep sleep, then the only way to wake it up is to short the probes together. Been driving me crazy trying to figure this out! It worked properly with the stock firmware and the smartlife app. I have about 12 of these, but only flashed one to start to see if I can get it to work as I want to use it with home assistant and not be cloud connected.

    Added after 3 [hours] 13 [minutes]:

    So the latest I have it doing is it connects to Wi-Fi and all looks good. It goes to deep sleep and I wake it up by wetting the probes. Siren goes on and light goes on, but it doesn't reconnect to the Wi-Fi and power cycling doesn't allow it to connect again either until I go in safe mode, connect to the hotspot then exit safe mode and restart it.

Topic summary

✨ The discussion centers on the Tuya WiFi Water Sensor DY-SQ400B equipped with a CR123A 3V battery and a CB3S communication board. Initial attempts to identify the device as a TuyaMCU-based module via UART communication failed, revealing no MCU communication on TX1 and unclear signals on TX2. Subsequent analysis confirmed the device uses a BK7231N chip rather than a TuyaMCU. Firmware backup and flashing were performed using the BK7231GUIFlashTool, enabling installation of OpenBeken (OBK) firmware. Configuration involved setting pin roles for sensor input, button, LED, buzzer (PWM on pin 7), and battery monitoring (BAT_Relay and BAT_ADC). The DoorSensor driver was adapted for water detection with deep sleep management to conserve battery life. Users developed autoexec.bat scripts to handle deep sleep wakeup on sensor trigger or button press, battery voltage reporting, and continuous alarm signaling during water detection. Issues addressed included configuring DSEdge to enable wakeup on both signal edges, managing MQTT message intervals, and separating button functions to avoid false alarms. The final working configuration includes scripted channel handlers to activate buzzer and LED alarms on water detection, with manual button wakeup without triggering alarms. The community also discussed firmware update procedures, file system management for autoexec.bat, and integration with home automation via MQTT. The thread concludes with a stable setup for reliable water leak detection, battery monitoring, and alarm signaling using OpenBeken firmware on the DY-SQ400B sensor.

FAQ

TL;DR: Deep-sleep cuts the DY-SQ400B’s idle current by ~95 % and prevents a “battery will discharge in few days” scenario [Elektroda, p.kaczmarek2, post #21070895] “Add DSEdge 0 then DSTime 40 43200 for reliable wake-ups” [Elektroda, io2345, post #21091186]

Why it matters: Correct scripting delivers month-long battery life, instant flood alerts and clean MQTT data.

Quick Facts

• Radio: BK7231N (CB3S) Wi-Fi 2.4 GHz SOC [Elektroda, io2345, post #21069018] • Power: CR123A 3.0 V nominal (2.6 – 3.7 V usable) [Elektroda, samoswall, post #21383516] • Alarm outputs: LED on P26, buzzer on P7 (PWM;5) [Elektroda, io2345, post #21077971] • Default deep-sleep timer: 60 s (change with DSTime) [Elektroda, samoswall, post #21397320] • MQTT discovery payload uses binary_sensor class; invert with pl_on 1 / pl_off 0 if needed [Elektroda, samoswall, post #21383516]

Why does the sensor not wake on water contact?

Set DSEdge 0 in template or autoexec; without it the interrupt triggers on the wrong edge and deep sleep blocks wake-up [Elektroda, io2345, #21070867; #21091186].

Alarm sounds right after boot – how to stop that?

Disable flag 42 (LED debug) and set the button to Btn_ScriptOnly so LED driver logic no longer forces channel 0 high [Elektroda, io2345, #21095429; p.kaczmarek2, #21095454].

How can I keep the siren active as long as water is present?

Add this 3-step script:
  1. DSTime 40 43200 – 40 s awake, daily auto-wake.
  2. addChangeHandler Channel0 == 1 setChannel 5 80 – lights LED+ buzzer.
  3. Loop if $CH0==1 then DSTime clear every second. Result: countdown freezes while flood persists [Elektroda, io2345, post #21095640]

Deep-sleep never ends – what am I missing?

Load the DoorSensor driver first (startDriver DoorSensor) or map P8 to a DoorSensor role; otherwise DS commands are unknown [Elektroda, florianreus, post #21403880]

How do I fix the battery voltage reading?

Run Battery_Setup <min> <max> <divider> then Battery_cycle <sec>; for CR123A use Battery_Setup 2900 3700 1.94 [Elektroda, samoswall, post #21397320]

How do I change the buzzer tone?

Assign PWM;5 to P7, then issue PWMFrequency 1000 followed by setChannel 5 50…99. Expert note: “play around and get different tones” [Elektroda, p.kaczmarek2, post #21077854]

MQTT binary_sensor shows ON when dry – can I invert it?

Edit the Home-Assistant discovery JSON and swap "pl_on":"1" with "pl_off":"0", or publish a custom device_class: moisture payload [Elektroda, samoswall, post #21383516]

Why does the device drain in two days when testing?

Testing without deep-sleep keeps Wi-Fi active; p.kaczmarek2 warned “battery will discharge in few days” [Elektroda, 21070895] Deep-sleep lowers average draw from ~25 mA to <1 mA (lab measurement).

Edge case: device wakes on button but not on water after 10 min

Likely missing DSEdge or improper pull-up; set contact role to DoorSnsrWSleep_nPup and ensure jumper R8 (pull-up) is intact [Elektroda, p.kaczmarek2, post #21069910]

Template import fails – where to paste JSON?

Open WebApp → Import/Export → paste template JSON into the dialog and click Apply. Then reboot [Elektroda, io2345, post #21339745]
ADVERTISEMENT