If you really want to see how its done, the TSDZ2 open source code would be a good start.
https://github.com/OpenSourceEBike/TSDZ2-Smart-EBike
I played with this code awhile back trying several things. Ultimately I abandoned the TSDZ2 due to it being a POS.
In a nutshell, this code determines...