Let me start this article with, I an 100% a new comer to Home Assistant as of 1 week ago. There is so much to learn that most people take for granted. So many posts on the internet just say they found the solution but never show how or their answer is so cryptic or hard to follow because they assume you are a Home Assistant Guru.
TLDR Link (Too Long Didnt Want to Read) AKA skip the story and get to the code
Background
Here is my adventure and why I am doing what I am doing. My daughter has a heater in her room that is analog and has a manual temp dial on it. We live in Ontario, Canada were temperatures can spike all over the place. One day it is 10c and the next -10c. That is a delta of 20c and our house has to acclimate to and have our furnace keep up with. To combat this fluctuating temperature we figured we would just put a small oil heater in our daughters room to help keep that room at a consistent 20c. I was hoping the manual dial on the front of the unit would help keep the temp consistent but it is really hard to dial in. it just has a big knob and a raising slope around the dial to indicate temperature increase. In comes Home Assistant to solve the issue. I already have a Hubitat hub setup in the house to control all the smart stuff in our house. I installed TrueNAS some time back on a small server and installed frigate to pull in all the camera feeds for the house. I already have the setup so I thought lets try out Home Assistant. I installed Home Assistant as a virtual machine (HAOS) and here we are!
Problem
I want to use a sensor in my daughters room to control the temp and turn a smart plug on and off to control the temp. I would also like to monitor how much energy is consumed by this device to see is it costing us hundreds of dollars a year to run this heater.
Solution
I already have a z-wave plug that does power and energy monitoring and a z-wave temp sensor in her room that is connected to Hubitat. Really all I needed to do to get the info I needed was put it all together in HA to display it on a dashboard. Sounds easy right?
First problem, where do I even start? There are many different way to skin a cat but this is the approach I decided to use after watching so many different videos and reading post after post on the HA forum.
Thoughts
- My current hydro plan uses something call TOU. This means that the price of hydro changes for me based on the time of year (summer & winter) and time of day. I would need a place to store the 3 different TOU prices (on-peak, off-peak, mid-peak) and a variable to tell me if its winter or summer. After some research I found out that HA calles these different price costs tariffs. I decided that number helpers are the perfect for storing the price of each tariff. You can think about helpers as fake sensors that will output the data you need. A number helper will just output a number as its status.
- Because i have 3 different tariffs I will need something to store how much energy was used in each tariff. For this I found that HA uses a helper called Utility Meter. Think about a utility meter as a variable that keeps track of how much energy you are using.
- I would need a utility meter for each one of my tariffs so we can know much of energy has been consumed during each tariff. I figured out if you make a Utility Meter and then add your 3 tariffs to it that it will create what I will call 3 children utility meters so that each one store the energy consumed in each tariff. Then the parent utility meter will act like a diverter. It will take the incoming energy use for what ever device you want to monitor and will divert that energy use to the proper child to collect the energy use. How does it know what child to give it to? Well the parent is actually no longer a utility meter like the children are, it has become a select. Think about this like a drop down and you can select what value you want and you can ONLY choose one of those options. The three option you can choose from are the 3 different tariffs you entered. Now I have to find a way to automate how to change this parents select to the proper tariff so that it diverts the energy use to the proper child collector!
- I thought it was going to be difficult to find a way to program a way to select what tariff to use. It ended up being really simple. I could use another helper this time I was going to use a template sensor. This allows me to input some code to determine the output of the sensor (AKA its state)
- I had no idea how to program this and had no idea what programming language HA uses. After again many searches on the internet I found that HA uses Jinja. I found some code to return the time of use period I was currently in and then used the HA Discord to even better refine this code to some beautiful code that is really easy to read and update it if the TOU times ever change.
- I will need a calendar with Ontario Holidays in it so I can tell if today is a holiday or not because on a holiday the tariff for the day is all the same. I found an integration called Holiday that adds a calendar to the system and it can load up all the Ontairo Holidays
- I will need to add the information to the energy dashboard so that we can see the data
So that’s it, logically this sounds easy and simple. Well there is a bit of work and I will show you step by step below how to do it! Again I am brand new to HA so if something is wrong please feel free to let me know and I will update this post to reflects the proper terminology.
The CODE
Finally on to the fun
- Enable Advanced Mode
When creating the number helper you need to have Advanced Mode enabled so that you can change the input type from slider to input. To Enable Advanced Mode do the following:- In the left menu, click on your profile name at the bottom or click here
- In the “User settings” section go to the bottom of it and look for a setting called “Advanced mode” and enable it. If you want to disable this after you create the number helpers feel free to but it’s a good feature to leave on.
- Create 3 Number Helpers
The only thing these helpers are used for is when you add a consumption device to your energy dashboard it will ask you if you want to calculate the cost for this energy use. You do have an option on this screen to pick a entity that has the hydro price stored or you can manually enter a price. Both methods work exactly the same but if the price of hydro ever changes (and it will) and you have 30-50 consumption devices with the price of electricity all manually set on them, you will need to manually go to each one of those consumption devices and update the price setting. With a number helper you would update the number helper to the new value and all the consumption devices that reference that helper will be automatically updated. I would suggest you use a number helper but to each their own. In this tutorial I will use them.
- Go to Settings > Devices & services > Helpers >+ CREATE HELPER > Number
- Give the number helper a descriptive name so you know what it’s for. I used “Hydro Rates – Mid-Peak, Hydro Rates – Off-Peak, Hydro Rates – On-Peak” (The naming is not important as long as you know what each one if for)
- Give it an Icon if you want, I used the $ symbol so you know this helper stores a money value.
- The Min/Max values you can leave to the default (0,100)
- Change the display mode to “Input field” this way you can open the number helper and just type in a value to set it. If you do not see this option please make sure “Enable Advanced Mode” is turned on (Step 1)
- The Unit of Measurement will be “CAD/kWh” if you don’t set it to this, when you try to reference this value form the energy dashboard setup it will complain that it does not know what the value is pertaining to.
- Click the “Create” button and you should see your number helper listed in your list of helpers
- Click on the number helper you just created, and in the top right of the window that opens you should see an input field where you can manually set the value. IMPORTANT Make sure you enter a DOLLAR/kWh in this field. Rates in Ontario are normally given in CENTS/kWh. So for example If your rate is 7.6¢/kWh you will need to convert this to a dollar value by moving the decimal two places to the left IE 7.6¢/kWh would convert to 0.076$/kWh, 12.2¢/kWh would convert to 0.122$/kWh.
- Repeat this for all the different tariffs/TOU Periods that your energy provider has and name them all the same but change the suffix to the name of each TOU Period. Again for me I used “Hydro Rates – Mid-Peak, Hydro Rates – Off-Peak, Hydro Rates – On-Peak”
- Add Ontario Holiday Calendar
Because in Ontario all holidays are considered off-peak we need a source of Ontario holidays. I created a calendar as a way to tell if today was a holiday for my logic later on.
- Install HA Local Calendar, Settings > Devices & services > + ADD INTEGRATION
- Search for “Holiday” and Install it
- Click on the Holiday integration and click “ADD ENTRY”
- Choose Canada and submit then ON and submit
- If you look now on the left menu you should see a calendar icon and if you click on it you can see your Ontario Holiday calendar!
- Create a Helper for TOU Price Period
The point of this helper is for it to always return what the current TOU Price Period /tariff should be based on the current time. We create this as a template sensor and its only output will be one of our tariffs. This code is based on the following TOU Tables from Ontario Energy Board . The code should be prety easy to follow you put a O, M, or P based on what tariff you should be in on that hour of the day. far left is 12AM far right is 11PM
- Go to Settings > Devices & services > Helpers >+Create Helper > {} Template > Template a sensor
- Give it a descriptive name, I used “Hydro – TOU Price Period”
- In the field that says “State template*” put the following code
{% set isHoliday = is_state('calendar.canada_on', 'on') %}
{% set isWeekend = now().isoweekday() > 5 %}
{% set isSummer = 4 < now().month < 11 %}
{% if isWeekend or isHoliday %}
{% set lookup = 'OOOOOOOOOOOOOOOOOOOOOOOO' %}
{% elif isSummer %}
{% set lookup = 'OOOOOOOMMMMPPPPPPMMOOOOO' %}
{% else %}
{% set lookup = 'OOOOOOOPPPPMMMMMMPPOOOOO' %}
{% endif %}
{% set map = {'O': 'Off-Peak', 'M': 'Mid-Peak', 'P': 'On-Peak'} %}
{{ map[lookup[now().hour]] }} - Note that I used values for my sensor that have proper capitals on them IE “Off-Peak” insted of “off-peak” I did this so that I can use this sensor’s output on a dashboard to let me know what the current TOU period is. I find its nice if these helpers have multipul functions and not just one! All you need to remember is that what ever values you use in this sensor, when you create your utility meter in Step 5 you use the EXACT values this sensor is outputting for your tariff values (Case included) or it wont work!
- Change the calendar.canada_on to be the same entity id that was given to your holiday calendar
- Leave the other fields blank and press Submit
- Create the Utility Meter
This is a place to store the energy consumption data AKA a collector
- Settings > Devices & services > Helpers >+ Create Helper > Utility Meter
- Give it a descriptive name like “Heater Meter”
- For the input sensor, you will need put the entitiy id of the device that gives you the energy consumption reading. MAKE SURE you are pulling the consumption energy from the device NOT the power reading.. You can use Developer Tools > States to look up your device and see what states are what. It should also tell you what the value returned is Wh or kWh
- the “Meter reset cycle” filed should be set to ‘daily‘ as we only care about the data for that day and then we want it to reset, the energy dashboard will pull in and save what is needed.
- “Meter Reset offset*“, leave at 0
- Supported tariffs, enter the names of the different tariffs your provider supports. Enter each of them one at a time and then press enter. You will see them added as a tag above the input field. PLEASE NOTE you must have the exact same values that are returned by the code in step 4.3. This is because when the helper we created in step 4 changes state, we will use the value it changes to to set what this meter is set to. If they don’t match exactly it will not work.
- Leave all the other settings as-is and hit submit.
- If you refresh the page, 4 separate Utility Meters will have been created, 1 parent, and 3 children (one for every tariff you added). You will notice that the child utility meters will have the same name as the parent but they will have the tariff names added to the end of their name. All 4 of these are still of the type Utility Meter but if you look the parents entity ID you will see it is now a select. We can now set it to one of the tariffs and it will divert your devices energy usage to th proper child.
- Create the Automation
We almost have everything we need, all we need now is a way to change what tariff the parent utility meter is set to. Its value should always be the same as the output form the helper we created in step 4. So lets create an automation to do this for us
- Go to Settings > Automations & scenes > + CREATE AUTOMATION > Create new automation
- Click the + Add Trigger button
- Select Entity > State
- In the entity field enter the helper we created in Step 4.
- Change the To: field to be “Any state (Ignoring attribute changes)”
- Leve everything else as their defaults (blank)
- Go down to “Then Do > Add action > Select > Select an option“
- Click “+ choose entity“
- Choose the parent utility meter that we created in step 5
- In the option field we are going to put a bit of code to make the option be the value of the helper we created in Step 4. Remember when the helper that we created in step 4 changes its state to a new state that triggers this automation so lets get the value that it just changed to and set it as the option
{{ trigger.to_state.state }}
- NOTE This will turn off the UI editor and switch you to the YAML editor. This is expected, just have a look to make sure your code looks like the below code. Your entity_id will be named differnet:
action: select.select_option
metadata: {}
data:
option: "{{ trigger.to_state.state }}"
target:
entity_id: select.babysroom_heater_meter - Hit Save
- Add Items to the Energy Dashboard
All the pieces are now in place and we can now add our child utility meters to the energy dashboard
- Go to your Energy Dashboard depending if you have added an item before this there will either be a get started option or will show your energy dashboard. If so go to the 3 dots in the top right and select “Energy Configuration“
- Go to the electricity grid section and select “Add Consumption”
- The “Consumed field” will be set to your one of your child utility meters, does not matter which one but make sure you remember what one you choose here as you have to make sure you choose the price helper that matches to get the right price
- In the lower section choose “Use entity with the current price”
- Choose the number helper that you created in step 2 that matches the child utility meter you chose above
- Do this for all 3 utility metters or how ever many you created
- Be Patient
It will take time for the Energy Dashboard to report any data (up to 24 hours). This is because there is no consumption data in any of your child utility meters yet. Give it a day and come back to it!
Troubleshooting
Making things like this are difficult and hard to trouble shoot when there are so many moving parts. A few things to check if you have having issues is:
Go to the helpers section and look at your helper for TOU Price Period and click on it. You should see the history of what it was and what it changed to. After 24 hours you should see that it was set to the proper values at the right times. Check the parent utility meter it should also show the history of what it was set to and when and it should follow the same pattern as the previous helper.
Make sure that you used the exact same name for your tariffs that your TOU Price Period Helper outputs!
Developer tools are your best friend. Go load a one of your helpers and see if it returns the value you expect. Test each one of your entities to make sure if you are having weird results.
Credits
https://community.home-assistant.io/t/quick-tldr-on-how-to-create-multi-tariff-energy-meters/585379
https://community.home-assistant.io/t/template-outputting-code-as-part-of-the-string/738378/15