It would be nice feature ? sensor filter?
should be like this ?
lambda: |-
std::string val = id(text_number).state;
int numeric_value = atoi(val.c_str());
return numeric_value;
It would be nice feature ? sensor filter?
should be like this ?
lambda: |-
std::string val = id(text_number).state;
int numeric_value = atoi(val.c_str());
return numeric_value;
There’s slightly different ways to do this which you’ll find on a forum search. I believe all require use of lambda’s.
Basically you need to convert the string to a float (like you’ve done) and then publish it to a sensor - as numeric values need to be in a sensor:
.
How can i get sensor value to parse ?
this dont work:(
- interval: 20sec
then:
- sensor.template.publish:
id: num_from_text2
state: !lambda |-
auto n = parse_number<float>(id(sensor1).state);
return n.has_value() ? n.value() : NAN;
error: ‘const class esphome::custom::CustomTextSensorConstructor’ has no member named ‘state’
with Custom UART Device install
#include "esphome.h"
class MyCustomComponent : public Component, public UARTDevice {
public:
MyCustomComponent(UARTComponent *parent) : UARTDevice(parent) {}
void setup() override {
// nothing to do here
}
void loop() override {
// Use Arduino API to read data, for example
String line = readString();
int i = parseInt();
while (available()) {
char c = read();
}
// etc
}
};
gave error:
src/my_custom_component.h: In member function 'virtual void MyCustomComponent::loop()':
src/my_custom_component.h:12:19: error: 'readString' was not declared in this scope
String line = readString();
^~~~~~~~~~
src/my_custom_component.h:12:19: note: suggested alternative: 'String'
String line = readString();
^~~~~~~~~~
String
src/my_custom_component.h:13:13: error: 'parseInt' was not declared in this scope
int i = parseInt();
^~~~~~~~
src/my_custom_component.h:13:13: note: suggested alternative: 'parent_'
int i = parseInt();
^~~~~~~~
parent_
*** [.pioenvs/o2/src/main.cpp.o] Error 1
========================= [FAILED] Took 17.70 seconds =========================