Unfortunately, this is not easily done in a single script.
You’ll need an MQTT Sensor to get the last value of /home/tempsensor
And since scripts can’t do “else if” or “else”, you’ll need one script to start it off, and additional scripts with conditions to achieve these goals.
A far easier way to accomplish this is with a python_script or with AppDaemon. For either of those, you’ll need to learn a little python. NodeRed is another popular option (that I don’t care for myself). So you may want to look into that as well.