Here is my method which I think could cover a large portion of the HA use cases and could be coded into the energy dashboard.
For grid consumption the cost is based off my import price:
My energy price changes every minute, so i multiply the energy price by the the current power being drawn from the grid, so based on my current cost, the nominal 1000W load costs $0.17 to run for 1 hour, which can be calculated using the Rienmann Sum integral.
Making things a little more complex if I am running off battery or solar. I attribute the cost to the value I could have achieved if I exported my solar or battery at that point in time.
I have sensors that report the power being delivered to the house by solar or battery and multiply that by my current feed in price. So in this case the nominal 1000W load costs $0.08 to run for one hour, which again can be calculated by Rienmann Sum integration.
Now into the more complex case, what happens when I am running off solar, battery and grid at the same time.
I just combine the entites above.
In total the house is consuming 12,073W (which is 1,736W from grid, 5010W from battery and 5343 W from solar), which is costing me $0.3/hr for grid and $0.83/hr for battery+solar for a total of $1.13/ hr. Divide by the house consumption of 12,073W gives me a house consumption cost of $0.09/ kWh. So the nominal 1000W load will cost $0.09 to run for one hour, but can be more accurately calculated using Rienmann Sum.
Sensor definitions below:
- name: house_consumption_grid_cost_rate
unit_of_measurement: $/hr
state: "{{(states('sensor.amber_general_price')|float(0) *
states('sensor.apf_grid2house')|float(0) /1000)|round(2)}}"
- name: house_consumption_feed_in_cost_rate
unit_of_measurement: $/hr
state: "{{(states('sensor.amber_feed_in_price')|float(0) *
(states('sensor.apf_batt2house')|float(0)
+ states('sensor.apf_solar2house')|float(0)) /1000)|round(2)}}"
- name: house_consumption_cost_rate
unit_of_measurement: $/hr
state: "{{(states('sensor.house_consumption_grid_cost_rate')|float(0) +
states('sensor.house_consumption_feed_in_cost_rate')|float(0))|round(2)}}"
- name: house_consumption_cost
unit_of_measurement: $/kWh
state: "{{(states('sensor.house_consumption_cost_rate')|float(0) /
states('sensor.apf_house_entity')|float(0)*1000)|round(2)}}"