Deep_sleep sleep_duration via global variable set via MQTT

While I can personally use a deep_sleep mode myself,
I’m currently breaking my head over the following process:

  • Receiving a MQTT value
  • Store the MQTT value in a global int <----
  • Recall the int, and apply to the deep_sleep_duration

I think I’m doing something wrong storing the MQTT value in the global int.
Therefore I’m storing it in a global int and string for testing:

globals:

  • id: global_deepsleep_time_int
    type: int
    restore_value: no
    initial_value: ‘60’

  • id: global_deepsleep_time_str
    type: std::string
    restore_value: no
    initial_value: ‘“60”’

The MQTT part, writing to both global int and str:

mqtt:
on_message:
- topic: sensor02/deepsleep/time
then:

Saving it to the Global STR

    - globals.set:
        id: global_deepsleep_time_str
        value: !lambda |-
          return x.c_str();
    - logger.log:
        format: "1_str: MQTT Received sleep time %s seconds"
        args: ['id(global_deepsleep_time_str).c_str()']

Saving it to the Global INT

    - globals.set:
        id: global_deepsleep_time_int
        value: !lambda |-
          return int(x.c_str());
    - logger.log:
        format: "2_int MQTT Received sleep time %d seconds"
        args: ['id(global_deepsleep_time_int)']

And painfully watching the unexpected int value of 1070199900 seconds:

[01:56:37][D][main:089]: 1_str: MQTT Received sleep time 22 seconds
[01:56:37][D][main:097]: 2_int MQTT Received sleep time 1070199900 seconds
[01:56:39][D][main:089]: 1_str: MQTT Received sleep time 36 seconds
[01:56:39][D][main:097]: 2_int MQTT Received sleep time 1070199900 seconds
[01:56:43][D][main:089]: 1_str: MQTT Received sleep time 57 seconds
[01:56:43][D][main:097]: 2_int MQTT Received sleep time 1070199900 seconds

Recalling initial data from the global int works fine.
It’s only when I write MQTT data to the global int, the number goes 1070199900

Question: How can I write the correct global int with the received MQTT data?

Is the 1_str value right?

Can you see the value of x in the logs? (I’m not familiar with MQTT behaviour). Might need to increase log level?

1070199900 looks like a timestamp?

Possibly it has to do with your int().

Yes, the 1_str values are correct.
The integer is probably a conversion going wrong, while writing it to the global int variable, which I can’t put my finger on.

Did you see/try the int() alternatives in my link?

Thanks for pointing it out.
I didn’t read all yet, but you made me curious now.
Freeing up time soon to try it.
Guessing for now, writing the integer:

  globals.set:
         id: global_deepsleep_time_int
         value: !lambda |-
           return atoi(x.c_str());

Not sure if I’m doing the right thing.
Reading this converts the result x to a character string.
Then the character string converted to integer by atoi.

Maybe the result x was a string to begin with.
Anyways, this is just theoretical thinking, I need my hardware/pc to test it.

1 Like

I can confirm, the pointer to the function atoi is the sollution to convert the MQTT state to an int.
Thank you @Mahko_Mahko

1 Like