However, my internet connection can be spotty sometimes and my home can be offline for a few minutes at night when my ISP gives me a new IP address.
So, how do I properly handle being offline with a REST sensor? What I want to happen is that the REST sensor doesn’t switch to unavailable and just keeps its old value and attributes while offline.
And if I don’t do this here and the source values, how do I properly handle this with other template sensors using the REST sensor when the latter turns temporarily unavailable?
In my case, several template sensors use the REST sensor result to calculate a schedule when to turn various devices on or off. One night while the REST sensor became unavailable for 15 minutes, the schedule was discarded and the heatpump was turned off because of it, instead of the heatpump just staying on its pre-calculated schedule.
create an automation that will trigger the update using the action homeassistant.update_entity documented here. You would want to only call that service when the resource is available, so perhaps using the ping integration and only running the action if the ping sensor is on. You can trigger a manual update of the ping sensor immediately before you manually update the REST sensor.
Create a new sensor that stores the last “good” value of the REST sensor
Input helpers and trigger-based template sensors are the only native entities in HA that can be used to store persistent data that survives restarts. Of those, trigger-based template sensors are the only ones that allow you to define attributes. So, you can create a trigger-based template sensor that updates whenever your REST sensor updates but then ignores the unavailable state. You would have to create that in YAML. All your other template sensors would then refer to this sensor instead of the REST sensor.
Alternatively, if you’re not worried about the failure mode of starting up HA while the template sensor is unavailable, you can create a normal template sensor, but it will still have to be done in YAML in order to define attributes.
In either case, you’d test for the state to be unavailable. Something like this (untested):