Increase sensor value based on other sensors

I am trying to create a new sensor that can track the amount of kwh I charged the battery with.

Currently I have one value coming from the inverter which says how many kwh went into/out of of the battery but no way to tell how much came from PV and how much from night time charging.

I would need two new entities, battery_grid_charged and battery_pv_charged

I do have all the values I believe I just need to be able to find a way to create the entity (either sensor or utility_meter)



The logic is simple enough:
if( sensor.grid_status = Importing and sensor.solar_battery_status = Charge)
add sensor.solar_battery_power to battery_grid_charged
else
add sensor.solar_battery_power to battery_pv_charged

My question is how to create the battery_grid_charged and battery_pv_charged entities and increment them based on the logic above

We can do this but it needs to be done with an energy sensor not a power sensor. Please provide your battery charging energy sensor entity_id.

This one?

Yes. First create a utility meter helper with two tariffs that uses that battery charge sensor:

utility_meter:
  daily_battery_charge:
    source: sensor.solar_battery_charged_today
    name: Daily Battery Charge
    cycle: daily
    tariffs:
      - from_grid
      - from_solar

Then automate changing the tariff based on your logic in the first post:

trigger:
  - platform: state
    entity_id: sensor.grid_status
    to: # null 'to' triggers on any state change
  - platform: state
    entity_id: sensor.solar_battery_status
    to: 'Charge'
condition:
  - condition: state
    entity_id: sensor.solar_battery_status
    state: 'Charge'
action:
  - service: select.select_option
    target:
      entity_id: select.daily_battery_charge
    data:
      option: "{{ 'from_grid' if is_state('sensor.grid_status', 'Importing') else 'from_solar' }}"

This will create two daily energy sensors that track your battery charge:

sensor.sensor.solar_battery_charged_today_from_grid
sensor.sensor.solar_battery_charged_today_from_solar
1 Like

Great I can try this once I’m home.
And if I want the same but for monthly, yearly values is it just a matter of creating two new utility_meters and change cycle: daily to monthly and yearly?

Correct, and you can change all the tariffs at once:

action:
  - service: select.select_option
    target:
      entity_id: 
        - select.daily_battery_charge
        - select.monthly_battery_charge
        - select.yearly_battery_charge
    data:
      option: "{{ 'from_grid' if is_state('sensor.grid_status', 'Importing') else 'from_solar' }}"
2 Likes

Ok it is working but not correctly, i.e. there has been no grid charge yet only a bit of solar this morning


image
I think my problem is this:
image
It toggles a lot between importing/exporting depending when the value refreshes so it’s not a great one to use.
Might be better to go with if solar_gen > 0 instead option: "{{ 'from_solar' if states('sensor.current_solar_generation') | int > 0 else 'from_grid' }}"