What Filter to smooth curve?

This is the outside brightness. I would like to apply a filter such that the new curve is oriented at the maximums, so that it is the outer envelope. The envelope is built by partitioning data into intervals and finding a maximum point in each such interval. Is there a custom filter doing that?

Example how it could look:

Now I did this: I store the last 4 values and take the maximum of the last 4 values and the current value. (the value is calculated first, then the attributes!). But 4 values are not enough.

         entity_id: sensor.lumi_lumi_sen_ill_mgl01_74b4783c_illuminance
         unit_of_measurement: "lx"
           l4: '{{ state_attr("sensor.helligkeit3","l3") }}'
           l3: '{{ state_attr("sensor.helligkeit3","l2") }}'
           l2: '{{ state_attr("sensor.helligkeit3","l1") }}'
           l1: '{{ states("sensor.lumi_lumi_sen_ill_mgl01_74b4783c_illuminance") }}'
         value_template: >-
           {{ (state_attr("sensor.helligkeit3","l1")|float,state_attr("sensor.helligkeit3","l2")|float,state_attr("sensor.helligkeit3","l3")|float,state_attr("sensor.helligkeit3","l4")|float,states("sensor.lumi_lumi_sen_ill_mgl01_74b4783c_illuminance")|float)|max }}

Take a look at the statistics sensor platform: https://www.home-assistant.io/integrations/statistics/