[ESPHOME] mqtt write payload to global variable

Hello,

im trying to get the payload from an topic into an variable but it does not work i always get an error.
i would like to get an string into a global variable but since i didnt find anything about that i tried to write an INT

what i tried is:

globals:
  - id: mqttvalue
    type: int
    restore_value: yes
    initial_value: '0'
    - topic: Effect
      qos: 1
      then:
        - globals.set:
            id: mqttvalue
            value: !lambda |-
              return x;

which give me this error:


In file included from src/esphome/components/api/user_services.h:6:0,
                 from src/esphome/components/api/api_server.h:13,
                 from src/esphome/components/api/api_connection.h:7,
                 from src/esphome.h:2,
                 from src/main.cpp:3:
src/esphome/core/automation.h: In instantiation of 'esphome::TemplatableValue<T, X>::TemplatableValue(F) [with F = setup()::__lambda58; typename std::enable_if<esphome::is_callable<F, X ...>::value, int>::type <anonymous> = 0; T = int; X = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]':
src/esphome/components/globals/globals_component.h:70:3:   required from 'void esphome::globals::GlobalVarSetAction<C, Ts>::set_value(V) [with V = setup()::__lambda58; C = esphome::globals::RestoringGlobalsComponent<int>; Ts = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]'
/config/esphome/wemos.yaml:153:4:   required from here
src/esphome/core/automation.h:28:46: error: no matching function for call to 'std::function<int(std::basic_string<char>)>::function(setup()::__lambda58&)'
   TemplatableValue(F f) : type_(LAMBDA), f_(f) {}
                                              ^
src/esphome/core/automation.h:28:46: note: candidates are:
In file included from src/esphome/core/component.h:4:0,
                 from src/esphome/components/api/api_connection.h:3,
                 from src/esphome.h:2,
                 from src/main.cpp:3:
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2443:7: note: template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor)
       function<_Res(_ArgTypes...)>::
       ^
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2443:7: note:   template argument deduction/substitution failed:
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2226:7: note: std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = int; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
       function(function&& __x) : _Function_base()
       ^
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2226:7: note:   no known conversion for argument 1 from 'setup()::__lambda58' to 'std::function<int(std::basic_string<char>)>&&'
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2429:5: note: std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = int; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
     function<_Res(_ArgTypes...)>::
     ^
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2429:5: note:   no known conversion for argument 1 from 'setup()::__lambda58' to 'const std::function<int(std::basic_string<char>)>&'
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2206:7: note: std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = int; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; std::nullptr_t = std::nullptr_t]
       function(nullptr_t) noexcept
       ^
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2206:7: note:   no known conversion for argument 1 from 'setup()::__lambda58' to 'std::nullptr_t'
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2199:7: note: std::function<_Res(_ArgTypes ...)>::function() [with _Res = int; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
       function() noexcept
       ^
/data/cache/platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/functional:2199:7: note:   candidate expects 0 arguments, 1 provided
*** [/data/elekstube/.pioenvs/elekstube/src/main.cpp.o] Error 1
========================== [FAILED] Took 2.08 seconds ==========================

i also tried

    - topic: Effect
      qos: 1
      then:
        - lambda: |-
            id(mqttvalue) = int(x);
            #and also  --> id(mqttvalue) = x;
/config/esphome/wemos.yaml: In lambda function:
/config/esphome/wemos.yaml:148:33: error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'int'
             id(mqttvalue) = int(x);
                                 ^
*** [/data/elekstube/.pioenvs/elekstube/src/main.cpp.o] Error 1
========================== [FAILED] Took 2.08 seconds ==========================

how can i solve that?

Guessing here, but '0' is not an int?

thank you, you are probably right. meanwhile i solved it.

but still would like to know how to get an string payload like “HELLO” and insert it into an global variable

Did you try to define your global var using a string type?
I would start with one of these (untested):

globals:
  - id: myStringVar
    type: string

globals:
  - id: myStringVar
    type: std::string

(The second one is probably the correct syntax.)

But didn’t tell how? And you ask another question?

Rude.

2 Likes

@nickrout
hi, i solved it exactly like you wrote it. maybe 10 minutes before you wrote your answer.

@Jpsy

thank you i will try that