I get asked this a lot, so thought I’d create a quick HOWTO on how to create a multi-tariff energy measurement system, for the dashboard etc. YMMV.
How to do multi-tariff power monitoring on Home Assistant. Assumes that you have some existing sensor that provides power consumption (W), or energy consumption (Wh).
create a template sensor that picks the right tariff (as a string) based on the logic that your location uses. Make sure it only outputs the tariffs you defined above.
eg (I have two separate tariff - one for import, one for export)
template:
- sensor:
# peak time is 3pm-9pm every day
- name: "Electricity Tariff"
unique_id: 230e307a-6150-4c2e-8571-7634dec8fcd9
state: "{{ 'peak' if today_at('15:00') < now() < today_at('21:00') else 'offpeak' }}"
# Time Of Use (TOU) – Local time Feed-In rates
# Peak (4pm – 9pm)
# Sholder (9pm – 10am) & (2pm-4pm)
# Off-Peak (10am-2pm)
- name: "Electricity Export Tariff"
unique_id: 541d7af3-7518-42b4-ad78-7fba858c7770
state: "{{ 'peak' if today_at('16:00') < now() < today_at('21:00') else ('offpeak' if today_at('10:00') < now() < today_at('14:00') else 'shoulder') }}"
in order to create the multiple sensors that apply your tariffs, use utility meter helper
supported tariffs = add each of the tariffs you created in step 3 (as labels)
this will create a “base” sensor (which controls which tariff is currently in use), and one sensor for each tariff (which counts the amount of energy used on each tariff)
now, you need a bit of automation to change the utility meter selection (from step 3) whenever the current tariff changes (from step 2)
the trigger is “When Electricity Tariff changes state or any attributes”
type=state
entity=the sensor you created in step 3
the action is to set the current tariff into the (first) sensor created by the utility meter
type = call service
service = select: select
target = (entity) the first sensor you created in step 5
option = “{{ trigger.to_state.state if ‘to_state’ in trigger else ‘offpeak’ }}”
you can also create this in YAML mode:
alias: Set Electricity Tariff
trigger:
- platform: state
entity_id: sensor.electricity_tariff
not_to:
- unavailable
- unknown
action:
- service: select.select_option
target:
entity_id: select.daily_energy
data:
option: "{{ trigger.to_state.state if 'to_state' in trigger else 'offpeak' }}"
mode: single
Now you have two energy sensors, which accumulate during the day, depending on which tariff you are currently in. You can add them into your energy dashboard individually (so you can get accurate pricing, and different colours)
When you add the utility_meter (say peak) to the energy dashboard, you pick the usage sensor (A) and you also configure a pricing sensor (B). Make sure you align peak-usage with peak-price, offpeak-usage with offpeak-price, etc
To debug your tariff selector, you can use the template editor. Change the now() to a local variable (nowish) then you can loop across the whole day and check the results. For example
{% for offset in range(24) %}
{% set nowish = today_at('00:05') + timedelta(hours=offset) -%}
{{ nowish }} "{{ 'peak' if today_at('16:00') < nowish < today_at('21:00')
else ('offpeak' if today_at('10:00') < nowish < today_at('14:00') else 'shoulder') }}"
{%- endfor %}
Thanks for this. Im getting my head around it. My question is what would the code for the template look like for 2 lots of peak rates during the day? For example, my peak rates are 7am-9am and 5pm-8pm, my shoulder times (2 lots 9am-5pm, 8pm-10pm) and off peak time (10pm-7am) would be covered by the above example. Thanks in advance.
edit: also my energy is provided in kWh, how do I change this using the reimann sensor to Wh?
I have been pullin my hair out to get this to work. I have a emporia vue 2 sensor on my main feed and its measured in watts. I have created a Integration - Riemann sum integral sensor to convert it to kWh. but i cant figure out how to get the tier to work properly. I have a 2 tier set up where the first 1350kwH are priced at $0.0975 and anything above is $0.1405. my utility company is set up on a bi-monthly billing cycle so i have added a cron to my utility yaml but i’m not sure i added it correctly.
cron: "0 0 6 2,4,6,8,10,12 * " so it resets on the 6th day of every even month
I am on a tarrif that changes it’s rates every 30 minutes (Agile Octopus in the UK). I use an HA integration that reports the current tarrif at any time.
I’d like to track the cost of charging my car. I’ve got the energy rate in kWh as a Reimann integration sensor.
I think I should be able to create a Utility Meter helper that has the Reimann integration as input, but giving the tariff sensor as the Supported Tariff is just returning the name of the tariff entity. I don’t understand how the tariff input works - it isn’t documented in enough detail. Any pointers?
in step 2 you write “create a template sensor …” - I am struggling to do this as i think the YAML code and files have changed since you wrote this - do you have an update? This is what I am looking for (for our tariffs), thanks, John
I was having trouble following the instructions kindly shown here, after a certain level of complexity with computers a monkey in my head starts banging a big cymbal and I lose track…
I eventually found the following YT vid which showed a similar process well:
and another one which helped me work out my situation which was peak, shoulder and offpeak during the week and offpeak all weekend.
Between the two I got it working quite easily.
It’s a little more straightforward, mostly UI based with a bit of cut and paste yaml which you just need to modify to suit your needs.
I am looking into this now to try and create a comparison.
Essentially…I want to compare a fixed rate tariff (easy), my current Tibber price (already set up), Octopus Go (2 different prices a day) and Octopus Go + Variable grid cost (3 Prices a day in different windows). This is turning out to be rather difficult for me…
I managed to solve it (I hope) by first creating a Utility Meter with an hourly reset (reading from my Solar Inverter Power Meter), using that to calculate the current cost via a Template and then using that as the source for a Utility Meter with Monthly Reset.