Can you IoT an Airwick air freshener?

This project is one that makes me feel on top of the world but also possibly one of my most craziest IoT projects. I am fascinated with IoT and the point where software interacts with my physical world for example in the simplest form, a sensor programmed to turn a light bulb on when there is motion. I get huge joy from tinkering and exploring how things work but the real satisfaction comes from combining connected sensors, data, automation and machine learning and of course learning myself. This project was no exception.

We’ve all most likely been into a public restroom somewhere and had the automatic air freshener trigger and possibly even make us jump out of our skin when it does. These units typically run on a schedule and spray some kind of air freshener into the room. Some are even a little smarter and trigger when there is motion in the room. Well I have a number of air fresheners like these in the house and sure they’re not necessary but I like them and they smell nice. What I don’t like is how they continue to work when I’m not in the room and even not in the house. This is entirely wasteful and not very smart. And well my house is smart so get with the programme! The other problem is if you’re like me and find it hard to sleep sometimes the noise of them spraying throughout the night is annoying and likely lead to me saying to myself was trying to get to sleep “I wonder if I can make these things smart“. I’ve likely fallen asleep as this stage and forgotten about the crazy idea, but clearly this one time I didn’t, so here I am writing about how to IoT Airwick air fresheners. Let’s consider a smart air freshener and how it would work if it wasn’t triggered by a manually.

Motion. Rooms can be busy so triggering with each motion event would as wasteful as it spraying every 15 minutes now. But we could use motion in combination with when the air freshener last sprayed to ensure it only sprays every 30 minutes for example.

Alarm state. If my house alarm is armed then that is a good sign I’m not home so the air fresheners shouldn’t work. This could also prevent them from working when in night mode but there is, of course, one the exception to that, the bathroom!

Room based triggers. In the living room, if the TV is on or the sofa is occupied then the room is in-use. In the office, if my workstation is unlocked then I am likely working and the room is in-use. If my toilet is flushed and the bath or shower is in-use then that is a great indication the bathroom is in use. And yes, I’ve IoT’ed my toilet! Then there are exceptions like if I’m having a bath I probably don’t want it to be triggered as it would mess with my Zen.

Spray interval. The unit provides some control over the frequency the air freshener sprays but this is limited to 9, 18 and 36-minute intervals. Why these intervals I don’t know but I’m sure Airwick have their reason. It would be useful to replicate this and allow longer intervals between sprays.

Sensors. Throughout my house, I have a large collection of sensors and in a truly connected world, these should be considered too. For example, when the alarm is unarmed after a trip away the air fresheners could be sprayed to create a welcoming smell. If windows are open then I’d rather have the fresh air than the air freshener so don’t have them spray.

Voice. In an inclusive world, voice assistants are increasingly used to perform tasks that ordinarily might have been out of reach for some people.

So considering all of the above I decided to accept the challenge and IoT’ed my air fresheners. But with all these crazy ideas – I wasn’t going to compromise on the aesthetics. The look and feel could not be compromised. Let me walk you through it.

Ingredients:

  • Airwick air freshener
  • ESP32 NodeMcu
  • Relay
  • Battery shield and 18650 batteries
  • ESPHome
  • Home Assistant

Airwick Air Freshener

The Airwick air freshener device is pretty discrete. It contains a can of the air freshener of your choosing and two AA batteries. Behind the scenes, there is a 3v motor which when powered, powers gearing that creates sufficient force to push down the nozzle on the can of air freshener. Notice the red plunger gear is only two-thirds round. This prevents the gearing from forcing the aerosol can to spray continuously and it runs out of gear. When the motor is no longer powered the gearing reverses from the return force of the nozzle of the can of compressed air freshener.

Gearing and the plunger from inside an Airwick air freshener.

The build

This was quick to build out and I had a working air freshener activated through my NodeMcu in about 15 minutes.

The components connected for testing.

I planned to use a smaller ESP32 dev board but for this prototype, I’ve opted to use the standard board for ease of access. I made a few alterations to the plastic insert in order for me to place the components inside.

All the components to make an IoT Air Freshener packed away inside.

I also planned to use larger batteries but will need to 3d print a housing for that so for this prototype I housed the battery discretely on the back for ease of access and space reasons.

The battery is discretely located on the back for the prototype.

ESPHome

In order to operate my air freshener automatically, I need something that I can programmatically control from Home Assistant. To provide me with the compute for this IoT project I turned to my trusted and favourite ESP32 NodeMcu board flashed with ESPHome. The ESP32 NodeMcu is powered from a battery and is connected to a relay. This relay allows a circuit to open and closes which then activates the motor and gearing inside the air freshener.

Fritzing wiring diagram.
Fritzing wiring diagram

In 10 lines of configuration, I was able to create a switch that operates that relay and that’s it. It is as simple as that. To turn the switch on and off you need an interface. ESPHome provides the ability to do this through a web UI, MQTT or in my case through integration with Home Assistant. In fact, Home Assistant auto-discovers ESPHome devices so the switch I defined in my configuration is immediately available to use.

To preserve the battery I decided to use the deep sleep and MQTT component in ESPHome. This allows me to wake the device, check the state of a MQTT topic. It then either triggers the air freshener or goes back to sleep depending on the state of that MQTT topic and save battery power.

Home Assistant

The brain of my house Home Assistant handles all the integration with ESPHome, other sensors and the home automation logic.

ESPHome air freshener switch integrated with Home Assistant

I won’t go through all of the different automations I have created to work with my new-fangled smart IoT air fresheners but instead, I’ll show you an example that in part is the most applicable for an air freshener. My bathroom air freshener. That particular air freshener is triggered when my IoT toilet is flushed and as automations go is the coolest toilet air freshener automation you will likely to ever see!

For me, the data is as much fun to play with and analyse as the tinkering with all the hardware. To provide me with additional data points that I can study, I added a counter that increments each time the unit is sprayed and a date sensor to store when the air freshener was last replaced.

To preserve battery life the ESP board goes into a deep sleep and wakes every few minutes to check if it needs to trigger the air freshener. This is achieved using an MQTT topic. I created an MQTT switch that is enabled when a binary sensor is on. This switch triggers the air freshener to spray and is reset before ESPHome sends the device to sleep again.

Alexa – beast-mode level five-thousand!!!

We all know there can be those moments when a little fresh air is most welcome. For those situations, I’ll leave you with this.

Alexa, it’s stinky.

This uses a custom component for Home Assistant called alexa_media_player. An Alexa routine is created to listen for that sentence which then set the brightness level of a virtual light created in Home Assistant to a particular level. Then an automation in Home Assistant is triggered. It checks which room Alexa was used in and then activates the air freshener a few times in that room.

One thing I have noticed with Alexa routines is that I’ve always had to keep the wake sentences short and not complex in order for them to be picked up by Alexa. For example, for this I started with “Alexa, it is stinky in here” but her response would be “Sorry, I don’t know that one”. Instead, I simplified the wake sentence to “Alexa, it’s stinky and “Alexa, it smells”. This could be taken a step further and a routine could be used to activate all the air fresheners like “Alexa, freshen up the house” for those moments when you have someone special coming over!

Screenshot of my it is stinky routine in the Alexa app.

Closing thoughts

This really could not have been made any easier and that is down to two great projects Home Assistant and ESPHome. I strongly recommend that if you haven’t explored Home Assistant, ESPHome or ESP32 NodeMcu for tinkering you give them a try.

From this picture, it is impossible to tell that this Airwick air freshener has been IoT’d.

I started by saying I would not compromise on the look and feel of the unit and hopefully the above demonstrates this. There are other benefits to this madness. My friends won’t freak out from the sound of the air freshener spraying nor think I think they smell! The air freshener will last longer and because I converted the batteries I can recharge them. I’ll be able to work out when the spray needs replacing from the data. Oh and I’ll be able to get off to sleep without hearing that clicking noise and troubling myself with such crazy ideas like this. For now at least!

So while I’ve made a few jokes in this article and had lots of fun along the way, the point I want you to take away is that ideas can become reality. Crazy to you might not be crazy to someone else. Let your mind run away and be creative. The growth and learning opportunities are endless.

I 💕 my Home Assistant

Turn lights on and off when Windows 10 is locked or unlocked

Like many, I have smart lighting throughout my house. This includes my office and desk. Behind my beautiful ultrawide monitor, I have a 3m Philips Hue strip which helps add some peripheral light and softens the glare when using the screen. I use scenes to adjust the level of white light towards early evening, but I’ll save that for another day. While I can manage the lights in the room based on motion, I wasn’t doing anything with my monitor lights as I might have been working, but the room could have had no movement. This would have resulted in the monitor lights turning off and would have been frustrating, so I decided to integrate whether my workstation is locked or unlocked or not with Home Assistant.

My desk lights turning on when I unlock my Windows machine using Home Assistant

Previously I had a PowerShell script that I could run that would hit the Hue API to toggle my lights on or off but since I have moved away from my Hue bridge this no longer works plus it wasn’t automated, but this was a good starting point. The sysadmin in me remembered there was a trigger in Task Scheduler that would be a task when the workstation was locked or unlocked, which gave me the trigger for my Home Assistant integration.

Picture of the trigger screen in Task Scheduler showing the different triggers that are available.
Workstation lock/unlock triggers in Task Scheduler

The next part was talking to the Home Assistant to perform service calls such as light.turn_on or scene.turn_on. Fortunately, I know Home Assistant provides great extensibility and has a fantastic rest API, so that was Home Assistant. This just left the process to call the API. For this, I wanted to stick with PowerShell as it is very versatile and I use it all day long anyway.

I had a prototype working very quick, but I wanted something more modular and parameter-based to use for other things. This is when I took found Flemming Sørvollen Skaret’s Home Assistant PowerShell Module. His PowerShell module is awesome and really handy. I was off and running with it within minutes. So I decided to leverage this module in my Task Scheduler automation. And that was that, but I still had to hook it all together.

  • 2 x Task Scheduler Tasks – triggered when the workstation is locked and unlocked
  • 1 x VBScript to launch our PowerShell script so it is hidden
  • 1 x Flemming’s Home-Assistant PowerShell Module
  • 1 x PowerShell script to call the Home Assistant API
  • 1 x Home Assistant long-live token
  • 1 x entity in Home Assistant (a light, scene or input_boolean for example)

Task Scheduler

First I created two tasks in Task Scheduler. One for when the workstation locks and the other for when it is unlocked. When these tasks are triggered, they call a VBScript with some parameters. Yes. An actual 1900’s VBScript in 2020. For the life of me, I could not stop or prevent the PowerShell console from flashing up. I’m a UX nerd, and this wasn’t acceptable for me. I spent way too long on trying to solve this before I reverted to this well-known workaround of using WScript to execute the PowerShell script silently. These have been exported from Task Scheduler and therefore can be imported straight back in obviously correcting usernames and paths.

You will notice on line 46 above calls a common TaskSchedulerLauncher.vbs script along with the path to a ps1 file and the parameters to go with it wrapped in double-quotes.

Are you using a battery-powered device?

Geert van Horrik (@GeertvanHorrik) kindly pointed out that if you are using a battery-powered device like a laptop or a tablet you will need to change the conditions so the task runs when on battery power.

Power settings for a task in Task Scheduler

VBScript

Having to use the VBScript is annoying but not as annoying as the PowerShell console flashing up each time I unlocked my workstation. It calls PowerShell.exe and our script while also setting the window style to be hidden.

PowerShell

In order to interact with the Home Assistant API you will need a long-lived token. You can create one in the Long-Lived Access Tokens on the profile page (http://localhost:8123/profile). Be sure to name it something sensible and meaningful.

The token and information about my Home Assistant instance are stored as variables in the script. The service I want to call such as light.turn_on, scene.turn_on and input_boolean.turn_on and the entity_id (input_boolean.james_desk) are passed to this script as parameters.

Additionally, as the PowerShell script contains the token to Home Assistant I store the script within my profile and limit the permissions to the file to just myself.

Home Assistant

I started off by just toggling the state of my monitor light but I shortly realised I could achieve more if I knew my workstation was unlocked so I created an input_boolean.

I toggle the state of this each time my Windows 10 machine is locked and unlocked. This means I can do more than just turn my lights on or off. I use this input_boolean in my motion automation which prevents my room lights from turning off if my workstation is unlocked (no more crazy waves). It also stops my Dyson fan from oscillating and directs it towards my desk and changes the speeds if the temperature is above a threshold. I can also choose route TTS notifications through to my desk Sonos speakers or HTML5 notifications to just my workstation.

Best of all with Windows Hello I just sit down at my desk, and Home Assistant handles everything else because it knows my workstation is unlocked and the chances are I’m working or tinkering with something at my desk.

I’ve also used the same PowerShell method to create a set of quick actions that I can call from buttons on a Home Assistant Stream Deck board where I can call my favourite playlists and scenes. I’ve also created some TTS scripts to announce and nag when chores need to be done or when it is dinner time! I think I’ll save that for another post!

I 💕 my Home Assistant

Delicate mushroom risotto

I’ve always been fond of risotto and during a work trip in Rotterdam, Netherlands this was reconfirmed when I had what was the most incredibly delicious mushroom risotto. Since then I have been just dying to cook it. But to cook it like the dish I had in the Netherlands I’ve had to source some special ingredients that I’ve never cooked with before. Not only is this an absolutely delicious risotto it was fun to research and cook. The trick though to any risotto is to keep stirring and use a hot stock so the risotto rice releases its starch which creates the beautiful creaminess that the best risottos have. If you don’t have any speciality mushrooms use what you already have at home.

I hope you enjoy my take on a classic risotto recipe inspired by chefs Theo Randal and Marco Pierre White.

My first attempt at a delicate mushroom risotto with truffle

Delicate mushroom risotto

Recipe: Delicate mushroom risotto
Serves: 2
Prep Time: 20 mins
Cook Time: 25 mins
Ready in 60 min
Category: Main

Ingredients

  • 3-4tbsp olive oil
  • 4 tbsp butter
  • White truffle oil
  • White wine
  • 2 garlic cloves, crushed over salt
  • 2 shallots, chopped
  • 2 sprigs of thyme
  • Fresh parsley, chopped
  • Rocket leaves
  • 700ml chicken stock
  • 50g dried porcini mushrooms
  • 400g shiitake or wild speciality mushrooms carefully, chopped
  • Slice of black truffle
  • 50g block of parmesan
  • 300g arborio rice
  • Salt and pepper to taste

Method

  1. Warm 700ml of chicken stock. Using hot stock and hot rice before adding the stock will allow the rice to cook evenly.
  2. Carefully chop the larger mushrooms while keeping the texture.
  3. Place the dried porcini mushrooms into 200ml of chicken stock and allow to soak for 20 minutes.
  4. While the porcini mushrooms soak, finely chop the fresh parsley, garlic and shallots.
  5. Grate the Parmesan but keep some back to slice grate over the risotto when serving.
  6. Drain the porcini mushrooms but make sure you keep the stock back for later.
  7. Heat 1 tsp of olive oil and a knob of butter over medium heat and cook of the mushrooms for about 5 minutes to reduce their water and intensify the flavour.
  8. Add the porcini mushrooms and continue to cook for a further 3 minutes.
  9. Add a nice generous glug the truffle oil, seasoning and a sprig of thyme.
  10. Transfer the cooked mushrooms into a bowl.
  11. Then in the frying pan with medium to high heat add 1 tsp of olive oil and a knob of butter and sweat off the chopped garlic and shallots for 2 minutes until they’re sauteed.
  12. Add the rice and keep stirring until the is evenly coated in the oil until the rice is translucent (4-5 minutes).
  13. Add the good splash of white wine to deglaze the rice and stir until it has all been absorbed (2-3 minutes).
  14. Add a small amount of chicken stock (100ml). Stir and allow the stock to be absorbed.
  15. Keep adding stock in small amounts (100ml) while continuing to stir until it is absorbed. This allows the stock to mix with the starch from the rice and create a creamy risotto.
  16. Once the rice is almost al dente pour in the stock used to soak the porcini mushrooms.
  17. Carefully introduce a few shavings of black truffle.
  18. Add the thyme, parsley and mushrooms.
  19. Continue to add the last of the stock while stirring for a further 15 minutes.
  20. Finally, add the parmesan, seasoning to taste and the final two knobs of butter and stir until melted then cover and rest for a few minutes.
  21. Stir before plating the risotto and serve with a topping of rocket leaves, parmesan and a small shaving of truffle.

Use my mushroom risotto recipe Pinterest board of inspiration

I’ve created a Pinterest board with samples of ingredients, recipes for inspiration, presentation tips, videos and photos of my own dish.

Other inspiration

The big D word – my one meal a day diet journey

I normally keep this stuff to myself and don’t share it but I thought I’d break that rule as I work towards a healthier happier mind and lifestyle.

Just over a week ago I decided to try out fasting. A week prior to that I decided to cut down on my drinking. I was drinking a little too much a little too often. Add the new mountain bike I got back in August and the long walk each weekend and I’m sure you can see what I am trying to do. You know the bid D-work. Diet. Diet, cut out the bad stuff and get back into shape! I’m just having a drink when there is an occasion now such as going out for a meal or friends coming over for dinner rather than the beer or wine routine that I had fallen into most evenings. It’s too easy to fall into this routine.

I really was not enjoying the person I was seeing in the mirror.

In September 2018 I weighed 14 stone and was fairly active but that was changing for the worst to inactive. A comfortable weight if a little more than it should be for a 6.2ft chap. Come Christmas I was sat at the 16st mark. Then around my accident late January I was 15.5st and then shockingly by June 2019 I was 18.5st. Now I know I was immobilised but that’s a big gain and sadly it continued and at my worst some two weeks ago I weighed in at 18.7st. I could also blame hotel life working away, the stress of work and personal changes but I won’t. They all still point to me. With the strongest determination and drive, I said to myself this had to change and by change I meant action. In no circumstances was I going to allow my weight to keep increasing and get any nearer to reaching 19 stone.

One meal a day diet (OMAD)

Those of you that know me will know I am a gadget man and I love my food and drink. With that in mind, I decided to find a diet that would work for my lifestyle. After lots of reading and research, I found one meal a day diet also known as OMAD. This seemed pretty easy to manage and something I know I could manage as I’ve often got by with just the one meal. The one meal a day diet involves just that. It is that simple. Eat one meal a day. So in effect fasting for two meals or 23 hours of the day. And better still you have the freedom to eat what you like (within reason) and it is absent of any need to create meal plans etc although I do plan to track my nutrition. Just because the diet permits you to eat whatever you like doesn’t mean you should. You should make sure your one meal is balanced and giving you sufficient nutrition. My plan is to stay on the OMAD diet until I reach my weight goal, to be more aware of my weight and create a structured exercise routine.

I love my tea and coffee and this is still permitted however I have decided to drink more black tea and coffee and more green and peppermint tea to avoid the milk. I’ve also massively increased my water intake and very conscious of it. The one meal I eat I am keeping under 1250 calories and ensuring is healthy and balanced but also things I enjoy. I have a treat day each Saturday where I am allowing myself to eat more of my allowance and enjoy a drink with friends if I choose to. A key part to the one meal a day diet is strong self-discipline and willpower of which I surprisingly have found I have. Early into the diet I found myself reaching for chocolate and then deciding against it. So that is the food side nailed.

Now for the gadget side. Each morning I record my weight on my Withings body+ scales. This has mostly been rewarding with the scales reporting in the right direction apart from one day where there was a small gain. These scales give you a little graph on the scales to show you your progress and it shares the data with Apple Health over WiFi. It uses bluetooth and a weight profile to know who is on the scales to avoid sharing the wrong weight if someone else uses the scales.

Graph showing my weight over the last month. The graph shows a weight of 18st 5lb on the 23rd October.
My weight a week ago reported from the Withings scales.

Throughout the day I track my food and drink intake using MyFitnessPal. This allows me to search for food to get their nutritional information and scan for products using their barcodes. MyFitnessPal then calculates my daily allowance and takes into account my fitness and activities. For example, if I go on a long walk, swim or cycle it adds calories I burn from these activities to my daily allowance. Alongside MyFitnessPal I also use WaterMinder to give me a little nudge throughout the day to drink water. I’m a big tea drinker and the app allows me to record other drinks such as tea, coffee, beer and wine. I’m working in the Netherlands at the moment and they really like their tea black. This has led to me drinking more black tea but also more green and peppermint tea. So far I haven’t had any problems holding out for my one meal. As I’m not restricting myself on what I eat I am still able to enjoy all the lovely food I was used too. I’m avoiding some foods such as bread and those high in fat and sugar to help my progress.

WaterMinder
My dinner captured in MyFitnessPal

In terms of exercise, I am trying to walk for an hour each day which I typically do at lunch or in the evening depending on work. At the weekends my daughter and I go on a nice long walk which usually involves a National Trust property or estate. I record these using the Apple Health app or Strava. My exercise plan involves me swimming every other day and cycling every other. When swimming or walking I use my Apple Watch to track my exercise. I also try to close all the health rings throughout the day. When I’m out cycling I use my Wahoo Elemnt with their ANT+ sensors of my bike for cadence, speed and I also have a heart rate monitor. Once I’m back Wahoo shares the data with Apple Health and Strava. I also track my sleep with Withings Sleep Sensor which is placed discreetly under the mattress. It means you can just forget about tracking your sleep when your home without having to rely on wearables and is very accurate. Strava, Apple Health and MyFitnessPal are at the heart of my tracking. I really enjoy the kudos my friends give me on Strava.

All the gear and no idea.
The fun side of exercise – me falling off my mountain bike. #NoPainNoGame

I’m just coming up to a full week and I’m sat at 17st 12lb (8lb loss) and feel so much better in myself. I don’t feel sluggish. I feel more flexible and committed to my plan. My sleep has improved slightly but my ankle and leg give me so much pain I don’t sleep very well as a result. Within two weeks I suspect I will reach my first goal losing a stone and quickly homing in on the sub-seventeen mark. I suspect later in the month I might have a little blip as I have a bucket list trip to New York for my birthday. I’m under decided whether I will keep up my OMAD diet or not but the plan is to try and get my weight down to 13 or 14 stone and keep it there.

Wish me luck!

Reference

A diet like this is not for everyone. I’ve shared some interesting articles on the one meal a day diet that both praise it and caution of it. Please seek professional and medical advice if you plan to diet.

Little Morton Hall

Visited Little Morton Hall, another National Trust property this weekend. This is a very wonky Tudor property that has been left entirely in its original state. The great gallery has some claims to tennis. I have shared a few more photos that are available on 500px here.

National Trust: Little Morton Hall
National Trust: Little Morton Hall
A very wonky Tudor hall.

You can browse my published portfolio over at 500px. Be sure to share your feedback.