Hello, I created a TCP client to return the status of an alarm. I get the alarm response, but I couldn’t send the response to HA.
What I want is to send the alarm response to HA and also create a template in HA to create the sensors.
I tried with text sensor but it doesn’t compile.
YAML:
substitutions:
#Configurações do dispositivo:
friendly_name: ESP AMT Control
name: espcontrol
versao: '0.5%'
esp32:
board: esp32dev
framework:
type: arduino
packages:
device_base: !include .device_basic_esp32_client.yaml
api:
encryption:
key: !secret espamtcontrol_key
##################################################
esphome:
includes:
- ./client/client.h
custom_component:
- lambda: |-
auto command = new CommandComponent();
return {command};
######################### COMANDOS #########################
binary_sensor:
######################### PGM #########################
- platform: template
device_class: door
name: Zona 22
id: espcontrol_22
- platform: template
device_class: door
name: Zona 23
id: espcontrol_23
- platform: template
device_class: door
name: Zona 24
id: espcontrol_24
Custom component:
#include "esphome.h"
#include "WiFi.h"
//// Status Check intervel
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
const long interval = 10000;
//// Check loop time
unsigned long StartTime = millis();
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
//// Delay for connection with HA
unsigned long DelayAfterBoot = 3000;
const uint16_t port = 9009;
const char * host = "192.168.0.15";
const int32_t timeout = 500; // timeout in milliseconds
String Status = {"\x0a\xe9\x21\x36\x38\x36\x31\x37\x33\x5b\x21\x4a"};
String line;
class CommandComponent : public Component {
public:
void setup() override {
}
void loop() override {
if (millis() >= DelayAfterBoot) {
unsigned long StartTime = millis();
currentMillis = millis();
if (millis() - previousMillis >= interval) {
previousMillis = millis();
status_update();
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
if ((ElapsedTime >= 30) && (ElapsedTime <= 1000)) {
Serial.print("ElapsedTime: ");
Serial.println(ElapsedTime);
}
Serial.print("ElapsedTime: ");
Serial.println(ElapsedTime);
}
}
}
void status_update() {
WiFiClient client;
if (!client.connect(host, port, timeout)) {
Serial.println("Connection to host failed");
ESP_LOGD(" ERROR", "Connection to host failed");
return;
}
Serial.println("Connected to server successful!");
client.print(Status);
line = client.readStringUntil(timeout);
delay(5);
if (client.available() > 0)
{
}
else {
Serial.println("No client...");
}
Serial.println("Disconnecting...");
client.stop();
// HEX
for (int i = 0; i < line.length(); i++) {
Serial.print(line[i], HEX);//excludes NULL byte print HEX value
Serial.print(" ");
}
Serial.println("");//excludes NULL byte print HEX value*/
if ((line[1] == 0xe9) && (line.length() >= 50)) {
if ((line[4] >> 5) & (0x01 == 0x01)) {
// Within lambda, publish an OFF state.
id(espcontrol_22).publish_state(true);
} else {
// Within lambda, publish an ON state.
id(espcontrol_22).publish_state(false);
}
if ((line[4] >> 6) & (0x01 == 0x01)) {
// Within lambda, publish an OFF state.
id(espcontrol_23).publish_state(true);
} else {
// Within lambda, publish an ON state.
id(espcontrol_23).publish_state(false);
}
if ((line[4] >> 7) & (0x01 == 0x01)) {
// Within lambda, publish an OFF state.
id(espcontrol_24).publish_state(true);
} else {
// Within lambda, publish an ON state.
id(espcontrol_24).publish_state(false);
}
} else {
ESP_LOGD(" ERROR", "NULL");
}
}
};
Response from serial monitor:
37 E9 0 0 A6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 41 52 1 0 0 4 12 2 C 9 16 0 0 0 0 0 F 0 0 0 0 0 0 0 0 0 0 0 0 0 9D
For the template I need to do something like this:
if ((line[4] >> 7) & (0x01 == 0x01)) {
// Within lambda, publish an OFF state.
id(espcontrol_24).publish_state(true);
} else {
// Within lambda, publish an ON state.
id(espcontrol_24).publish_state(false);
}
I appreciate any help.