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.