Template sensor for standing charges not working

I have the below template sensors in place. The idea being that each day they add the kWh value that works out the cost of the days standing charge. However both just seem to go up by 1 every day

- trigger:
     - platform: time
       at: "00:00:01"
  sensor:  
     - name: Electricity Standing Charge
       unique_id: electricity_standing_charge
       unit_of_measurement: 'kWh'
       device_class: energy
       state_class: total_increasing
       state: "{{ states('sensor.electricity_standing_charge') | int + 1.2059 }}"
       
- trigger:
     - platform: time
       at: "00:00:01"
  sensor:  
     - name: Gas Standing Charge
       unique_id: gas_standing_charge
       unit_of_measurement: 'kWh'
       device_class: energy
       state_class: total_increasing
       state: "{{ states('sensor.electricity_standing_charge') | int + 2.766 }}"

Just tried this sensor. For me, after the first trigger, I get status unavailable.

However, for your first sensor your actually adding 1 every day. By converting to int you strip off any fractional every day and adding back 1.205. So you must get 1.205, 2.205, 3.205, … see here.

To ensure your addition can work you should change | int to | float, see here, to convert to a float. This should not strip of your fractionals.

For the second sensor change this:

state: "{{ states('sensor.electricity_standing_charge') | int + 2.766 }}"

to:

state: "{{ states('sensor.gas_standing_charge') | float + 2.766 }}"

Should be better :slight_smile:

Your standing charge isn’t an integer. It is a floating point number (it has a part after the decimal point). Try this:

- trigger:
     - platform: time
       at: "00:00:01"
  sensor:  
     - name: Electricity Standing Charge
       unique_id: electricity_standing_charge
       unit_of_measurement: 'kWh'
       device_class: energy
       state_class: total_increasing
       state: "{{ states('sensor.electricity_standing_charge') | float(0) + 1.2059 }}"

thank you!

thank you! Appreciated

Hi Mark, I’m new to home assistant and don’t understand the YAML side just yet, could you please explain the steps you took to create this standing charge sensor that I can add to the energy dashboard.

Thanks

Hi Daniel

So the corrected YAML after changing to a floating point instead of integer as above is:

- trigger:
     - platform: time
       at: "00:00:01"
  sensor:  
     - name: Electricity Standing Charge
       unique_id: electricity_standing_charge
       unit_of_measurement: 'kWh'
       device_class: energy
       state_class: total_increasing
       state: "{{ states('sensor.electricity_standing_charge') | float(0) + 1.2059 }}"
       
- trigger:
     - platform: time
       at: "00:00:01"
  sensor:  
     - name: Gas Standing Charge
       unique_id: gas_standing_charge
       unit_of_measurement: 'kWh'
       device_class: energy
       state_class: total_increasing
       state: "{{ states('sensor.gas_standing_charge') | float(0) + 2.766 }}"

This code is in my template.yaml file. I also noticed for my gas one i was using the electricity unit cost this entire time haha so also changed that!

The trigger part tells it to run at a time which is set to “00:00:01”

The sensor is device_class energy with measurement set to kWh so that it will be usable in the Energy dashboard. The state_class also needs to be total_increasing.

The code then takes the standing charge sensor (itself) and adds a kwH value to it. This value is determined by the unit cost of your energy vs your standing charge cost. i.e. how many units of electricity costs the same as the standing charge. i.e. as above, 1.2059 x my unit rate ends up being the cost of my electricity standing charge.

I do the same for another sensor for gas and then add both to the energy dashboard.

So at 00:00:01 each night both sensors add the desired KwH usage that works out to be the cost of the standing charges.

The unit is already known to the energy dashboard from when you add your main energy input.

Does that make ANY sense? Took me a moment to remember how i’d done it!

1 Like

Hi Mark

Thanks so much for this, I’m out now but will give it a go tomorrow.

So just to check is the only yaml file that needs editing the template.Yaml, I don’t need to create any other files before adding it to the energy dashboard with the unit cost?

Thanks

No problem! Any issues just post angain and I’ll try to help.

So long as you’re already referencing the template.yaml in your configuration.yaml file for templates via

“template: !include template.yaml”

Basically tells it to look in the template.yaml file for templates.

I’m aware the sensors/templates configuration methodology has changed over the last year and to be honest I’m a little uncertain if I’m following old or new rules or a mix haha but this works so!

1 Like

Hi Mark

There’s something in not doing quite right, and it’s probably obvious but I’m only just getting use to yaml.

My elec unit rate is 34.28p per kWh, this is already gathered using a Shelly EM

My elec standing charge is 44.41p per day and I’ve added this in the Grid Consumption section of energy as shown in below photo. I’m unsure what the 1.2059 is in your code though. Should this be 1.3428 for me?

When I save the Electricity Standing Charge grid consumption I end up with the statistics_not_defined and Entity unavailable error as shown in the image below.




Firstly I’d imagine the entity doesn’t exist error will be if you haven’t reloaded your config files / done a restart so that they get created :).

Secondly you may have actually found a simpler way to do things. With my method I have the unit rate set as the actual unit rate for electricity. If you just set the unit rate for that sensor as the value of your standing charge you can change the code to just be +1 daily and you’d get your standing charge value sorted!

With my current method, you’d want to make 44.41p which is roughly 1.2955 * 34.28 (your unit rate).

However as above, If you set the unit rate for that sensor as the standing charge amount you can just use +1 for the same result :slight_smile:

1 Like

Hi Mark

Thanks for the updated info, it has now appeared on my energy dashboard but I have a few issues.

  1. It adds a 1kWh spike to my daily use (see screenshot), is there any way to avoid this by adding a correction which removes the 1kWh spike?

  2. It added the standing charge an hour early at 11

  3. Is there any way to have the unit as 4 decimal places like my actual charge 0.4441 rather than 0.44?

Thanks

It will add the spike sadly as that’s how I’m achieving adding the standing charge. For my own use I’m happy with the standing charge being added as a unit of energy used.

Not sure on the 11pm part though as long as it’s daily you’re okay I’d say?

For more decimal places just change the sensors value that it uses for how many KWH it adds