I must say i love your passion on this topic and I love a good challenge to see if I’ve covered my bases
I think the misunderstanding here is that I believe you think I’m comparing the difference between total_cleaned_area
(from_state) and total_cleaned_area
(to_state) but I’m not.
What I’m comparing is the number of 50m^{2} that can fit into total_cleaned_area
(from_state) vs into total_cleaned_area
(to_state). If the latter is bigger than the former it means the robot must have passed a multiple of 50m^{2}, i.e. it’s gone from 6062 to 6101 (or from 6099 to 6101) but not when going from 6062 to 6099. Speaking in mathematical terms it’s all about the integer division.
So, to use your example and 40m^{2} as the “limit”, this is actually what happens:

total_cleaned_area starts with 6062. 6062/40 = 151,55 or simply 151 with integer division. You could see that as the number of how many 40m^{2} runs have been made since first setup.
 I run room cleanup which is only 30m^{2}.
 Once cleanup is done and the vacuum returns to charging, the to_state’s total_cleaned_area will be 6092 (6062+30) and the integer division will be 6092/40=152,3 → 152.

(trigger.to_state.attributes.total_cleaned_area / 40)int
= 152

(trigger.from_state.attributes.total_cleaned_area / 40)int
= 151
 152 > 151 → Automation is triggered
 I run another cleanup of 30m^{2},
total_cleaned_area
(from_state) is now 6092 (and that integer divided by 40 is 152)
 When cleanup completes,
total_cleaned_area
(to_state) is now 6122 which will trigger the automation because 153*40=6120 and 153>152
 If this last cleanup would have been only 20m^{2} then the
total_area_cleaned
would have ended up as 6112 and then no automation would have been triggered because 6112 divided by 40 equals the same as 6092/40 for integer division.
Basically what I’ve done is that whenever the vacuum surpasses 0, 40, 80 … 6040, 6080, 6120… m^{2} total area cleaned, the automation will trigger.
The feature here is that the from_state.attributes.total_cleaned_area
is from the start of the cleanup run when the automation trigger is “when state goes to returning”
And this is how I’m using it:
Now it only says
Finished cleaning, returning to base.. For every 50m
^{2} cleaned it will also add “
Check dustbin” to the message and for every 100m
^{2} cleaned it says “
Check dustbin and filter” instead
All this because there’s no good notifications and sensors on Xiaomi robot vacuums But I do love my Robocop and Home Automation