Utility meter helper kwh

I am trying to create a kwh utility meter sensor based on the value of another sensor but I am not getting much luck with it working.
Ideally this new sensor will increase and then reset when a new month starts.
Created like this:

And the sensor that feeds it data:
image

The new sensor stays at 0 and I’m trying to track the kwh used per month.
However when it increases (I charge the car) it does go up:
image
I actually only want it to increase when the battery sensor decreases, how to set it like that?

By default the utility meter only measures increases.

Enabling Net Consumption will show increases and decreases.

There is no “decrease only” mode.

What you could do is enable net consumption and create two tariffs, charging and discharging.

You then get two sensors, one for the monthly charge amount and one for the monthly discharge amount.

You have to automate switching of the tariffs. See: https://www.home-assistant.io/integrations/utility_meter/#advanced-configuration

So I never want the helper utility meter to “decrease” only to go up by the amount of kwh the battery decreases by, if that makes sense.
Is there an easier template sensor to be created instead of this helper perhaps?

Yes, that’s what this will do. It will just have a minus sign in front of it. If you want to get rid of that you would need a template sensor as well.

Ok I know how to automate the tariff change but not sure on what value I should toggle between the two.

1 Like

Do you have a binary sensor that indicates if your battery is charging?

Yup, I have, with values Charging/Not Charging which is exactly what’s needed

1 Like

@tom_l thanks but I am still unsure how to deal with the kwh discharged sensor because I technically don’t have one and for the utility meter helper you can only provide one sensor not two.
I only have the battery left one which is kwh and increases and decreases.
I do have a monthly kwh sensor which comes directly from a shelly on the EV circuit which would give me an increasing value.
Or am I misunderstanding something here?

You can create a utility meter that works for both the charging and discharging, like @tom_l suggested.

Just create a utility meter with these settings:

  1. Give it a name you like (ex. utilitymetername)
  2. Enter the sensor of your battery
  3. Choose monthly
  4. Enter Charging and Not Charging
  5. Turn on Net consumption

And save!

HA creates three new sensors for you. One that shows you the amount the battery’s charges every month and one for discharging.

The third is a sensor to toggle whether you are charging or not. This is what Tom told you to automate. If the car isn’t charging, set it to not charging and visa versa. When set to not charging the utility meter sensor.utilitymetername_not_charging will show you the amount of discharge!

1 Like

Got it now.
So this:
image
Then set up an automation that toggles the tariff based on charging/not charging sensor and done?
And this makes sense?

alias: i4 Battery - Switch tariff
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.i4_m50_charging_status
  condition: []
  action:
  - service: select.select_option
    target:
      entity_id:
      - select.i4_kwh_used_this_month
    data:
      option: '{{ ''Charging'' if is_state(''binary_sensor.i4_m50_charging_status'', ''on'') else ''Not Charging''
        }}'
  mode: single

So when your car discharges, the not charging meter will run. When charging, the other meter runs. Just be patient and start (dis)charging :wink:

The only additional thing you need is a template sensor to make the not charging value positive:

template:
  - sensor:
      - name: Charge Used This Month
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: kWh
        state: "{{ states('sensor.i4_kwh_used_this_month_not_charging')|float(0)|abs }}"
        availability: "{{ states('sensor.i4_kwh_used_this_month_not_charging')|is_number }}"

I will add this now but is it normal that it went into the minus for a bit last night and then back to normal, without the template value?
image
The value should have totaled around 22

The utility meter “charging” sensor should always increase. The utility meter “not charging” sensor should always be decrease. However it took some time for you to get the tariff switching automation set up correctly, so that could account for the short negative value in the charging sensor.

You can use the Developer Tools → Services page to call the utility meter calibrate service if you need to tweak the current totals.

1 Like

Ok so if that’s just because of the initial setup it’s fine, as it will be correct from the start of the new month anyway.
Thanks again @tom_l, much appreciated :slight_smile:

Just keep an eye on the sensors and make sure that one keeps increasing and the other decreasing.

1 Like

Thanks on all for the perfect instructions.
How can i split the ‘used this month charging’ for charging when state ‘car postion’ is ‘not_home’.
That’s because I usually charge at home and I want to know how much I’m charging externally.

Thanks so much.

Three tariffs in the utility meter? Home charging, away charging, not charging.

And Modify your selection automation accordingly.

How do i modify the automation? I think the data option is requiered.
Please help. Thanks

You need some more logic in your ‘option’ line - I don’t know the answer and without your sensors to test I’d never get it right!

the outline is

if
home charging - charging is on AND car position is home
elseif
away charging - charging is on AND car position is not_home
else
not charging - charging is off (as it is now)

this post Logical AND in a template is helpful in showing how you can AND conditions together.

The template editor (Developer Tools - Template) is your friend to get the syntax correct.

Good luck