Even with the help of AI, I am going around in circles with this!
Can you YAML/C++ experts have a look at this code and hopefully correct or suggest an alternative approach?
I am reluctant to go down the Home Assistant API sensor route if it can be avoided. I am hoping to do as much as I can in the ESP32.
This is the “active ingredients” of my yaml (full disclosure available on request ):
<<: !include tinypico_mundane_base_code.yaml
globals:
- id: my_ts
type: int
restore_value: true
initial_value: '0'
text_sensor:
# See: https://esphome.io/cookbook/lambda_magic.html
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(myUART23));
App.register_component(my_custom_sensor);
return {my_custom_sensor};
text_sensors:
- id: "odb_tx"
name: "OBD TX"
#This works. But I want an incrementing conter, not static text.
filters:
prepend: "A psudo timestamp "
#filters:
# - lambda: |-
# *globals.my_ts++; // Increment the global counter
# return std::to_string(*globals.my_ts) + " " + x;
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(myUART18));
App.register_component(my_custom_sensor);
return {my_custom_sensor};
text_sensors:
- id: "obd_rx"
name: "OBD RX"
#filters:
# - lambda: |-
# *globals.my_ts++; // Increment the global counter
# return std::to_string(*globals.my_ts) + " " + x;
What I hope you can see, is that I am trying to create a global counter that is read/writable by both text_sensors and than can be pre/append to the returned value from my_custom_sensor().
The rem’d out filter section is where I would like some help. I get compilation errors on both the increment my_ts and the return lines:
Compiling .pioenvs/tinypico/src/main.cpp.o
<unicode string>: In lambda function:
<unicode string>:40:15: error: expected primary-expression before '.' token
<unicode string>:41:37: error: expected primary-expression before '.' token
*** [.pioenvs/tinypico/src/main.cpp.o] Error 1
Regards, Martin
PS - Be as rude as you like about having to resort to ChatGPT’s help