The good news is that this should be relatively straightforward.
I’m currently in the process of automating my spa/pool combo to let me heat the spa separately, so have looked into solving a very similar set of issues with reading temperatures and switching 3-way valves.
I don’t know about the Sonoff TH16, I am reading temperature via a Ubibot device, but with luck they will work similarly. The trick is to get readings into IFTTT, and send these to Apilio for storage–unfortunately while you can often see values in Apilio you cannot (at present) do much with them, but IFTTT does let you do that.
So, if you can get the temperature readings into IFTTT, then you can use IFTTT to write these values into two separate Apilio variables (solar_temp and pool_temp). You will probably need to poll IFTTT every so often to retrieve the temperatures (every few minutes should be enough), which I do by setting an Apilio time condition to trigger when required (use the CRON setting – for example, “*/4 * * * *” means trigger every 4 min, which is what I use). Then set up a logicblock in Apilio that is triggered by your time condition, and which tells IFTTT to take the temperatures and send them back to Apilio.
At this point you will have two variables in Apilio that are constantly being refreshed with the temperature readings. You can compare these using a numeric condition, so if solar_temp > pool_temp then turn on smart switch, else turn off smart switch.