Keeping the Fans at Home: Our Mantra Fan App

If you've read any of my posts before, you'll know I hate remote controls. So when we fitted Mantra ceiling fans in the lounge and the bedroom, the little white remotes went straight in a drawer and the fans went into the Smart Life app. The plan was simple, add them to Homey with everything else in the house and crack on.
Hold on a minute? Homey could see the lights, not a problem, but the fans themselves were nowhere to be found. No on & off, no speed, no direction, none of the other settings, and it was the same story when I tried Home Assistant. The information the fans send out, their "data points", simply wasn't available to either of them, so I had a smart light with a very dumb fan attached!
So the fans ended up running through Homebridge, a separate bit of software sat in the middle just to get them talking. And in fairness, it worked, the fans did what they were told and I can't say I had any problems with them. But it meant one more platform to keep running, one more thing to update, and one more place to look when something needed changing. Everything else in the house lives in Homey, so why should the fans be different?
So I did what any sensible person would do on a quiet evening, opened up the laptop and wrote my own Homey app for them, bringing everything into one platform, and while I was at it, making it completely local.
Has it worked? In short, yes, the fans now live in Homey alongside everything else, and they don't need the internet to do it.
Hold on, what's "the cloud" got to do with my fan?
This is the bit most people don't realise, so let me explain. A lot of smart home kit, and that includes the Mantra fans, is built on a platform called Tuya. When you tap the fan in the app, that command doesn't go across the room to the fan. It goes out of your house, across the internet, to Tuya's servers, and then all the way back again to the fan sitting a few metres away from you.
Think of it like wanting to ask someone in the next room to switch the light on, but instead of shouting through, you post them a letter via a sorting office in another country. It gets there, usually, but it's a long way round!
Now, I'll be honest, ours worked fine that way. But if I was going to the trouble of writing an app anyway, why build it on that long way round, when it brings three problems with it:
It's slower. Every command has a round trip to make, and when the servers are busy you feel it
It breaks when the internet breaks. No broadband, no fan. Same if the company's servers have a bad day, and they do
It's not really yours. If the company changes the rules, starts charging, or switches the service off, your smart fan becomes a dumb fan with the remote in a drawer
Why local matters
Local control simply means the smart home talks directly to the device, inside your own four walls, without going out to the internet at all. In our house that's Homey, the little hub that runs everything, talking straight to the fans over our own Wi-Fi.
In all honesty, I think this is the single most important thing to get right in a smart home. Things you use every day, the lights, the heating, the locks, and yes, the fans, should keep working when the internet doesn't. It's quicker, it's more reliable, and there's the privacy side too. Do I really need a company somewhere logging every time I turn the bedroom fan on? I'd rather not.
That's not to say the cloud is all bad, it's brilliant for some things, but it should be a bonus, not a requirement for turning on a fan.
So what does it do?
The Mantra Fan app runs on our Homey Pro and talks to both fans directly. Each fan shows up as two devices, the fan itself and its light, so they can be controlled separately and used in flows just like anything else in the house.
From the dashboard or my phone I can:
Turn the fans on & off and set the speed
Turn the lights on & off, dim them, and change them from a warm to a cool white
I also added the fan control to the smart wall switches for button press control so anyone can use
Switch the direction between Summer & Winter
That last one is worth a quick explanation. In summer a ceiling fan should spin so it pushes air down onto you, which makes the room feel cooler. In winter you reverse it, so it gently pulls air up and pushes the warm air that's collected at the ceiling back down the walls. Most people never touch it because the button is hidden away somewhere, so I put a Season Mode widget on the dashboard. One tap and both fans swap to Summer or Winter, and the app remembers the setting, so the next time a fan starts up it spins the right way.
The light that wouldn't stay off
Then came the annoying bit. These fans have a "courtesy light", so when you turn the fan off, the light switches itself on. I'm sure it's helpful for someone, but turning the bedroom fan off at night and having the light come on full in your face isn't my idea of helpful!
Worse still, if you turn the light off straight away, the fan ignores you while the motor is slowing down, and then about 13 seconds later turns the light back on again anyway. I spent more evenings than I'd like to admit sat on the end of the bed with a stopwatch working that one out 🤦🏻♂️
So the app now watches for it. If you turn the fan off and you didn't have the light on, it turns the light straight back off, and keeps an eye out for the fan trying to sneak it back on again. If you did want the light on, it leaves it alone. Problem solved, and something the official app has never been able to do.
How's it working out?
Brilliantly. The fans are finally in Homey properly, on the dashboard, in flows, with no Homebridge in the middle. The bedroom light stays off when it should, and the fans would carry on working whatever the broadband is up to. The real test was always going to be whether I'd ever think about it again, and I haven't, which is exactly how a smart home should be.
Tech Details
For those of you who like to know what's going on under the bonnet, here's how it all fits together. No Tuya cloud, no Smart Life app in the loop, and no internet needed once it's set up.
The hardware: a Homey Pro, which already runs the rest of the house, plus two Mantra DC ceiling fans with lights, one in the lounge and one in the bedroom
How it's built: a custom Homey app written in Node.js on Homey's SDK 3, with two drivers, one for the fan and one for the light
Talking to the fans: the fans use Tuya's local protocol version 3.5, so the app opens a direct connection to each fan on the local network (TCP port 6668). I wrote the protocol client from scratch, including the handshake where Homey and the fan prove they both know the device's local key and agree a fresh session key for each connection
Encryption: every message between Homey and the fan is encrypted with AES-128-GCM, so even on our own network nothing is sent in the clear
One connection per fan: the fan and its light share a single connection, which keeps the fan happy (they don't like lots of connections at once) and means the fan and light always agree on what's going on
Staying connected: a heartbeat goes to each fan every 5 seconds, if the connection drops it reconnects within a second, and a fan is only marked offline in Homey if it's been gone for 15 seconds, so a quick Wi-Fi blip doesn't set off any alarms
Speed, brightness & colour: the fans have 32 speed steps and the lights have their own brightness and colour temperature ranges, all mapped onto Homey's normal sliders, with a short delay so dragging a slider doesn't flood the fan with commands
Getting the keys: the one time the cloud was needed was at the very start, to get each fan's device ID and local key from the Tuya developer platform. After that, the app never goes near it again
Flows: there are flow cards to set one fan or all fans to Summer or Winter, and triggers for when the season is changed, so other things in the house can follow along
Dashboard widgets: the Season Mode widget for flipping the fans' direction, and since I was in there anyway, a few other widgets for our dashboard snuck into the same app, including a guest Wi-Fi QR code and a window status widget that shows which windows are open, alongside sunrise & sunset times worked out on the Homey itself
At the moment it's built just for our house, so it isn't in the Homey App Store, but the same approach works for an awful lot of Tuya based kit, which covers a huge number of the smart fans, plugs and lights on the market. If you've got devices stuck in the cloud and you'd like them brought home and working locally, it's exactly the kind of project we love, so get in touch.




Comments