返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
数字生活

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.

曦远 (DealiAxy)约 19 分钟
目录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:

  1. 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.
  2. Loss simulation: Even when unused, the water heater dissipates heat. The script automatically deducts the percentage drop caused by heat loss based on time.
  3. 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

Wiring diagram

The installation process is very simple:

  1. Power off: First turn off the circuit breaker corresponding to the water heater in the home distribution box
  2. Series installation: Connect the Shelly 2PM Gen4 in series on the water heater’s live wire
  3. 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
  4. 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.

Create a new script

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.

Power detection and hot water volume changes

Real-time Data

Script running

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

App interface showcase

More features showcase

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.

Water heater temperature gauge

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 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…