My current yaml which works:
esphome:
name: "tinxy-4n-myroom"
friendly_name: "tinxy-4n-myroom"
on_boot:
- priority: 200.0
then:
- output.turn_on: gen_pwm_test
- output.set_level:
id: gen_pwm_test
level: 50%
includes:
- uart_read_line_sensor.h
output:
- platform: esp8266_pwm
id: gen_pwm_test
pin: GPIO14
frequency: 1000 Hz
- platform: template
id: fanoutput
type: float
write_action:
- uart.write: ""
esp8266:
board: esp01_1m
# Enable logging
logger:
baud_rate: 0
level: verbose
# Enable Home Assistant API
api:
ota:
platform: esphome
password: !secret wifi_password
wifi:
manual_ip:
static_ip: 192.168.86.144
gateway: 192.168.86.1
subnet: 255.255.255.0
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
fast_connect: true
# status_led:
# pin:
# number: GPIO12
# inverted: yes
uart:
id: uart_bus
tx_pin: 1
rx_pin: 3
baud_rate: 115200
text_sensor:
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
App.register_component(my_custom_sensor);
return {my_custom_sensor};
text_sensors:
id: "uart_readline"
name: "UART Feedback"
on_value:
then:
- lambda: |-
ESP_LOGD("main", "The current value is %s", x.c_str());
if (id(uart_readline).state == "41") {
id(relay4_wall_switch).publish_state(true);
id(relay4).turn_on();
} if(id(uart_readline).state == "40") {
id(relay4_wall_switch).publish_state(false);
id(relay4).turn_off();
}
if (id(uart_readline).state == "31") {
id(relay3_wall_switch).publish_state(true);
id(relay3).turn_on();
} if(id(uart_readline).state== "30") {
id(relay3_wall_switch).publish_state(false);
id(relay3).turn_off();
}
if (id(uart_readline).state == "21") {
id(relay2_wall_switch).publish_state(true);
id(relay2).turn_on();
} if(id(uart_readline).state== "20") {
id(relay2_wall_switch).publish_state(false);
id(relay2).turn_off();
}
if (id(uart_readline).state == "11") {
id(relay1_wall_switch).publish_state(true);
auto call = id(fan_1).turn_on();
call.perform();
} if(id(uart_readline).state== "10") {
id(relay1_wall_switch).publish_state(false);
auto call = id(fan_1).turn_off();
call.perform();
} if (id(uart_readline).state == "13") {
id(relay1_wall_switch).publish_state(true);
auto call = id(fan_1).turn_on().set_speed(1);
call.perform();
} if (id(uart_readline).state == "14") {
id(relay1_wall_switch).publish_state(true);
auto call = id(fan_1).turn_on().set_speed(2);
call.perform();
} if (id(uart_readline).state == "15") {
id(relay1_wall_switch).publish_state(true);
auto call = id(fan_1).turn_on().set_speed(3);
call.perform();
}
binary_sensor:
- platform: template
name: "Relay4 Wall Switch"
id: relay4_wall_switch
- platform: template
name: "Relay3 Wall Switch"
id: relay3_wall_switch
- platform: template
name: "Relay2 Wall Switch"
id: relay2_wall_switch
- platform: template
name: "Relay1 Wall Switch"
id: relay1_wall_switch
switch:
- platform: gpio
pin: GPIO12
name: "Green LED"
- platform: template
name: "Relay2"
id: relay2
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
turn_on_action:
- uart.write: '#2100#'
turn_off_action:
- uart.write: '#2000#'
- platform: template
name: "Relay3"
id: relay3
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
turn_on_action:
- uart.write: '#3100#'
turn_off_action:
- uart.write: '#3000#'
- platform: template
name: "Relay4"
id: relay4
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
turn_on_action:
- uart.write: '#4100#'
turn_off_action:
- uart.write: '#4000#'
fan:
- platform: speed
output: fanoutput
id: fan_1
speed_count: 3
name: "Fan"
on_turn_on:
- uart.write: !lambda |-
static std::string myUartStringsOn[4] = {"#1100#", "#1033#", "#1066#", "#1100#"};
if(id(fan_1).speed == 0)
id(fan_1).speed = 1;
std::string chosenString = myUartStringsOn[id(fan_1).speed];
std::vector<uint8_t> vec(chosenString.begin(), chosenString.end());
return vec;
on_turn_off:
- uart.write: '#1000#'
on_speed_set:
- if:
condition:
lambda: return (id(fan_1).state);
then:
- uart.write: !lambda |-
static std::string myUartStringsOn[4] = {"#1100#", "#1033#", "#1066#", "#1100#"};
std::string chosenString = myUartStringsOn[id(fan_1).speed];
std::vector<uint8_t> vec(chosenString.begin(), chosenString.end());
return vec;
number:
- platform: template
name: "Switch Delay"
id: "switchdelay"
optimistic: true
restore_value: true
initial_value: "60"
min_value: 5
max_value: 100
step: 1
set_action:
then:
- uart.write: !lambda |-
int multiplexer = 625 + static_cast<int>((x - 5) * 25);
String uartValue = "(";
if (multiplexer < 1000) {
uartValue += "0";
}
uartValue += String(multiplexer, DEC) + ")";
std::vector<unsigned char> uartBytes(uartValue.begin(), uartValue.end());
return uartBytes;
Uart reading library
#include "esphome.h"
class UartReadLineSensor : public Component, public UARTDevice, public TextSensor {
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
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 ';': // 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 = 20;
static char buffer[max_line_length];
while (available()) {
if(readline(read(), buffer, max_line_length) > 0) {
publish_state(buffer);
}
}
}
};
I used this code below which made the device to burst and made the device to stop working.
// Synchronize ESPHome with actual fan state based on switch feedback
if (id(uart_readline).state == "10" && id(fan_1).state) {
auto call = id(fan_1).turn_off();
call.perform();
ESP_LOGD("sync", "Fan state corrected to OFF due to switch state '10'.");
} else if (id(uart_readline).state == "11" && !id(fan_1).state) {
auto call = id(fan_1).turn_on();
call.perform();
ESP_LOGD("sync", "Fan state corrected to ON due to switch state '11'.");
}
More images - Imgur: The magic of the Internet
I have a feeling this happened because both UART code logic and this code is trying to update the virtual switch in HA dashboard at the same time, and it caused it. I am not sure. All I know is I updated this code and turned off the switch, I heard the first spark. The second time I turned on the switch heard spark again and lost the device.
I am NOT here to argue or prove myself right or say esphome did it. I am here trying to find the right yaml code to make it work with my device. I gave this piece of information to help find the right yaml code and reuse the old code which made the device stop working.
More information on discord thread where I was getting help trying to sync HA virtual switch with the physical wall switch – Discord
I am trying to make my fan switch work like a one way switch. Only my fan switch doesn’t work like a one way switch and it works like a two-way switch. All my other 3 relay switches work like an on-way switch. I am just trying to make my fan switch make it work like a one-way switch, which is what I am trying to do. Any input, advice, suggestion is highly appreciated. Thank you!