yes. I was suggesting hard coding it in an array, but now I understand what you mean. So back to your code above, double check that you are using std::string
rather than just string
or maybe throw in a using namespace std
. See if that makes a difference. Aside from that, I’m not sure you can use stringstream on ESP32, but I could be wrong.
Here’s it working with an array:
sensor:
- platform: template
name: testing_test
id: test_1
lambda: |-
std::string strArray[3] = { "09:00", "12:00", "13:00" };
for ( std::string s : strArray ) {
ESP_LOGD("test: ", "Array to string: %s", s.c_str());
}
return 1;
Which produces this output:
[03:08:50][D][test: :182]: Array to string: 09:00
[03:08:50][D][test: :182]: Array to string: 12:00
[03:08:50][D][test: :182]: Array to string: 13:00
[03:08:50][D][sensor:092]: 'testing_test': Sending state 1.00000 with 1 decimals of accuracy
Second Edit:
I think this does what you want.
text_sensor:
- platform: homeassistant
name: "hatext"
entity_id: input_text.text1
id: hatext
- platform: template
name: testing_test2
id: test_2
update_interval: 10s
lambda: |-
std::string str = id(hatext).state;
std::vector<std::string> v;
char * token;
char seps[] = ",";
token = strtok (&str[0],seps);
while (token != NULL)
{
v.push_back(token);
token = strtok (NULL, seps);
}
for ( std::string s : v ) {
ESP_LOGD("test: ", "String to Vector: %s", s.c_str());
}
return 1;
Which outputs:
[04:14:18][D][text_sensor:015]: 'hatext': Sending state '09:00,12:00,13:00'
[04:14:20][D][test: :204]: String to Vector: 09:00
[04:14:20][D][test: :204]: String to Vector: 12:00
[04:14:20][D][test: :204]: String to Vector: 13:00
[04:14:20][D][sensor:092]: 'testing_test2': Sending state 1.00000 with 1 decimals of accuracy
[04:14:25][D][homeassistant.text_sensor:016]: 'input_text.text1': Got state '09:00,12:00,15:00'
[04:14:25][D][text_sensor:015]: 'hatext': Sending state '09:00,12:00,15:00'
[04:14:30][D][test: :204]: String to Vector: 09:00
[04:14:30][D][test: :204]: String to Vector: 12:00
[04:14:30][D][test: :204]: String to Vector: 15:00
[04:14:30][D][sensor:092]: 'testing_test2': Sending state 1.00000 with 1 decimals of accuracy