Hi everybody, I’m pretty new on HA and EspHome. I have a sensor that returns 5 characters through uart in hexadecimal format, for example the ascii characters 00500 → 30:30:35:30:30 in hexa. I need to compare these values and change the state of a GPIO, but for that I need them to be in decimal format and be readable by HA. Is there any form to change these values from Hexa to Decimal? I am using UART Text Sensor.
In the image you can see the responses I get from the sensor (30:30:30:30:30). I need to change that data to Decimal, it should be 00000. The lambda publishes the state of the buffer. Any Ideas how to do that?
Hello mulcmu, I tried your aproach but I get a lot of “trash data” like numbers and “???” in RawString template even when I am not requesting the info from the sensor. The closest I got was with the UART readline but I can’t parse the info I get from the custom sensor, I tried to get the position I need from the buffer and compare it with a value to trigger a binary sensor but I cant get it.
I think it is something of the sensor, because I tried your program with an Arduino sending data through serial and receiving with an ESP and it works.
Hello again, thanks for replying. Each time I send “%” sensor will reply with the measure value. But now I keep getting all that data. Here is the Yaml.
esphome:
name: serial-inficon
friendly_name: serial_inficon
includes:
# - datos/uart_read_line_sensor.h
# - datos/my_custom_component.h
esp8266:
board: nodemcuv2
# Example configuration entry
web_server:
port: 80
# Enable logging
# Enable Home Assistant API
api:
encryption:
key: "xxxxxxxxx"
ota:
password: "xxxxxxxxxx"
wifi:
ssid: !xxxxxxxxx
password: !xxxxx
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Serial-Inficon Fallback Hotspot"
password: "xxxxxx"
captive_portal:
logger:
baud_rate: 9600
uart:
baud_rate: 9600
tx_pin: GPIO1
rx_pin: GPIO3
debug:
direction: RX
dummy_receiver: true
after:
delimiter: "\n"
sequence:
- lambda: |-
UARTDebug::log_string(direction, bytes); //Still log the data
int sensorID=0;
float sensorTEMP=0;
//Example to convert uart text to string
std::string str(bytes.begin(), bytes.end());
//watch for potential problems with non printable or special characters in string
id(rawString).publish_state(str.c_str());
//Sample binary / hex data
if (bytes.size()==9) {
if(bytes[0]==0x40)
id(binData).publish_state( bytes[4] );
}
text_sensor:
- platform: template
name: "Raw String"
id: "rawString"
sensor:
- platform: template
name: "Temp 1"
id: "temp1"
- platform: template
name: "Temp 2"
id: "temp2"
- platform: template
name: "Binary Data"
id: binData
button:
- platform: template
name: "Test Button %"
on_press:
- uart.write: "%"
- platform: template
name: "Test Button R"
on_press:
- uart.write: "R"
#if you need to send a request periodically to get response from the uart device
interval:
- interval: 60s
then:
- uart.write: "%"
I am trying other options in the logger right now. I removed the part of the temperature sensors from your sketch.
I am 99,9% sure that my sensor is the problem. I need to make some changes then try again. Thank you for your time, I will update news if I have something.