The Hue-Light Mosquito Killer
Tags: lowcode, nodered, iot, beginners
Published: 2021-08-13 14:29:36

Real-life problem: Mosquitos enter your bedroom at night, but electrical Mosquito killers consume electricity (and cartridges) all day. So let's put a smart plug in the middle and schedule the Moskito killers to run in the evening only.

If that's all you want, you can do it from an App. In the Hue App, you can create a zone for your mosquito killer and create an automation like this:

![Click-Through Guide for the Hue App: Create a new zone, switch to the Automations-Tab, create an Automation, choose Custom, set a start and end date to the evening, choose your newly created zone, name the Automation and you're done.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mojqh5m1hea3wi1d6j9p.jpg)

But we're programmers! So if you want finer control or create automations with devices from different IoT manufacturers, you can control individual plugs with [node-red](https://nodered.org/). It comes [pre-installed on raspbian](https://www.raspberrypi.org/software/operating-systems/), or you can install it [locally or in the cloud](https://nodered.org/#get-started). I've used a Philips Hue smart plug because [I've played around with Hue lights and node-red before](https://youtu.be/LM7bk_jDHWw). If you're using a different manufacturer, you can [search the extensive library for a node](https://flows.nodered.org/).

### Installing packages in node-red

Navigate to the URL of your node-red installation, which is `http://localhost:1880` if you've installed it locally. Note that the port `1880` is most often also the port when you run it on a Raspberry Pi or NAS on your network. Head to "Manage palette" in the side-menu on the right.

![node-red-manage-palette](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjusq9dj3byqanudc2nj.jpg)

Here you can see which nodes are available in your installation, in which package they come, or you can install new packages. The [nodes for the Hue lights](https://flows.nodered.org/search?term=node-red-contrib-huemagic) can be found in the `node-red-contrib-huemagic` package. When you click "install" the package will be downloaded and the nodes in it will be available shortly afterwards in the palette on the left of the main screen.

![node-red-install-package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0d51nzxod7czeu2w16w.jpg)
### Connecting node-red with the Hue Bridge
Before we can use them, we need to configure the connection to the hue bridge, which is accessible from the "config" tab in the menu. A click on the "search"-icon will look on the local network for an available hue bridge, so you don't have to manually enter the IP. Once you have the IP, you can generate an API key by clicking on the "user" icon, and pressing the bridge's button within 20 seconds.

![node-red-config-bridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdxkwmt36dnsjw8ng62u.jpg)

### Adding and connecting nodes to send messages

From the panel on the left we drag and drop two inject nodes for testing the smart plug. Clicking the little rectangle to the left of the node will send a message into the flow, but for now the nodes are unconnected. Double-clicking the flow opens a property panel. The inject node which has `msg.payload` set to `false` switches the plug off, the node which has `msg.payload` set to `true` switches the plug on. The `Name` is just for yourself to tell the nodes apart.

![node-red-switch-off-light](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o50y3d9sxvtm3ek4vnht.jpg)

### Accessing a node's documentation

How do you know what `msg.payload` should be? That depends on the node that receives the messages. The smart plug can be controlled with the "Hue light" node (because switching on/off is the same behaviour for lights and for switches). To receive messages from the inject nodes, it needs to be connected. All messages flow left to right, so with the "Hue light" having one on the left and one on the right, it means that this node can also send messages. The current state "turned off" is displayed on the bottom. To find out what goes in and out of the "Hue light" node, we can look at the documentation, which you can find by clicking on the book icon.

![node-red-documentation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wy6ir8oetln5zbca9in1.jpg)

### Debugging a flow
From the documentation we can learn that the "Hue light" node has a "simple" and "extended" way of interpreting `msg.payload`. In the simple mode it takes a boolean (true/false, 1 or 0), in the "extended" mode a JSON object. The output always has the form of the "extended" JSON object, which you can try out by using a debug node, and checking the output in the "debug" tab. As a plus, if you have several debug nodes, hovering over a debug message will highlight the corresponding debug node in the flow.

![node-red-debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/122317ou37b7u6dtw2i7.jpg)

### The Mosquito Schedule mixed with our schedule
Now I don't know when exactly Mosquitos are active, but I roughly know mine (Sometimes I have the feeling they're active all night). It's not always regular and sometimes I'm not at home, so it would be great if the plug switched off then as well.

So as a basis a time-based schedule is necessary, and there are some options: You can install a [scheduler-package that's configured inside the flows](https://flows.nodered.org/node/node-red-contrib-light-scheduler), or you could [try out a dashboard scheduler](https://flows.nodered.org/node/node-red-contrib-ui-time-scheduler#frontend--demo). You can then still combine that with other nodes. With one of the schedulers and the HueMagic nodes alone we could set up a combined logic including a motion-, temperature- or brightness sensor. But with this article you should have the basics of setting up your very own `Mosquito Killer Smart Home Defender™`🤺🛡️ ... Let me know what you've built! 😊

### While researching for this post I found...

This is also not the only way to protect yourself from mosquito bites. There are repellants and there's even 
[research about which type of bug is attracted by which type of light](https://www.researchgate.net/publication/38031708_Response_Of_Adult_Mosquitoes_To_Light-Emitting_Diodes_Placed_In_Resting_Boxes_And_In_The_Field). So dimming your LEDs red apparently makes your home less attractive to blood-sucking mosquitos, but may attract other creatures of the night 😉.

Photo credits for part of the header go to: 
[Wolfgang Hasselmann](https://unsplash.com/photos/nZgpg4xYhjM)