Hi,
I’ve tried hard to dynamically configure the list of ntp servers for a sntp time component - but without success.
I want to let the user configure the 3 possible ntp servers via a simple web form (realized with the ESPAsyncWebServer - not with the standard web_server component, to save resources).
For this I’ve tried to define a global with the three ntp servers (the global will be set from the ESPAsyncWebServer submitted html form - this was the easy part).
But I found no way to feed the values from the global component into the servers variable of the sntp timer component:
This is the simple setup with constants (and also with global substitutions)
time:
- platform: sntp
id: sntp_time
timezone: America/Phoenix
servers:
- 0.us.pool.ntp.org
- 1.us.pool.ntp.org
- 2.us.pool.ntp.org
But as soon as I try to use a lambda to fill the list of servers from the global, it fails:
I’ve tried:
globals:
- id: global_ntp_servers # time servers
type: std::string[]
restore_value: true # persistent
initial_value: '{"0.us.pool.ntp.org", "1.us.pool.ntp.org", "2.us.pool.ntp.org"}'
but this results in compiler errors.
And this:
globals:
- id: global_ntp_servers # time servers
type: std::array<std::string, 3>
restore_value: true # persistent
initial_value: '{"0.us.pool.ntp.org", "1.us.pool.ntp.org", "2.us.pool.ntp.org"}'
but again compiler errors.
Using type
std::vector<std::string>
in the global also doesn’t work.
I’ve done a different test with just one server - just a string.
globals:
- id: global_ntp_servers # time servers
type: std::string
restore_value: true # persistent
initial_value: '"0.us.pool.ntp.org"'
this global definition is ok (as expected).
But when trying to use it to feed the sntp servers variable, I fail again.
time:
- platform: sntp
id: sntp_time
timezone: America/Phoenix
servers:
- lambda: 'return id(global_ntp_servers);'
with the error:
string value cannot be dictionary or list.
When I try to convert this string to a list, it fails also.
time:
- platform: sntp
id: sntp_time
timezone: America/Phoenix
servers:
- lambda: |-
std::list<std::string> stringList = {id(global_ntp_servers)};
return stringList;
I’m a bit lost and have two questions:
- How do I define an array (or list or vector) of strings in a global?
- How can I feed values defined in a global to a component variable in general - and in my case a list of strings?
Thanks in advance for your help