Here's the landscape. Short version: everything you described is not only possible, it's a well-trodden path — and the best approach doesn't need any cloud API at all.
1. Controlling the KP400 — you have a clean local option
The key discovery: there's a mature open-source library, python-kasa, that talks to Kasa devices directly over your local network — no internet, no TP-Link cloud, no reverse-engineered web login. The KP400 is explicitly supported, including its two independently-switchable outlets (each of your outlets shows up as a child you can toggle separately). This is exactly what powers the Home Assistant Kasa integration.
Why local is the right choice here:
- Reliability — an ice-protection system shouldn't depend on TP-Link's cloud being up, your account not getting rate-limited, or a firmware change breaking an unofficial web API. It just needs your LAN.
- Speed & simplicity — kasa --host 192.168.1.x on --index 0 from a shell, or a few lines of Python.
- No account fragility — TP-Link has no official public developer API; the cloud methods people use are reverse-engineered from the Kasa app, and TP-Link periodically changes them (they've been migrating newer devices to an encrypted "KLAP" transport). Your KP400 uses the older, stable, well-supported local protocol.
One caveat to plan around: give the KP400s static/reserved IPs (DHCP reservation in your router) so the controller always knows where to find them.
Cloud alternatives exist (tplink-cloud-api and similar) and would let you control the switches from outside your home network — but for a device sitting on your own roof controlled by a program on your own network, that's added fragility for no benefit. I'd only reach for cloud control if the controller can't be on the same LAN as the switches.
2. Weather data — free and ideal for your location
Since you're in Montana (US), the National Weather Service API (api.weather.gov) is the natural fit:
- Completely free, no API key, ~5,000 requests/hour — you'll use a handful per hour.
- Just requires a User-Agent header identifying your app + a contact email.
- Flow: hit /points/{lat},{lon} once to get your gridpoint, which gives you URLs for hourly forecast and current observations (temperature, precipitation, etc.).
The main limitation is US-only, which doesn't affect you. If you ever want richer data (snow depth, freezing rain probability, historical) you could supplement with a keyed service like OpenWeatherMap, but NWS alone is plenty to start.
3. The actual logic — inferring "icing conditions"
This is the interesting part, and worth getting right so you're not just burning electricity all winter. Ice dams form from a melt-refreeze cycle: snow on the roof melts (from attic heat or sun), water runs to the cold eaves, and refreezes. So the danger zone isn't "very cold" — it's near and just below freezing, with snow present.
A reasonable first heuristic: - Turn ON when: there is snow on the roof (recent snowfall or snow on ground) AND temperature is in roughly the 15°F–38°F band (cold enough to refreeze at the eaves, warm enough for melt to occur). - Turn OFF when: well above freezing for a sustained period (snow sheds on its own) or no snowpack present. - Add hysteresis / minimum on-time (e.g., don't flip states more than every 30–60 min) so a temperature hovering at the threshold doesn't rapid-cycle the relays.
You can refine over your first season — some people simply run cables whenever there's snowpack and temps are below ~40°F, accepting some extra energy use for safety margin. Because the KP400 has two outlets, you could even zone it (e.g., north-facing eaves vs. south).
4. How to run it — a few architectures
| Approach | What it looks like | Best if… |
|---|---|---|
| Standalone script + scheduler | A Python script (python-kasa + NWS) run every 15–30 min by cron/systemd timer on a Raspberry Pi or always-on machine at home |
You want the simplest thing that works and full control over the logic |
| Home Assistant | Install HA on a Pi; it has built-in Kasa + weather integrations; express the logic as an automation | You might want a dashboard, history graphs, manual override, and other home automation later |
| Cloud-run script | Script on a VPS using the cloud API | Only if you can't run anything on your home network |
For a focused, single-purpose system like this, I'd lean toward the standalone script on a small always-on device at home — it's the least moving parts, and a Raspberry Pi (or any machine that's always on) sitting on your LAN can reach the switches directly. Home Assistant is the better pick if you anticipate this being the first of several automations.
Important safety layer regardless of approach: the automation should fail safe. Think about what happens if the controller crashes, the network drops, or the NWS API is down during a storm — you likely want a "when in doubt during winter, turn them ON" bias, plus the ability to manually override via the Kasa app (which keeps working independently). The KP400's own schedule/manual control remains as a fallback.
A sensible path forward:
1. Confirm you can reach a KP400 locally — install python-kasa and run kasa discover on your home network to see the switches and their two outlets.
2. Pull a live forecast from api.weather.gov for your coordinates.
3. Write the decision logic + a scheduler, starting conservative on the "on" conditions.
If you'd like, I can help you build this — e.g., start by scaffolding a small Python project that discovers your KP400s and pulls your local NWS forecast, so we can verify both halves work before writing the icing logic. Want me to go ahead and set that up?
Sources: python-kasa · python-kasa docs · KP400 product page · NWS API docs · NWS API FAQ · tplink-cloud-api