Custom Smart Thermostat

Last year I programmed my own custom smart thermostat using Home Assistant and a couple of WiFi enabled heaters. I’m writing this article mostly to document the process of how I control my heaters in the living room and to hopefully inspire others to implement their own solution. I will also try to update the numbers in this blogpost as I make changes to it in the future.

It was important for me that this system would work the entire year round and always provide us with a comfortable indoor temperature whilst simultaneously saving money by (temporarily) turning down the heat whenever the electricity prices are high or when we don’t need a heated living room because we are for example sleeping. Ultimately, this system should run in the background without any of the inhabitants even knowing that it exist.

I got inspired by a blogpost by Martin Bekkelund (article in Norwegian). His basic idea is as follows:

  • Change the heaters set temperature based on a couple of variables such as time of day, electricity price, etc.
  • Allow for a couple of ‘overrides’ such as vacation mode to lower the temperature when nobody is around.

I took this idea and started defining how it should work. Firstly I looked at the operating temperature. In Martin’s blogpost he defines his minimum temperature to be 5°C. This was a bit low for my liking, so I upped that to 18°C. Even if we’re away I don’t want the temperature to sink much below this as it would take a long time to heat the house up again and probably keeping a temperature of 18 degrees would not cost that much more energy than letting the house cool down and heating it up again to 18°C. At the time I programmed this I also wasn’t sure my algorithm would always predict perfectly if people were home or not, meaning that even if the algorithm messed up people in my living room would at least be sitting in 18°C.
For the maximum temperature I decided to go for 22°C. After running this system for a year I experienced that my wife finds 21°C a bit too cold, so we now use 22°C as the maximum.

Secondly, I had to define which variables would influence the set temperature.
After a year I have the following variables contributing:

  • The outdoor temperature.
  • The energy price:
    • General energy price (is today an expensive day?)
    • Peak energy price (Is energy significantly more expensive this hour compared to the rest of the day?)
  • Amount of sunshine entering through the window.
  • Manual override.

My idea was to look at these variables and assign a number to each of them, depending on their value. I would then add all the numbers together, and this would create a temperature offset. This way I quantify as many variables as I want and directly translate them to a desired living room temperature.

Thirdly I defined 3 overrides:

  1. Night Mode. Turns on at 22:00 and turns off at 06:00.
  2. Weekend Away mode. Turns off automatically on Sundays at 15:00.
  3. Vacation Away mode. Does not automatically turn off.

Temperature Offset

The temperature offset will be a calculated number based on the variables defined above. I defined the base temperature in the house as 21°C, and the temperature offset will be added to this temperature to give the final setting for my heaters, within a minimum and maximum of 18°C and 22°C respectively. So for example, if the temperate offset is calculated to be -2, the heaters will be set to 21°C – 2 = 19°C. If the temperature offset is +5, the heaters will be set to 21°C + 5 = 26°C 22°C as the maximum temperature is 22°C.

After a year of running the system my variables are quantified as follow:

Outdoor Temperature Offset
< -10°C +3
> -10°C & < -5°C +2
> -5°C & < 0°C +1
> 0°C & < 5°C 0
> 5°C & < 10°C -1
> 10°C & < 15°C -2
> 15°C -3
General Electricity Price Offset
< 1 NOK/kWh +3
> 1 NOK/kWh & < 2 NOK/kWh +2
> 2 NOK/kWh & < 4 NOK/kWh 0
> 4 NOK/kWh -1
Peak Energy Price Offset
Hourly Price < 130% Daily Average 0
Hourly Price > 130% Daily Average -1
Sunshine through the window Offset
Yes -1
No 0
Manual Offset Offset
Manual offset. Resets daily at 00:00 Between +3 and -3

Remember that a + means more heat, a – means less heat. As mentioned the algorithm goes through all the variables one by one and adds/subtracts the offset value based upon what that variable is at the time the algorithm runs. Whenever one of the variables change the algorithm reruns to recalculate the offset.

Additionally I defined an offset for the override modes. They’ll just get a large minus number to make sure they always default to the minimum temperature of 18°C.

Mode Offset
Night Mode -9
Weekend Away mode -10
Vacation Away mode -10

Night mode has a different offset than Weekend Away mode and Vacation Away mode to in the future allow myself to differentiate between the two, in case I want to expand the system and for example want a different temperature when we’re away vs. when we’re sleeping.

Implementation in Home Assistant

This algorithm runs inside my Home Assistant instance. Home Assistant allows the definition of so-called ‘helpers’. I created a number of these helpers and virtual sensors which help create the main automation that calculates the offset:

Helper Explanation
Heating: Manual Offset Manual input number which can vary between +3 and -3.
Heating: Night Mode A schedule which defines when night mode should be active.
Heating: Offset An input number which holds the current offset.
Heating: Offset Calculator An input number used during the calculation of the offset.
Heating: Outdoor Temperature Combines my 2 outdoor temperature sensors into a single number.
Heating: Sunshine Switch A threshold sensor that turns on when the light sensor in front of my window senses more than 1000 lux.
Vacation Away An input boolean which can be turned on and off manually.
Weekend Away An input boolean which can be turned on and off manually. Automatically turns off on Sundays at 15:00
High Energy Price Binary A binary sensor that turns on when the hourly energy price is >1.3x the daily average energy price

Additionally I am getting the energy price from my energy provider Tibber.

Automations

There are 2 automations running in Home Assistant. The first one calculates the Heating Offset, while the second one sets the heater’s temperature based upon the newly calculated Heating Offset number. This is because the Heating Offset can be calculated multiple times during the day but won’t necessarily change all that much. As long as it doesn’t change, the heaters will be left alone and won’t receive an update.

The first automation to calculate the Heating Offset is in essence a large If/Then/Else function. If any of the overrides are on Then we set the Heating Offset Calculator to the value of whatever mode is active. Else we calculate the Heating Offset Calculator based upon the variables and their respective offset described in the table above.

Actions in Heating: Calculate Offset.

When this action is done I set the Heating Offset variable to the value of Heating Offset Calculator, which was just calculated. Setting Heating Offset to a different value triggers the next automation which sets the living room heaters to their new required temperature. If Heating Offset does not change compared to the last time the automation ran then the second automation is not triggered.

Set Heating Offset from Heating Offset Calculator

Experiences last year

Last winter we experienced some of the highest energy prices in Europe since ever. This mainly drove the Heating Offset during the winter, lowering the temperature of the heaters whenever there was a peak and thereby lowering my energy bill. Unfortunately I do not have numbers on how much money this has saved.

In the summer the Heating Offset is mainly influenced by the Sunshine sensor and the Outdoor Temperature sensor, lowering the heaters to 19°C/20°C during most of the month of August. Additionally we were on holiday in the beginning of August, causing the heaters to be at 18°C all day.

Living Room heater requested temperature vs Outdoor temperature in August 2023. Click to view in full screen.

The last couple of weeks the outdoor temperature has been fluctuating around 0°C. This has caused the heaters to be 21°C (22°C since last week, when I changed the maximum temperature) almost continuously:

Living Room heater requested temperature vs Outdoor temperature in November 2023. Click to view in full screen.

Now when the weather is getting even colder I expect the heaters to be on 21°C/22°C for most of the time, as long as the energy prices are not getting out of hand again.

Improvements

As I mentioned, this system has now been running for over a year. During this year I’ve made a few minor changes, mainly tuning of the Outdoor Temperature offset and the addition of a Manual Override to add/subtract to the Heating Offset. The system has mostly been running in the background and whenever I glanced at the heaters the set temperature often made sense. My house inhabitants have unfortunately complained a couple of times about my system (so they do know it exists), but the root cause was almost always a broken WiFi connection with the heaters, instead of the algorithm making weird calculations. To improve this I have created a 3rd automation which resets the power to the heater, by toggling a power switch connected to the heater’s power cord, whenever Home Assistant notices that it stops reporting its measured temperature, forcing those pesky heaters into submission!

I feel the minimum and maximum temperatures might be a bit restrictive, so I might tune this in the future. As said, I’ll try to update the numbers whenever I make changes. If you have idea’s please let them know below!