You really need to format the paste when you paste code or people have trouble helping you…
But the code, from what I can tell, looks fine but have you checked that sensor and can you see it has actually dropped below 20? I am not asking about the device battery itself. I am talking about the sensor for the battery in home assistant… You can see that in the history.
On a side note, I doubt you’re causing any issues by just leaving it in. Tablets stop charging when they are full and will just top it off every now and then. By turning that switch on/off, you’re actually going to hurt the battery more by cycling it than trickle charging it.
After researching the best way to reserve the battery I found that the best way is to change the routine, putting the tablet on charger 24/7 is ok but it shorten the battery life, power cycling is also ok but it should not happen always.
So I scheduled the automation to run on selected weekdays only, the rest of the days the charger is on.