Using and initializing array variables in esphome

I can’t seem to figure out how to initiate and use array variables in esphomeyaml. I saw on the Automations and Templates page (linked below) that I can use array variables but I don’t know the initialization syntax. Since I haven’t successfully created one, I haven’t tried using the variable in lambda so tips there are also welcome.

Here’s an example of what I’m trying to do:

globals:
  - id: test_rgb
    type: int[3]
    initial_value: "[100,80,67]"

Any help appreciated.

Thanks,

-Josh

initial_value: "{100,80,67}" (see C++ arrays)

When I use the above syntax, I get an error and it doesn’t compile.

Error:

src/main.cpp: In function 'void setup()':
src/main.cpp:48:95: error: no matching function for call to 'esphomelib::Application::make_global_variable(<brace-enclosed initializer list>)'
GlobalVariableComponent<byte[3]> *test_rgb = App.make_global_variable<byte[3]>({100,100,100});
^
src/main.cpp:48:95: note: candidates are:
In file included from src/main.cpp:3:0:
.piolibdeps/esphomelib/src/esphomelib/application.h:1501:29: note: template<class T> esphomelib::GlobalVariableComponent<T>* esphomelib::Application::make_global_variable()
GlobalVariableComponent<T> *Application::make_global_variable() {
^
.piolibdeps/esphomelib/src/esphomelib/application.h:1501:29: note:   template argument deduction/substitution failed:
src/main.cpp:48:95: note:   candidate expects 0 arguments, 1 provided
GlobalVariableComponent<byte[3]> *test_rgb = App.make_global_variable<byte[3]>({100,100,100});
^
In file included from src/main.cpp:3:0:
.piolibdeps/esphomelib/src/esphomelib/application.h:1506:29: note: template<class T> esphomelib::GlobalVariableComponent<T>* esphomelib::Application::make_global_variable(T)
GlobalVariableComponent<T> *Application::make_global_variable(T initial_value) {
^
.piolibdeps/esphomelib/src/esphomelib/application.h:1506:29: note:   template argument deduction/substitution failed:
src/main.cpp:48:95: note:   cannot convert '{100, 100, 100}' (type '<brace-enclosed initializer list>') to type 'unsigned char*'
GlobalVariableComponent<byte[3]> *test_rgb = App.make_global_variable<byte[3]>({100,100,100});

Same errors. @OttoWinter

src\main.cpp:35:94: error: no matching function for call to 'esphome::Application::make_global_variable(<brace-enclosed initializer list>)'
GlobalVariableComponent<int[4]> *gpio14_relays = App.make_global_variable<int[4]>({1,2,3,4});
^
src\main.cpp:35:94: note: candidates are:
In file included from .piolibdeps\esphome-core\src/esphome.h:1:0,
from src\main.cpp:3:
.piolibdeps\esphome-core\src/esphome/application.h:337:31: note: template<class T> esphome::GlobalVariableComponent<T>* esphome::Application::make_global_variable()
GlobalVariableComponent<T> *make_global_variable();

config

globals:
  - id: gpio14_relays
    type: int[4]
    restore_value: no
    initial_value: "{1,2,3,4}"

Try single quotes instead:

globals:
  - id: gpio14_relays
    type: int[4]
    restore_value: no
    initial_value: '{1,2,3,4}'

It has been fixed in 1.12.0. Thanks

The below code should also work. But gives compile time error

globals:
  - id: gpio14_relays
    type: int[]
    restore_value: no
    initial_value: "{1,2}"

In file included from src/esphome.h:17:0,
from src/main.cpp:3:
src/esphome/components/globals/globals_component.h: In instantiation of ‘class esphome::globals::GlobalsComponent<int >’:
src/main.cpp:3279:63: required from here
src/esphome/components/globals/globals_component.h:47:12: error: ‘esphome::globals::GlobalsComponent::value_’ has incomplete type
T value_{};
^
src/esphome/components/globals/globals_component.h:47:12: error: invalid use of array with unspecified bounds
src/esphome/components/globals/globals_component.h:48:17: error: ‘esphome::globals::GlobalsComponent::prev_value_’ has incomplete type
T prev_value_{};
^
src/esphome/components/globals/globals_component.h:48:17: error: invalid use of array with unspecified bounds
src/main.cpp: In function ‘void setup()’:
src/main.cpp:3279:63: error: no matching function for call to ‘esphome::globals::GlobalsComponent<int >::GlobalsComponent()’
my_global_int = new globals::GlobalsComponent<int >({1, 2});
^