Is there a better way to adjust a (scrape) sensor manually?

I’m reading my electricity meter data every 15 min via scrape method and updating the sensors I created.
So far no issues.

With the sensor below I’m calculating the summery of the electricity as of June 1st '20 (the day my new contract started) till now. I’m adding 1264 (kWh) so my total consumption sensor starts from 0 (Zero) (June 1st '20).

  - platform: template
    sensors:
      osc_total:
        friendly_name: OSC Total
        icon_template: "mdi:thermometer"
        unit_of_measurement: "kWh"
        value_template: "{{ (state_attr('sensor.osc_scraper', 'Total (delivery – return)') |int +1264) }}"     # Adjusted (+1264) to real consumption as from June 13th '20.
        attribute_templates:
          updated: >
            {{ as_timestamp(states.sensor.osc_scraper.last_updated) | timestamp_custom('%Y-%m-%d %H:%M', true) }}

I do have 2 questions:

  • Sometimes the site, I get the data from, can’t be reached which means that sensor is updated with 1264, is there a way to avoid this by doing a check and add previous number for that period?
    afbeelding

  • From July 1st '21 my new contract starts, is there perhaps a better way to ‘adjust’ my sensor data so my total consumption sensor starts from 0 (Zero) again?

Perhaps I haven’t been very clear, so if you have additional questions let me know. :wink:

Thanks for your help!