This is one approach…
You could also save the values to SPIFFS of LittleFS of you’re using and ESP MCU so that your data is protected against power failure.
Pete.