Adding a “Battery Display” to Your Water Heater: Zero-Modification Water Volume Prediction with a Shelly Gen4 Script
Treat water heater power like a battery meter? Fair enough—at least it’s better than tearing the unit apart and leaking water. A Gen4 local script computes a virtual water volume, error within 5%—middle-school physics could do this job, but you just wouldn’t think of it. Code solving life anxiety? Heh, I’ve already been using it.
目录16 个章节
Preface
I believe many people still using storage water heaters at home suffer from hot-water anxiety: you have to turn it on in advance before showering, and you’re afraid of running out of water halfway through. Most traditional water heaters on the market are just a black box—aside from a vague temperature gauge, you have no idea whether the remaining hot water is enough for a 5-minute or 15-minute shower.
Recently I got the new Shelly 2PM Gen4 from the German company Shelly, and tried to thoroughly solve this problem in a geeky way.
The Idea
Translate “battery level” into “water volume”
The traditional solution is to take the unit apart and add sensors, but that not only voids the warranty, it also risks leaks.
The clever part of the Shelly approach is: using the Gen4 chip’s local scripting capability to build a virtual sensor.
Note: The Shelly 2PM Gen4 can not only predict hot water volume, but also supports remote on/off control and real-time energy metering. Gen4 devices further support the Matter protocol and Zigbee, and can seamlessly integrate with mainstream smart home platforms such as HomeKit, Google Home, and Alexa.
The principle is simple:
- Heating model: The script monitors real-time power. As long as power reaches the threshold, it means the water heater is heating; it calculates the injected heat according to physics formulas and increases the water volume percentage in real time.
- Loss simulation: Even when unused, the water heater dissipates heat. The script automatically deducts the percentage drop caused by heat loss based on time.
- Local UI: All calculations are completed inside the Shelly, and a percentage progress bar like a phone battery is generated directly in the mobile app.
Implementation
Hardware Installation

The installation process is very simple:
- Power off: First turn off the circuit breaker corresponding to the water heater in the home distribution box
- Series installation: Connect the Shelly 2PM Gen4 in series on the water heater’s live wire
- Wiring specifications:
- Live incoming (L) connected to Shelly terminal 1
- Live outgoing connected to Shelly terminal 2 (output to the water heater)
- Neutral (N) connected directly to the water heater
- Restore power: After confirming the wiring is correct, close the circuit breaker
Safety note: If you are not familiar with electrical work, it is recommended to have a professional electrician assist with installation.
Power limit reminder: The Shelly 2PM Gen4’s rated load is 2400W (single channel) / 3840W (total). Be sure to check the water heater’s nameplate power before installation—some large-capacity or tankless water heaters may have power as high as 3000W~6000W. Do not exceed the limit, or there is a risk of overheating or even fire. If the water heater’s power exceeds the limit, please choose a higher-spec Shelly device or use it only for the control circuit.
Smart Algorithm
We developed a script that runs inside the relay. The core principles are as follows:
1. Power Monitoring
// 获取实时功率
let status = Shelly.getComponentStatus("switch:0");
let power = status.apower; // 单位:瓦特
2. State Determination
- Heating state: When real-time power > rated power × 70%, it is judged as heating
- Keep-warm state: When power is below the threshold, it is judged as keep-warm or off
3. Hot Water Volume Calculation
// 加热时:按时间比例累加
let add_percent = (delta_s / expected_time_s) * 100;
water_percent = water_percent + add_percent;
// 保温时:模拟热散失
let drop_percent = cool_per_sec * delta_s;
water_percent = water_percent - drop_percent;
4. State Persistence
Use Shelly’s virtual component (Virtual Component) to store the hot water volume percentage, so data is not lost even if the device restarts.
Real-World Results
Creating the Script
In the Shelly App, go to the "Scripts" module, tap "+" to create a new script. Paste our smart prediction script in, save, and run it.

Runtime Monitoring
After the script runs, it automatically creates a virtual component named "Predicted Hot Water Volume". You can see the hot water volume percentage change in real time.

Real-time Data

After running for a while, the App interface will display complete monitoring information:
- Device home page: Shows device online status and quick control buttons, supporting remote on/off of the water heater
- Power chart: Real-time power curve, clearly showing heating and keep-warm cycles
- Energy statistics: Cumulative electricity usage and runtime, helping you understand the water heater’s actual energy consumption


Verifying Accuracy
By comparing the water heater’s built-in temperature gauge readings with our prediction results, we verified the algorithm’s accuracy. Actual measurements show the prediction error is controlled within ±5%, fully meeting daily use needs.

Why Is This Approach More Meaningful?
In the smart home field, we often talk about intelligence, but many solutions are just simple remote switches. The significance of this Shelly project lies in software-defined hardware:
- Non-invasive: No need to touch the water circuit, no damage to the original unit.
- Low latency & privacy: Data is not uploaded to the cloud; the local script computes the result directly, with second-level response speed.
- Remote control: Forgot to turn off the water heater when you left? One tap on your phone to turn it off remotely, plus scheduled on/off.
- Energy metering: Precisely records electricity used for each heating cycle, helping you optimize usage habits and save on electricity bills.
- Scene automation: You can set—“When hot water volume reaches 80%, push a notification to the phone and automatically turn off the bathroom exhaust fan.”
- Ecosystem compatibility (Gen4): Supports Matter and Zigbee protocols, can connect to Apple HomeKit, Google Home, Amazon Alexa and other mainstream platforms, seamlessly linking with other smart devices.
Wrap-up
For users, the Shelly 2PM Gen4 is not just a switch—it’s more like a mini server running at home. Through the open scripting interface, we can smart-upgrade any traditional appliance.
Besides the hot water volume prediction introduced in this article, you also get practical features like remote on/off and energy statistics. Gen4 devices further support Matter and Zigbee protocols, easily integrating into a whole-home smart ecosystem.
If you’re also tired of blindly guessing hot water temperature, this way of using code to solve everyday pain points might be another elegant solution.
Final reminder: Before purchasing and installing, be sure to confirm that the water heater’s power is within the Shelly 2PM Gen4’s capacity (single channel ≤ 2400W, total ≤ 3840W). Some high-power water heaters (such as storage or tankless units above 3000W) need a higher-spec device—do not exceed the limit.
_EOF_
Adding a “Battery Display” to Your Water Heater: Zero-Modification Water Volume Prediction with a Shelly Gen4 Script
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
AltSnap:让你的 Windows 窗口管理像 Linux 一样丝滑
🔹 还在为 Windows 窗口不好拖动而烦恼? 🔹 想像 Linux 一样 Alt + 拖动窗口,随心所欲? 🎯 试试 AltSnap —— 小巧、强大、免费! 📦 让你一键拖动、快速缩放、边缘吸附,无需精准点击标题栏,效率翻倍!
2025-08-27 · 数字生活 · 约 1 分钟
你不知道的窗口切换神器:window-switcher
专为Windows用户提供,能通过快捷键轻松切换当前应用内的多个窗口,模拟macOS的Cmd+`体验。它轻量级、无资源占用,安装简单,可提升工作效率。适用于频繁切换多个软件窗口的用户。
2025-08-27 · 数字生活 · 约 1 分钟
比Django官方实现更好的分页组件+Bootstrap整合
前言 Django全家桶自带的分页组件只能说能满足分页这个功能,但是没那么好用就是了 Django的分页效果 django-pure-pagination分页效果 使用方法 首先安装: pip install django-pure-pagination 添加到App INSTALLED_APPS = ( ... 'pure_pagination', ) 配置一下,配置说明如图 P
2021-12-18 · Django · 约 1 分钟
Asp-Net-Core开发笔记:实现动态审计日志功能
审计日志不仅仅是一种遵循最佳实践的手段,更是确保数据安全、提高系统透明度、促进责任归属明晰的关键工具。
2024-03-21 · Asp-Net-Core开发笔记 · 约 1 分钟
讨论
共 0 条加载评论中…