Custom sensor, read pv_energy today only

Hey,

I have created a custom sensor to estimate the savings that solar panels generate.
It’s just a sensor that tracks and converts the kwh from the solar panels to GBP to provide a daily value.

  - sensor:
      - name: "Todays Solar Value"
        unit_of_measurement: "£"
        state: >
          {% set today_generated_kwh = states('sensor.pv_energy') | float %}
          {% set import_rate = 0.2305 | float %}
          {% set savings = (today_generated_kwh * import_rate) %}
          {{ savings | round(2) }}

The “sensor.pv_energy” is a 24 hour value which means it has data from the previous day.

Does anyone know how I can make the below “today_generated_kwh” have only have todays value?

I tried the below but didnt have any success.

{% set today_generated_kwh = state_attr('sensor.pv_energy', 'today_energy') | float %}

I used chatgpt and it recommended setting up a sensor via utility_meter.

utility_meter:
  pv_energy_daily:
    source: sensor.pv_energy
    cycle: daily
    always_available: true

template:
  - sensor:
      - name: "Daily Solar Value"
        unit_of_measurement: "£"
        device_class: "monetary"
        state_class: "total"
        state: >
          {% set generated_kwh = states('sensor.pv_energy_daily') | float %}
          {% set import_rate = 0.2305 | float %}
          {% set savings = (generated_kwh * import_rate) %}
          {{ savings | round(2) }}

It seems to have started tracking the live data into a new sensor “sensor.pv_energy_daily” but it isn’t using the available data in “sensor.pv_energy” to generate any historic data.

I’ve set these up for weekly, monthly and yearly tracking.

It’s a solution but not ideal because I loose all my historic data.