Hello guys,
I know the way to set global variables. But I find it very uncomfortable when you need many varriables.
Is there a way to define global varriables inside a lambda?
And, is there a way to write/read data directly to the flash?
I ask these questions because I am planning to program a light control for my aquarium. I have not found anything like this. For this I need many variables to calculate the dimming curves for all color channels.
Thanks Blayk, but that doesn’t begin to answer my questions. Perhaps I need to be more clear. I wrote that I know about the “globals:”. And I find them too cumbersome for the many variables I need.
globals:
- id: value
type: int
restore_value: no
initial_value: ‘0’
is one way…
lambda: |-
int value=0;
is much easier. Unfortunately different ‘lambdas’ have no common namespace.
lambda: |-
int value=5;
…
lambda: |-
if (value) …
does not work.
I also don’t need the variables to be storable. But I would like to write something on the flash myself…
Hi Gabriel,
I came across this post and your solution when doing things with global variables in ESPHome. Almost sure you have your thing running already with your class, but just in case I post my findings:
Defining global variable like this:
globals:
- id: count1
type: unsigned int
restore_value: no
initial_value: '0'
- id: count2
type: unsigned int
restore_value: no
initial_value: '0'
Note that you will need to use the id( var ) syntax to pick the global context. Hope this helps.
Why did I need these globals?
I did something like this in a project that I needed to turn on a fan for only 15 min. At first, I did something like this:
But I realized that if you turned on and off multiple times in a row, it was queuing multiple events to ‘turn off’. Consequently, the fan went off 15 min after the first ‘turn on’ even if the switch was turned on and off very recently.
So I thought to use 2 global counters incremented in the ‘on_turn_on’ and when the delayed action was performed. Comparing these 2 counters I make sure I only turn off the fan 15 min after the last turn on.
This is the code: