Trains from Madrid to Valencia go through Cuenca, a hill town with houses hanging over the edge of a ravine. The historic center is very atmospheric. When I visited Cuenca in 2016, there were two small modern-art museums I really enjoyed. I think there's a parador in an historic building (maybe a former convent?). I didn't stay there, but I bet it's nice.
There are both fast and slow trains on the Madrid-Valencia line. You will certainly want to take a fast train for both segments of the tip, because the slow trains are much slower indeed. The fast trains will be spendy if you don't buy the tickets early. And the station used by the fast trains is far enough outside Cuenca that you'll need to take either a bus or a taxi into town. I don't know whether the buses from the airport go up to the hilltop historic center (which is otherwise a non-trivial walk) or how often they run. I have a suspicion they run less frequently on weekends/Sundays but haven't researched their schedules.
Unfortunately, all the towns I could think of along or near the Madrid-Valencia access will probably be hotter than Valencia in July/August. That's not the part of Spain I'd choose to visit at that time of year.
Valencia weather -- July 2024 -- (July 2025 wasn't as bad)
You can play around with that website, using the pull-down box at the right, just above the graph, to change the month and year displayed. Use the Search box to change the location.