Global variables

Hi, everyone,
I’m a newbe with this programming and I’d want to understand about using / modify a variable in ESPHOME.
My project use a ESP32, I receive some state/values from HA and report them in a serial display (Nextion). Now I want to use some variable in order to keepsome time function: every minute I want a simple increment but so far I can’t deploy it!
Let’s suppose:

   on_time:
      - seconds: 0
        minutes: '*'
        then:
          - globals.set:
              id: timer
              value: timer+=1

Could you help me? THANKS

where you have value: timer+=1 that is C++ code, but you can’t use C++ in yaml. You need to increment using a lambda. Try

   on_time:
      - seconds: 0
        minutes: '*'
        then:
          - lambda: |-
              id(timer) += 1;

As an alternative to on_time you could try interval automation

  - interval: 1min
    then:
      - lambda: |-
          id(timer) += 1;

Perfect the final code is

interval:
  - interval: 1min
    then:
    - lambda: |-
        id(timer) += 1;

THANKS a lot

1 Like

no worries! You know there is already a “minutes counter” using the time component - the only thing is it resets to zero every hour :grinning:
But seriously, it may actually do whatever it is you’re doing by counting minutes

This has a problem. When you work with on_boot with high priority like 900 in order to do it as soon after boot as possible, then lambda: id(variable)++ and then ESP.deepSleep does not save changed variable…
You have to set up boot_priority to max 799 in order to global variable get updated. It is done about 70ms after the boot then.