Utility meter helper kwh

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

Syntax error with this - what are wrong?

{{ ‘Charging’ if is_state(‘input_boolean.charging_status’, ‘on’ AND state_attr(‘device_tracker.tmxxxxxxxxxxxxx_position’, ‘home’) elseif ‘Extern Charging’ state(‘input_boolean.charging_status’, ‘on’ AND state_attr(‘device_tracker.tmxxxxxxxxxxxxx_position’, ‘not_home’) else ‘Not Charging’ }}

Please help, Tanks

`

`

Haven’t tested anything but looks like a missing close bracket before each AND?

And elif not elseif

Ignore previous two comments. This from the post referenced earlier works fine in the template editor

{% if is_state('binary_sensor.dave_s22_wifi_state','on') and is_state('sensor.dave_s22_wifi_state','MyHomeWIFIName') %}
on
{% elif is_state('binary_sensor.brian_pixel6_wifi_state','on') and is_state('sensor.brian_pixel6_wifi_connection','MyHomeWIFIName') %}
on
{% else %}
off
{% endif %}

Change the sensor names and on and offs to Extern Charging etc and it should work!
(famous last words)

I had/have no idea what the syntax would be to do it in one line - template editor is a great place to try things out but I couldn’t get anywhere with one line approach.

Good luck

This works fine for me :wink: Thanks for helping.


data:
option: >-
{% if is_state(‘input_boolean.charging_status’,‘on’) and
is_state(‘device_tracker.xxxxxxxxxxx_position’, ‘home’) %}
Charging {% elif is_state(‘input_boolean.charging_status’,‘on’) and
is_state(‘device_tracker.xxxxxxxxxxx_position’, ‘not_home’) %}
Extern Charging {% else %} Not Charging {% endif %}