Using the value of a global variable

I’m trying to learn how to use global variables. I have seen that there’s a function set() to allow setting one without using a lambda.

What I do not see, and can’t find, is how to use the current value of a global variable. There are no examples in the documentation and nothing clear when I search for it. (I found this, which indicates you can only access a global variable with a lambda, but the link in the accepted reply returns “Page not found.”)

I’ve tried this in YAML, but it throws a compile error:

globals:
  - id: TestVar
    type: string
    initial_value: "First Text"

time:
  - platform: sntp
    on_time:
      - seconds: /15
        then:
          - logger.log:
              format: "Checking on variable, TestVar: ->%s<-"
              args: id(TestVar)

So how would I get the value of TestVar to log the current value (or to use it in something else)?

As per the example in the docs - you probably need to apply .c_str():

https://esphome.io/components/globals#global-variables

      then:
         - logger.log:
             format: "Checking on variable, TestVar: ->%s<-"
             args: id(TestVar).c_str()

I know you need to in lambda , so probably for logger.log as well.

Okay. That part of the example was a bit confusing, since I haven’t used or was at all clear about the function ESP_LOGD() and wasn’t sure just what was going on in there and missed the use of c_str().

I also realized I had used “string” instead of “std::string.”

And I see, in the example, the initial value of the string is in a set of double and a set of single quotation marks, like this: ‘"extra quotes’". What’s the reasoning for that?

It worked to display the variable, but when I try to use globals.set, I run into compile errors:

globals:
  - id: TestVar
    type: std::string
    initial_value: '"Testing the variable"'

time:
  - platform: sntp
    on_time:
      - seconds: /15
        then:
          - logger.log:
              format: "Checking on varible, TestVar: ->%s<-"
              args: id(TestVar).c_str()
          - globals.set:
              id: TestVar
              value: 'And it is tested'

To me the part where I set a new value in TestVar looks just like what’s in the example (although the example changes an integer, not a string). I get errors:

src/main.cpp: In function 'void setup()':
src/main.cpp:308:44: error: 'And' was not declared in this scope
   globals_globalvarsetaction_id->set_value(And it is tested);
                                            ^~~
*** [.pioenvs/esp32x/src/main.cpp.o] Error 1

So when I use quotation marks, it takes the value as a component and gives me an error. I tried adding c_str() to it, with the slim hope that might help, but when I do that, I get an error even before it gets to compiling. I also tried it with double-quote marks instead of single ones. Same problem with double-quotation marks.

The example in the docs, for setting a value, is:

on_...:
  - globals.set:
      id: my_global_var
      value: '10'

There is no documentation about whether this is setting a string or int. I found that to set my string, I had to specify value: '"Text info"'. This is the same as with my issue above. I had to enclose the text in double-quotes and, outside that, in single quotes.

What does this do and why is it needed? Seems as if single quotes are required around an int as well, so I’m wondering if the single quotes have to surround a quoted string or an int or any value for a global.

Not sure why it works the way it does, that’s a question for the devs. I would try:

          - globals.set:
              id: TestVar
              value: '”And it is tested”'

That’s what I’m finding works. Yes, it’s a question for devs, but it helps to know just why I need to use double and single quotation marks, so I know what to use ad when in the future.