You can find the size of a value stored in ESP32 NVS by running nvs_get_blob()
with length
set to a pointer to a variable with content 0
, and out_value
set to nullptr
. nvs_get_str()
works as well, the only difference is the type of the out_value
argument, which does not matter at all.
It’s easier to understand if you look at this example function:
size_t NVSGetSize(nvs_handle_t nvs, const char* key) { esp_err_t err; size_t valueSize = 0; if((err = nvs_get_str(nvs, key, nullptr, &valueSize)) != ESP_OK) { if(err == ESP_ERR_NVS_NOT_FOUND) { // Not found, no error return 0; } else { // Actual error, log & return 0 printf("Failed to get size of NVS key %s: %s\r\n", key, esp_err_to_name(err)); return 0; } } return valueSize; }
Usage example
This assumes that you have setup myNvs
as we have shown in our previous post How to initialize NVS on ESP32
size_t myKeySize = NVSGetSize(myNvs, "MyKey");