Add Number in Template Sensor

I have the following Template Sensor to calculate my Inverter Production.

  template:
   - sensor:
       - name: "Inverter_Production"
         unique_id: "inverter_net_prod_test"
         unit_of_measurement: "kWh"
         device_class: energy
         state_class: total_increasing
         state: >
           {{ states ('sensor.sdm220m_3_import_active_energy') | float(2) | round(2)
           - states ('sensor.sdm220m_2_import_active_energy') | float(2) | round(2)}}
         availability: >
           {{ states ('sensor.sdm220m_3_import_active_energy') | float(none) != none and
               states ('sensor.sdm220m_2_import_active_energy') | float(none) != none }}

It is no longer working as it now returns a negative value as smd220_3 has rolled over at 100,000

Result
template:
  - sensor:
      - name: "Inverter_Production"
        unique_id: "inverter_net_prod_test"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          -68165.73999999999
        availability: >
          True

I have added 100,000 to the value in the Template Editor Which has it returning the correct value, but this will become invalid when the other meter rolls over.

        state: >
          {{ states ('sensor.sdm220m_3_import_active_energy') | float(2) | round(2)
          - states ('sensor.sdm220m_2_import_active_energy' ) | float(2) | round(2) + 100000}}

Can anyone suggest a better approach for this method, given that both meter s will eventually roll over each time they hit 100,000.

Thanks.

Feed sensor.sdm220m_3_import_active_energy to a utility meter without a cycle option defined. It will keep counting up and ignore the resets to 0.

Use the sensor from the utility meter in your template.

If you like you can simplify your availability template too.

availability: >
           {{ has_value('sensor.sdm220m_3_import_active_energy') | float(none) and
               has_value('sensor.sdm220m_2_import_active_energy') }}

Note, do not put spaces between states or has_value and the following brackets. Do this states(...) not this states (...)

Thanks Tom,

Similar to this?
“Sensor Always Available” Option??
Would that also help to cover the occasions when the Modbus doesn’t read.
Would also need two as I have one of these meters on either side of my inverter.
Mains in & load out.
If there’s not solar the mains input takes over.

Also have one on my time controlled load that can only be read when the controlled load is turned on.
That option could also help in reading that one.