How to calculate by month and year, the autonomy of my house on solar?

Hello

I created a binary sensor that switches from true to false depending on the position of my house’s electrical energy supply (true: solar, false: enedis).

I would like to be able to make a count per hour of the position of this sensor to be able to calculate per month and over the year my percentage of autonomy.

I am with a hybrid inverter and a 14 kWh LifePo4 battery

Thanks for your insights

Edit :
To go further, it would be more interesting to be able to calculate conditional time.

Like, counting the consumption of the house while the sensor is on true and the same thing on false. Then do the calculation: kwh sensor true / (kwh sensor false+kwh sensor true) / 100

And there we would have the percentage of autonomy

Nobody can help me on this problem ?

Hey, maybe I have a little input for you. Hopefully this will help you in some way.
However only on the second part of the question.

In order to count the power consumption there is probably many ways. The way I am doing it is as follows ( example config at the end).

Sensor P1,P2,P3 - shows the current power in W for each of the 3 phases.
Sensor S - Shows the state on/off depending on the heating (if it is on/off)

First I create a template that is P1+P2+P3 only is S is on.

Based off that I have an integration sensor that counts the energy in kWh.

And then as utility meters I have sensors that reset daily/monthly/yearly and count the energy for that interval.

See below my configs. Note however that currently I have some problems with these ( see my topic on that Integration Templates and Utility Templates suddenly "Unavailable" )

This is the power template


- platform: template
    sensors:
      shelly_power_water:
        friendly_name: "Shelly Power Consumption Water"
        unit_of_measurement: 'W'
        value_template: >-
          {% if ( ( (states('sensor.shelly3em_channel_a_power')|float - states('sensor.shelly3em_channel_b_power')|float + states('sensor.shelly3em_channel_c_power')|float) > 0 ) and ( is_state('sensor.wkh_status_water', 'on') ) ) %}
            {{ states('sensor.shelly3em_channel_a_power')|float - states('sensor.shelly3em_channel_b_power')|float + states('sensor.shelly3em_channel_c_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{ 
            [ states('sensor.shelly3em_channel_a_power'),
              states('sensor.shelly3em_channel_b_power'),
              states('sensor.shelly3em_channel_c_power')
            ] | map('is_number') | min 
          }}"

This is the integration (energy counter)


sensor:
  - platform: integration
    source: sensor.shelly_power_water
    name: shelly_energy_water
    round: 2
    unit_prefix: k
    method: left

And these the utility-meters


utility_meter:
  shelly_energy_water_daily:
    source: sensor.shelly_energy_water
    cycle: daily
    
  shelly_energy_water_monthly:
    source: sensor.shelly_energy_water
    cycle: monthly

  shelly_energy_water_yearly:
    source: sensor.shelly_energy_water
    cycle: yearly

Good morning

Thank you for your feedback which brings me new light.

after careful consideration and in order to calculate my percentage of electrical autonomy, I must compare:
electricity consumption of the house if autonomous mode is ON
and total electricity consumption of the house over the same period.

So I created a utility_meter:
total household daily, monthly and annual consumption

utility_meter:
  total_household:
   source: sensor.house_consumption
  cycle: daily
  total_household:
   source: sensor.house_consumption
  cycle: monthly
  total_household:
   source: sensor.house_consumption
  cycle: yearly

Thanks to your method, I created a sensor that calculates the consumption of the house in off grid mode and then created a utility_meter for it daily, monthly and annually.

I now want to create a template_sensor that would allow me to compare:
sensor.offgrid_consumption_daily*100 / sensor.total_consumption_daily

    - name: "electric_autonomy"
      unique_id: Electric_Autonomy
      state: >
        {{ [ states('sensor.offgrid_consumption_daily') * 100 / states('sensor.total_consumption_daily) ]
             | map('float') }}
      availability: >
        {{ not 'unavailable' in 
           [ states('sensor.offgrid_consumption_daily'), 
             states('sensor.total_consumption_daily') ] }}

Which gives me a percentage of autonomy over the day

can someone correct me if he sees any programming errors?

Thank you