How to create a sensor for kWh

hi all,
my power company charges $0.2104 per kWh for the first 425 kWh and $0.2166 for the excess of 425 and a charge fee of $4.00.
how can I create a sensor to reflect those numbers so I can use it in grid consumption
thanks Miguel

1 Like

Add two entities, one for current monthly usage and the other for current price. Add an automation with a numeric state as a trigger: when your monthly usage goes above 425, update the value of the current cost. Not sure of a good way to add that four dollars though.

If you are going to use this in Energy tab, then there is an option to select an entity which gives the current price of the power consumed. We just have to create that entity which will change the current price to 4 dollars when the total consumption goes beyond 425. This can be done with a template sensor.

template:
  - sensor: 
      - name: 'Power Unit Price'
        state: > 
          {% if states('sensor.power_consumed')|int<425 %}
              {{4/states('sensor.power_consumed')|int+0.2104}}
          {% else %}
              {{4/states('sensor.power_consumed')|int+0.2166}}
          {%endif%}
        unit_of_measurement: '$/kWh'

Do change the entity ids

1 Like

basically there is a $4.00 charge no matter how much power is consumed, that’s like a base price then there is a tariff of $0.2104 per kWh up to 425 kWh, after that (ie 426 and up) the tariff goes to $0.2166 per kWh.it wont change to $4.00 after 425kWh.
thanks anyways I got a start just that im a real newbie and dont understand all this

If the excess charge is only levied on the power consumed beyond 425kw, instead of the price per unit charged, we have to select the entity to calculate total power price in the energy tab and create the following template sensor which will give the total energy price.

template:
  - sensor: 
      - name: 'Total Power Price'
        state: > 
          {% if states('sensor.power_consumed')|int<425 %}
             {{4+(states('sensor.power_consumed')|int*0.2104}}
          {% else %}
             {{4+(425*0.2104) + ((states('sensor.power_consumed')|int-425)*0.2166)}}
          {%endif%}
        unit_of_measurement: '$'

If the entire power consumed is charged at 0.2166 after 425, use the previous code.

the first 425 will stay charge at .2104 the excess of 425 will increase to .2166
so up to 425 will be at .2104 from 426 will be at .2166
example 4.00+{425x0.2104)=93.42
from 426 on will start 1x.2166=.2166
so 426 will be 89.42+.2166=93.67
and 500 will be (75x.2166)=16.24+the $4.00 and the first (425@ .02104=93.42) for a total of $109.66

Use the above total energy price sensor

Invalid config
The following integrations and platforms could not be set up:

template
Please check your config and logs.

Logger: homeassistant.config
Source: config.py:464
First occurred: 11:37:43 PM (1 occurrences)
Last logged: 11:37:43 PM

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘sensor’][0][‘state’]. Got “{% if states(‘sensor.power_consumed’)|int<425 %}\n {{4+(states(‘sensor.power_consumed’)|int0.2104}}\n{% else %}\n {{4+(4250.2104) + ((states(‘sensor.power_consumed’)|int-425)*0.2166)}}\n{%endif%}\n”. (See /config/configuration.yaml, line 41).Processing: Untitled document.pdf


The above tells you the issue, you have a missing parenthesis:

{{4+(states('sensor.power_consumed')|int*0.2104)}}
1 Like

As @r-j-taylor said try this.

template:
  - sensor: 
      - name: 'Total Power Price'
        state: > 
          {% if states('sensor.power_consumed')|int<425 %}
             {{4+(states('sensor.power_consumed')|int*0.2104)}}
          {% else %}
             {{4+(425*0.2104) + ((states('sensor.power_consumed')|int-425)*0.2166)}}
          {%endif%}
        unit_of_measurement: '$'

Missed a)

thank you guys. this works.
now I have another request, is there a way to reset or restart the cost on a specific day of the month?
my billing cycle starts on the 9th of each month.

Depending on how you want to go about it, {{ now().day }} or a time-related trigger.

More information on using time in HA can be found at these links:

Instead of resetting the total price, we can look into resetting the total energy. This can be done with utility meter integration.

You should add your energy consumed sensor to this platform and set the offset to 9 and select monthly cycle. So it will reset the energy on that date. Now you can use the utility meter entity with the earlier template to give the total price accordingly.

so this should work?

template:

  • sensor:
    • name: ‘Total Power Price’
      state: >
      {% if states(‘sensor.power_consumed’)|int<425 %}
      {{4+(states(‘sensor.power_consumed’)|int0.2104)}}
      {% else %}
      {{4+(425
      0.2104) + ((states(‘sensor.power_consumed’)|int-425)*0.2166)}}
      {%endif%}
      unit_of_measurement: USD/kWh
      utility_meter:
      energy:
      source: sensor.power_consumed
      cycle: monthly
      offset:
      days: 9
      hours: 0
      minutes: 0

no it didnt 68kWh shows’ almost $150

Please paste the code between ``` so that it is correctly rendered. There are some errors in code as I can see.
The utility integration will look like this.

utility_meter:
  monthly_energy:
    source: sensor.power_consumed
    name: Monthly Energy
    cycle: monthly
    offset:
      days: 9
      hours: 0
      minutes: 0

Make sure that sensor.power_consumed is the total power irrespective of the time period i,e this sensor should not be reset. This will create a sensor with id sensor.monthly_energy which we need to use in the template.

template:
  - sensor: 
      - name: 'Total Power Price'
        state: > 
          {% if states('sensor.monthly_energy')|int<425 %}
             {{4+(states('sensor.monthly_energy')|int*0.2104)}}
          {% else %}
             {{4+(425*0.2104) + ((states('sensor.monthly_energy')|int-425)*0.2166)}}
          {%endif%}
        unit_of_measurement: '$'

Hello, i follow your steps for creating a “Total Power”.
I’d like to create a “Net Power” : total power consumed - power produced by Solar.

But i cannot see this sensor in the “return to grid” combobox. How to create a variabile that i can select in the energy management?

thank you

Old topic, but hope that anyone can see what’s wrong with this snippet. This sensor refuses to show when I’m trying to add it to the Energy tab as a consumer sensor. It works otherwise to use in entity cards. I have all my sensors in this format in a sensors.yaml file.

- platform: template
  sensors:
    power_consumption: 
      friendly_name: "Effektförbrukning total"
      unit_of_measurement: "kW"
      value_template: >
        {{((states.sensor.nibe_52610_40083.state|float*240 + states.sensor.nibe_52610_40081.state|float*240 + states.sensor.nibe_52610_40079.state|float*240)/1000)|round(2) }}
      icon_template:
                mdi:flash

how did you get it to reset with your utility meter so each bill is lined up?