Here is one simple and cheap way to automate electric boiler activation. There are lots of “smart power plug” available on OLX. Especially the ones from tuya. For example, I was able to purchase one for 300 ₴ (~$8). It came without any written instruction or even an identification. However, there are lots of guides and manuals on the web, and it was a matter of error and trial to get it creating a WiFi hotspot. Then after connecting to it, the Android application Smart Life can be used to configure plug’s connection to the LAN WiFi. That’s just enough to start manually controlling the plug like turning it on and off on schedule.

Now imagine I’d like to do something that’s not a stock functionality. For example, when the power is cut for some load shedding, and comes back a bit later, I’d like to make sure the water boiler stays switched off for extra minutes before turning on. This would allow the grid to ramp up the power supply without causing immediate overload. An incentive for custom automation!

I took the first Python library that popped in the search: tinytuya. It has detailed instructions on how to register as a developer, discover the device id and get the necessary key to control the device. I ended up customizing one of the examples to ensuring the switch is on:

#!/usr/bin/env python

import tinytuya, time

while True:
    try:
        d = tinytuya.OutletDevice(dev_id="<id>", address="<addr>", local_key="<key>", version=3.3)
        status = d.status()
        if status['dps']['1']:
            break
        print("Turn on")
        d.turn_on()
        time.sleep(60)
    except Exception as e:
        print(f"Exception {e}")
        time.sleep(10)

The script then is wrapped into a systemd service:

[Unit]
Description=Boiler power on
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
User=sakhnik
Group=users
ExecStart=/usr/bin/python /home/sakhnik/work/boiler/boiler.py
StandardOutput=journal

[Install]   
WantedBy=default.target

The service should be started by a timer:

[Unit]
Description=Start boiler deferred after boot

[Timer]
OnBootSec=15min

[Install]
WantedBy=timers.target

And the timer needs to be enabled:

systemctl daemon-reload
systemctl enable boiler.timer

Now, the switch relay can be configured to start in the switched off state after powering up in the Smart Life app. When the power is restored, the switch is initially off and the computer is booting up. After 15 minutes, the timer kicks off the boiler service running the program, which turns the switch on. It’s a one-time action, so one can continue managing the switch manually.

There are disadvantages in this system too. For example, the privacy. Although, there’s very little harm that can be done, I don’t particularly like to share my internal network layout with Tuya. I’d rather prefer to have a direct access to the plug without engaging complex external servers. I’ve just heard that this might be possible with some other smart plug producers. Looking forward to explore.