The *online* message sent to the availability topic needs to have the retained flag set. Without it, when Home Assistant restarts, it will receive the retained *offline* message and mark the entity as unavailable.