I’ve added this as a feature request here. Feel free to upvote
We have a similar situation here in Australia (charged $0.26 per kWh and a daily charge or $0.9393 per day, and the way I dealt with it was as follows:
- platform: template
sensors:
energy_import_cost:
friendly_name: 'Import Cost'
icon_template: mdi:home-import-outline
unit_of_measurement: '$'
value_template: "{{ (states('sensor.grid_consumed_energy_day') | float * 0.262026 + 0.9393) | round(2) }}"
Cheers
+1 for this - I’ve upvoted the feature request!
Hi Guys,
Attached is what I did, effectively I used an input number home assistant function that uses kWh as the measurement. I have an automation to increment that input number once per day, then separately created two utility meters, to reference that daily incrementing input number. One import and one export (charge the supply charge per kWh on the import, and don’t provide a cost for the export but reverse the kWh).
Hope this helps / provides inspiration for someone to do something different, happy to provide config samples from yaml / how to guide.
One big gotchya is to make sure you create the utility metres before the input number increments, and also you will basically need to wait another increment after adding the utility meters to your energy config (but that kind of makes sense, if a little painful).
I am interested in your approach. Could you please provide your setup with some basic instructions to get me up and running with this.
Thanks
Hi All,
I had a go at a kludge for this. It seems to be working well so I thought I’d share.
I basically created a sensor that uses 0.001kW per hour. Then I take the daily supply charge, multiply by 1000 and divide by 24. This give me an hourly static price I can use to multiply by the sensor to get an hourly supply charge.
So in detail the sensor looks like this.
- name: "Supply Charge"
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
state: >-
{{ (((as_timestamp(states('sensor.date_time_iso')) / 3600)-454585) | int) / 1000}}
attributes:
last_reset: '1970-01-01T00:00:00+00:00'
The formula for the sensor is basically the date & time in hours
(as_timestamp(states('sensor.date_time_iso')) / 3600)
minus an offset of hours since 1 Jan 1970 00:00.(Unix Epoch) In my case this was 454585 but it will be different depending on the time you start the sensor.
I take the integer and divide this by 1000 to get the smallest possible unit (1Wh). So this sensor now increments by 1Wh every hour forever. This will get added to your hourly grid consumption but this value should be negligible and well within the margin of error of most measuring devices.
Once I created the sensor I added it to the energy config screen under Grid Consumption as below
Note: The static price used is 1000 times the actual price for 1 hour supply.
In my case I pay 105.1400 cents per day supply charge, so when I adjust this to an hourly static price I get 1.051400 *1000/24 = 43.81 (you can only use 2 decimal places in the config price)
It’s not perfect but I hope it works for others.
PS. You could easily use this same approach for a “daily” sensor but I wanted to spread the cost across hours in the day so I can compare it to another system I have that does something similar.
That’s brilliant! Simple, but clever. I’m going to give this a go now!
Thanks, hope it works for you
Hi Dormani,
First of all create an entry in your configuration.yaml:
# Input number
input_number: !include input_number.yaml
Now create a file input_number.yaml .
Contents of input_number.yaml
daily_kwh:
name: Daily kWh
min: 0
max: 1000000
step: 1
unit_of_measurement: kWh
Next restart home assistant to create this value (note they will all be 0 and undefined as to their units of measurement at this stage).
To get your input number to start storing values you need to increment that input_number.daily_kwh; so create an automation in your Configuration/Automations/Add Automation area as follows (yaml provided or use the UI to create yourself).
alias: Increment kWh
description: ''
trigger:
- platform: time
at: '00:01'
condition: []
action:
- service: input_number.increment
target:
entity_id: input_number.daily_kwh
mode: single
Next run the actions on your new automation.
Now that you have a value stored in input number (N.B don’t create your utilities below until you have data stored in your input number), now update your configuration.yaml as follows:
# Utilities
utility_meter: !include utility_meter.yaml
Create a file called utility_meter.yaml
daily_supply:
source: input_number.daily_kwh
name: Daily Supply
cycle: daily
tariffs:
- charge
daily_correction:
source: input_number.daily_kwh
name: Daily Correction
cycle: daily
tariffs:
- count
Restart your home assistant a second time to create the utilities, Run the automation “Increment kWh” to increment the kWh again (your utility meters should now start record data).
Now if you look at the developer area in Home Assistant you should be able to see something like this.
You will now have two utility meters that reset to 0 at midnight and then increment by 1 kwh at 1 minute after midnight each day, after HA energy detects these as available with data (usually 2 hours) you will be able to add them to your Home Assistant energy config as follows noting you can have as many consumption and export sensors as needed:
Grid Consumption
daily_supply charge
add your daily supply charge /standing charge/grid connection fee here (for me 0.96 per kwh)
Return to Grid
daily_correction count
No charge
This fixes your bottom line kWh count and also provides the per day standing charge/grid connection fee/daily supply charge.
Hope this helps.
Warm Regards,
Sean
P.S lessons learnt Home Assistant doesn’t like mixed measurements in it’s recorder so do not create your utilities until you have incremented your input number.
Thanks Sean. Your instructions are spot on and I have all set up. I will now look at adding sensors that have been created to Energy platform. Thanks once again for your response.
All the best
Ian
@sean.mcgee - thanks a great work around - just to note:
only one call to utility_meter can be made in the configuration files and only the last call to utility meter is used I therefore have all my utility meters in the utility_meter.yaml or the main configuration.yaml
I always screw this up:
What should come before this?
Should it be
sensor:
- platform: template
sensors:
Can someone point me to a good source in understanding why I struggle with this?
@NieropT Hi Thijs, I know what you mean. It can be confusing so I’ll try to help.
Home Assistant has sensors which I think of as measuring things at the base level. eg Temperature, Voltage etc.
These are prefixed with
sensor:
Quite often however you need to convert the sensor data into something more meaningful and derive a new value from the sensor data. A simple example might be:
If the time is < noon then AM else PM
This is called a template and these are prefixed with
template:
For a template you then have to define what sort of template it is, eg sensor, binary_sensor.
In my example I want to use the template to create a new sensor so the whole thing will be
template:
- sensor:
- name: "Supply Charge"
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
state: >-
{{ (((as_timestamp(states('sensor.date_time_iso')) / 3600)-454585) | int) / 1000}}
attributes:
last_reset: '1970-01-01T00:00:00+00:00'
Hope this helps
Legend. Thank you very much.
Thanks Sean. I made two changes to your setup - I set the step to 0.001 kWh instead of 1 kWh in the input number, and then set the charge in the Grid Consumption source to be 1000 x the daily charge. This works really well, I am now having the daily charge accrue but without the 1 kWh up/down spike appearing on the graphs and affecting the %s or amounts in the distribution diagrams.
Thank you for the instructions. I just followed them but the sensor.daily_correction_hs_count
has no device_class: energy
(like on your screenshot) . That is why I can’t add it to the Homeassistant energy config.
Did I miss something?
EDIT: Nevermind it worked after the first increment this night. THANK you
alias: Increment kWh
description: ''
trigger:
- platform: time
at: '00:01'
Thanks a lot for this genius hack! Rather than distributing the supply charge throughout the day, I tried to keep it simple and create a spike when the time transitions to the next day. Also I can use the supply charge cost from my energy retailer as is.
template:
- sensor:
- name: "Supply Charge"
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
state: >
{{ (as_timestamp(states('sensor.date_time_iso')) / 86400) | int }}
Will this come and bite me in later on somehow?
Hi @aksvenk, this approach is fine, except you must remember by doing it this way you are adding 1kW to your power consumption every day, which may be significant in your overall consumption. My average daily usage is about 15kW so this would introduce a margin of error of about 6-7% every day.
That’s why I divided it by 1000, so I’m only adding 1w per hour which is negligible and an error margin of about 0.1%. I chose 1w because it seems to be the lowest unit of power HA will allow.
In your case if you mutliply the charge by 1000 and divide the sensor by 1000, it would only be 1w per day which would be even better.
Good point @muzzak. The supply charge shows up as a separate line item so I’m not too fussed.
But I see what you mean especially when we are tracking monthly / yearly totals.
As per your suggestion, I’ll change the above unit to Wh and then multiply the supply charge by 1000.
Hi Michael,
Yes that’s correct, as per the yaml standards one heading with utilities as subentries beneath indented or a reference (as I have done) to a separate file for your multiple utility entries.
Thanks,
Sean