Capture Power Usage between certain State and Time of day?

Eternally grateful if i could get some help on this template… Don’t even know how to begin this??

I want to create an entity that records my power consumption the moment my inverter turns off and its after sunset to the moment it turns on and its after sunrise??

Any ideas or ways other have tried to capture this information?

this is what I have with help but i dont think its correct, any help is apreciated

      friendly_name: Night Time Power Consumption W
      icon_template: mdi:meter-electric
      value_template: >-
        {% if ('off' in states.binary_sensor.above_horizon.state) and ('offline' in states.sensor.fronius_inverter_status.state) %}
        {{ (states.sensor.fronius_total_real_energy_consumed_kwh.state | float(0)) }}
        {% else %} 0.0 {% endif %}