Hi all,
i would like to use an ESPHome to create Sensors in HA from an UART Reading.
My oven send every second a long String through an UART interface. The String looks like this:
If you get the string in to HA then you can perhaps use this template to convert it to a json that can be placed as attributes and probably should work.
Just replace the string with the entity
{% set str = "State= 30. 31, Temp= 29.3, SOLL-Pos= 5, IST-Pos= 76, MotorMAX=1520, Zeit=45143, TempAV= 29, MAX_Temp=749, TempGradient= 0, Tuer=1, ECO= 0, LEDRot=1, LEDGruen=0, ST= 0, Magnet=0, Relais=1, Control=0, PRIME= 1, BetriebsStdn= 1391" %}
{% set str = str.replace("= ", "=") %}
{% set arr = str.split(", ") %}
{
{% for ar in arr -%}
{% set pair = ar.split("=") %}
{% for p in pair -%}
"{{ p }}"
{%- if loop.first %}: {% elif loop.last %}, {% endif -%}
{%- endfor %}
{%- endfor %}
}
Hello,
i’m a little bit further in my approach.
I’ve got on temprature reading to HA. And it is done at the ESP.
I mixed a littlebit the UART Text sensor with general custom sensor stuff.
But now my next try is to create a sensor and a binarysensor in one component. Like the Custom_Sensor with the two outputs.
Two sensors works but i want one sensor and one binary_sensor.
What i have to do?
Here is my current code:
#include "esphome.h"
class UartReadLineSensor : public Component, public UARTDevice{
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
Sensor *temperature_sensor = new Sensor();
Sensor *tuer_sensor = new Sensor();
void setup() override {
// nothing to do here
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void loop() override {
const int max_line_length = 240;
static char buffer[max_line_length];
char buffer2[max_line_length];
String mystring = "123";
float temp = 0;
bool tuer = 0;
while (available()) {
if(readline(read(), buffer, max_line_length) > 0) {
mystring = String(buffer);
//mystring = mystring.substring(20, 24);
temp = mystring.substring(20, 25).toFloat();
tuer = mystring.substring(133, 134).toInt();
mystring.toCharArray(buffer2,max_line_length);
temperature_sensor->publish_state(temp);
tuer_sensor->publish_state(tuer);
}
}
}
};