Calculate the prize to charge my car

I would like my HA instance to show how much money I have spent charging my car during a month.

My electricity price changes every hour on the hour, so I have to take into consideration for how long the car have been charging and at what time of the day.

I have created this calculation:

{{ (state_attr(‘sensor.energi_data_service’, ‘current_price’)/60)*11 }}

that shows the current price for every minute that i charge 11 kW.

I would like to have an entity that shows the total price for the current month, so that the value is increased every minute with the value of the above. The value should only be increased when my current powerdraw is above 11 kW.

Currently every minute costs 0,678 kr, so I would like to add this value to the counter every minute but every hour the value changes.

How do I do this?

Can I store the values for each month?

Have you considered using a utility meter?

This would allow you to create a daily, weekly, monthly and yearly meter to track your use.

I have considered that, but I don’t know how to make it count every minute and use a price that changes avery minute

You could create a sensor to store the price. This assumes you can get the price in real time.

template:
  - trigger:
      - platform: time_pattern
        hours: "/1"
  sensor
    - name: "Current Electricity Price"
      unique_id: "Current Electricity Price"
      state: >-
        {% if 0 <=now().hour  < 1 %}
          {{ sensor.cep}}
        {% elif  1 <= now().hour < 2 %}
          {{ sensor.cep }}
        {% elif  2 <= now().hour < 3 %}
          {{ sensor.cep }}
     .......  Repeat for all 24 hours
        {% else %}
          {{ sensor.cep}}
        {% endif %}

The price will be updated every hour on the hour and you can use this (sensor.current_energy_price) price in the calculation.

Thank you for your reply.

If I insert this into my configuration.yaml file I will get a syntax error.

I am currently thinking of creating some kind of script that runs whenever the car is charging:

I have a “sensor.elmaler_power” with an attribute called “power”
I have a “sensor,clever_minut_pris” which shows the price if I charge the car for one minute. This sensor value is updated every hour.

When this is true:

{% if state_attr(“sensor.elmaler_power”, “power”) > 10500 -%}

I would like the script to wait for 1 minute and then take the value of “sensor,clever_minut_pris” and add it to a new variable. This should be repeated once every minute until

{% if state_attr(“sensor.elmaler_power”, “power”) < 10500 -%}

At the end of every month I would like to reset the value to 0

Why don’t you use the Energy dashboard?
You can add the powermeter of the charger and assign the rates to that sensor.

1 Like

I don’t have a separate powermeter in front of the car charger. I only have a powermeter for the whole house, but I want to only calculate the price for the amount of power that goes to the car.

I’m just curious. You’ve got an entire home that is consuming power and you want figure out how much is going to that car from a device that only counts the total usage? I wonder how you’ll overcome all the variables that come with calculating your usage, take into account that no day will be the same.

I would look into Power Monitoring With Sonoff TH and ADC121 - Tinkerman or something like this.

I am having trouble finding a 3 phase powermeter that runs via Zigbee and that support 3*16A continuesly.

I know that when the car is charging it is always pulling 11 kW. If for example I connect the car at 14.40 today and it charges until 18.00, it will charge 20 minutes at 1 rate and at 15.00 the rate will change.

Today I have to do the calculations manually using Excel and the price chart, but I was wondering if I could get HA to do the calculations for me.

I’m still trying to grasp what you are trying to accomplish.

You’ve got a calculation that will show you the price of charging per minute, you say that your charger has a fixed consumption of 11kW (which I doubt because to my knowledge most chargers will lower their consumption if the battery is almost full).

If you assume that if power consumption is above 11kW, then the car is charging at a rate of 11kW/hr. This would be my interpretation of your idea. I’ve borrowed some of the idea’s I’ve posted in Using power consumption to show information (status, elapsed time, count) of cycles on a dish washer - #2 by hebom

Let’s create a binary sensor that determines if the carcharger is in operation:

- platform: template
  sensors:
    carcharger_status:
      friendly_name: "Carcharger status"
      delay_on:
        minutes: 0
      delay_off:
        minutes: 0
      value_template: >-
        {{ states('sensor.powermonitor') | float(0) > 11 }}

sensor.powermonitor is the sensor that monitors your overall power consumption.
The 11 is the 11kW that the carcharger consumes.
You can add delays if you want to, I’ve set them to 0 because of the fixed 11kW rating.

Let’s create some (regular) sensors:
Now that we know when your car is charging it’s time to determine the running time (in minutes).

- platform: template
  sensors:
    carcharger_runtime:
      friendly_name: "Carcharger runtime"
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if states('binary_sensor.carcharger_status') == 'off' else now().timestamp() - states.binary_sensor.carcharger_status.last_changed.timestamp() %}
        {{ ( t / 60 ) | round(0) }}

We know now how long the car is charging, so we can calculate a tariff based on the duration of the charging:

- platform: template
  sensors:
    carcharger_spending:
	  friendly_name: "Carcharging spending"
	  value template:
	    {% set a = binary_sensor.carcharger_runtime %}
		{% set b = ((sensor.energytariff() * 11) / 60) %}
		{{ ( a * b ) | round(2) }}

You would like to know the “damage” per day, week and month:

utility_meter:
  carcharger_daily_total:
    source: sensor.carcharger_spending
    name: Daily carcharger spending
    cycle: daily

  carcharger_weekly_spending:
    source: sensor.carcharger_spending
    name: Weekly carcharger spending
    cycle: weekly
	
  carcharger_monthly_spending:
    source: sensor.carcharger_spending
    name: Monthly carcharger spending
    cycle: monthly

I hope this works, but it might need some tinkering.

1 Like

@lundmilo Was my post of any help?

Your car probably isn’t consuming a constant power during the whole charging process, it probably have a curve (or even more complex shape) based on the battery level (at least).

You probably can measure this consumption on the different battery levels (maybe in a friend’s home or a public charger where you can measure the power consumption) and then use something like Powercalc with the precision calibration, then you will have a quite good sensor updating your power consumption each minute (as soon you have a way to detect you car is charging and it’s battery level).
With that, add this Powercalc sensor to your energy dashboard with the electricity price and you will have what you need with a reasonable accuracy.

Thank you. I will run some tests, but it looks like the right direction. Currently I have managed to have HA export my tarrif every minute into a CSV filewhich I can then import to Excel to do the calculations.

I would still like to have HA show the cost on the dashboard.

It actually looks like it is charging at full 11 kW all the way to 100%.

It starts at 3:11 and stops exactly at 7:05 where it drops down to 5xx W. The extra comsupmtion on top is the washer running.

So i think it is close enough to asume that it comsumes 11 kW whenever it is charging.

Regards,

1 Like

So you can still use PowerCalc with 11kW for when it is charging, then you add this to the Energy dashboard together with your electricity price sensor and the cost per day, month, etc. will be calculated automatically, with no need of exporting files to Excel.