I would like to store 1460 integer constants in an array and perform a lookup of four values each day of the year using esphome yaml code. The application is to run on an ESP32 without access to the internet or homeassistant. It runs an esphome webserver using an access point. I am not sure “globals” could handle this many integers to load on startup and they would use RAM which is not necessary for constants.
Is there a way to create an external_component that includes the array?
Or perhaps use an external eeprom? Not sure how to program it with that many values.
Have you just tried creating a global of int[1460]
? This would be less than 6kb. You didn’t mention what chip you’re using, but I can’t imagine anything that runs esphome having a problem allocating such a tiny bit of memory.
Just to put that in context, the average English word (including an allocation for associated spaces and punctuation) is about 8.3 bytes, and an average-ish paragraph might be about 200 words. (Both stats according to a quick Google search.) So about 3.5 paragraphs of English text would take up about the same amount of memory as 1460 4-byte integers (the size of the normal C++ implementation of int
).
A ESP32-S3 N16R8 should easily handle the array.
There are a lot of statements to initialize like this:
on_boot:
priority: -10
then:
- lambda: |-
id(my_array)[0] = 1;
id(my_array)[1] = 2;
id(my_array)[2] = 3;
id(my_array)[3] = 4;
……
Is there a better way to do this?
Can’t you set the initial values more like this?
This is a great solution. I don’t need the on_boot section either
One improvement is to add an include statement to isolate the array values from the main yaml code.
- id: ARRAY_NAME
<<: !include .array_data.yaml