Syntax help please

I have the following code that need help to get syntax right.

esp32_ble_tracker:
  on_ble_service_data_advertise:
    - mac_address: E9:F5:D3:BB:1C:30
      service_uuid: 180F
      then:
        - lambda: 'id(PowerMeter_sensor_battery).publish_state(x[0]);'
    - mac_address: E9:F5:D3:BB:1C:30
      service_uuid: 2A03
      
      then:
        - lambda: |-
            std::string b(x.begin(), x.end());      
            ESP_LOGD("PowerMeter_sensor_instant_power:", "%s", &b[0] );
            id(PowerMeter_sensor_instant_power).publish_state(???);

the value is a number over 500. I found an example that gets it into a string output in LOGD, but I cannot pass the same value into the publish_state

You should be able to use the std::stoi function. Can you try replacing ??? with std::stoi (b, nullptr)?

No go

/config/esphome/m5atom-lite.yaml: In lambda function:
/config/esphome/m5atom-lite.yaml:39:54: error: 'stoi' is not a member of 'std'
             id(PowerMeter_sensor_instant_power).publish_state(std::stoi (b, nullptr));

Unfortunately std::stoi doesn’t work as ESPHome doesn’t include <string>. But there’s an alternative, parse_int, it returns an optional<int> though.

        - lambda: |-
            std::string b(x.begin(), x.end());      
            ESP_LOGD("PowerMeter_sensor_instant_power:", "%s", b.c_str());
            id(PowerMeter_sensor_instant_power).publish_state(parse_int(b).value());

I did not try the suggested, but made it work with parse_float, as publish_state expects float value for some reason

@oxan - do you know what &b[0] means in the code above? I have just reused it, but can only guess…

It takes the address of the first character of the string – basically a hacky way of converting the C++ string object to a C-style string for the log macros. Using b.c_str() is better.

1 Like

Do you know why I get error: 'parse_int' was not declared in this scope when compiling to esp32? I’m trying to replace std::stoi because it isn’t apparently included in the esp32 libs.

How about going old school and using atoi(str.c_str())?

FWIW, parse_int() also doesn’t work for me, stof/stoi only on ESP8266.

Thanks for the suggestion. Does it work for you on esp32? I just get error: cannot convert 'const value_type {aka const std::__cxx11::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'. I know nothing about C++, but apparently I can’t just replace stoi with atoi. I guess I need to troubleshoot this on github.

You have to use atoi(str.c_str()), not atoi(str) (where str should be the name of the std::string variable you want to convert).

1 Like
src/vallox2.h:493:31: error: 'str' is not a member of 'std'
         setFanSpeed(std::atoi(std::str.c_str(*call.get_custom_fan_mode())));

I’m still getting errors. By the way, thank you for so kindly helping me out.

It seems pretty weird that something as basic as std::str is left out

Try this (I think I have the parentheses right :sweat_smile:):

setFanSpeed(atoi((*call.get_custom_fan_mode()).c_str()));

Oh my goodness, it compiled! Thank you :smiley: