How do I loop through a string?

I have tried all of these methods but I still can’t get it working.

Example:

            then:
              - lambda: |-
                      for(std::string::size_type i = 0; i < id(somestring).state.size(); ++i) {
                          ESP_LOGD("%s", id(somestring).state[i]);
                      }
In file included from src/esphome/components/display/display.h:11,
                 from src/esphome/components/display/display_buffer.h:6,
                 from src/esphome/components/addressable_light/addressable_light_display.h:5,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
/config/esphome/waveshare.yaml: In lambda function:
src/esphome/core/log.h:95:100: error: invalid conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'} to 'const char*' [-fpermissive]
   esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
                                                                                                    ^
src/esphome/core/log.h:152:28: note: in expansion of macro 'esph_log_d'
 #define ESP_LOGD(tag, ...) esph_log_d(tag, __VA_ARGS__)
                            ^~~~~~~~~~
/config/esphome/waveshare.yaml:275:11: note: in expansion of macro 'ESP_LOGD'
                           ESP_LOGD("%s", id(somestring).state[i]);
           ^       
src/esphome/core/log.h:56:72: note:   initializing argument 4 of 'void esphome::esp_log_printf_(int, const char*, int, const char*, ...)'
 void esp_log_printf_(int level, const char *tag, int line, const char *format, ...)  // NOLINT

I just can’t get this to work.
My end goal is to use each character in a if/elseif, at least that is what I believe for now.

Maybe this is of some help?

Hmm… this seems to split the string in chunks based on “\r”. Not really what I wanted

I tried this code, and it compiles but it doesn’t output anything in the logs.
Not sure if it works or not. That was what I was hoping the ESP_LOGD would help me figure out.

              - lambda: |-
                      for (int i = 0; i < id(somstring).state.size(); i++) {
                        ESP_LOGD("test: ", "%s", id(passtext).state[i]);
                      }
1 Like

Yeah I thought I had something more relevant but I thought there may still be parts of it which may be of some use.

Try like this. Works for me. %c may be the secret sauce.

text:
  - platform: template
    name: "Template text"
    id: somstring
    optimistic: true
    mode: text
    initial_value: TestLoop
    on_value:
      then:
        - lambda: |-
              std::string str = id(somstring).state;
              for (size_t i = 0; i < str.size(); i++) {
                ESP_LOGD("test", "%c", str[i]);
              }
1 Like

Will try this tomorrow!

1 Like

It works.
Perhaps some of the other methods also worked. I realized I made a mistake in the code.
I have an if else and I placed my loop in the wrong section.
I started getting suspicious when your working your on_value worked but the exact same lambda did not work in my code.

Thank you!

1 Like