About secret variables

Hi all,
I’m trying like since a couple of hours to use a secret variable inside a lambda.
Somehow, it looks like, it’s not possible directly.
So I’ve created a global variable of type string like this:

globals:
  - id: emoncms_api_key
    type: std::string
    restore_value: no
    initial_value: !secret my_variable

my_variable is defined as"123fadk99kjdfa789" in secrets.yaml.
And then in the lambda, I’ve tried “almost” everything similar to:

std::string test = id(emoncms_api_key);

I get this error:

/config/esphome/linky-huzzah-esp8266.yaml: In function 'void setup()':
/config/esphome/linky-huzzah-esp8266.yaml:95:64: error: unable to find numeric literal operator 'operator""fadk99kjdfa789'
   95 |           url: !lambda |-
      |                                                                ^                
*** [.pioenvs/linky-huzzah-esp8266/src/main.cpp.o] Error 1

As you can see, all numbers from the beginning of the variable are removed, then the rest is correct !!
What’s going on ?!? I don’t understand it all !

Thx for your help,
Fred

Is there perhaps an other way to use a secret in a lambda ?

What lambda do you have?

Edit: Sorry you have provided an example. Need more coffee;)

1 Like

Well, I’ve just removed everything from the lambda, I still get the strange error.
So, it looks like the initialization of the global variable is buggy.
Even the doc says the initial_value is a string, it’s getting somehow interpreted/converted/… and the any starting number leads to this strange behavior.

If you just directly type the value in the initial_value field in your yaml does it work as expected?

That might help confirm at what point the issue is being introduced?

Try

'"123fadk99kjdfa789"'

in your secret

I think the yaml parser is thinking the quotes are yaml rather than defining it as a string.

Another thing to try might be

initial_value: "!secret my_variable"

Oh, and finally, you may need to use this syntax to access the string inside that global as a string:

std::string test = id(emoncms_api_key).c_str();

Please do report what works!

I’ll try when I’m back from the office…

I’ve had success reading secrets using substitutions

# secrets.yaml
my_variable: "123fadk99kjdfa789"

# main yaml file
substitutions:
  emoncms_api_key: !secret my_variable

# lambda code
std::string test = "${emoncms_api_key}";