finally got sonoff dual r1 code esphome working as I need
This commit is contained in:
@@ -67,7 +67,7 @@ substitutions:
|
||||
|
||||
# Device Settings
|
||||
relay_icon: "mdi:heating-coil"
|
||||
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
|
||||
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
|
||||
update_interval: "20s" # update time for for general sensors etc
|
||||
|
||||
# Timer Settings
|
||||
@@ -113,6 +113,12 @@ esphome:
|
||||
friendly_name: "${friendly_name}"
|
||||
comment: "${description_comment}" # Appears on the esphome page in HA
|
||||
area: "${device_area}"
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- "-Os" # optimize for size
|
||||
- "-Wl,--gc-sections" # drop unused code/data
|
||||
- "-fno-exceptions" # strip C++ exceptions
|
||||
- "-fno-rtti" # strip C++ RTTI
|
||||
on_boot:
|
||||
priority: 900 # High priority to run after globals are initialized
|
||||
then:
|
||||
@@ -166,7 +172,7 @@ preferences:
|
||||
flash_write_interval: 5min
|
||||
|
||||
mdns:
|
||||
disabled: false
|
||||
disabled: true
|
||||
|
||||
##########################################################################################
|
||||
# ESPHome Logging Enable
|
||||
@@ -225,13 +231,13 @@ globals:
|
||||
- id: evening_on
|
||||
type: int
|
||||
restore_value: False
|
||||
initial_value: "${morning_off_default}"
|
||||
initial_value: "${evening_on_default}"
|
||||
|
||||
# Evening Off time (minutes from midnight),
|
||||
- id: evening_off
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "${morning_off_default}"
|
||||
restore_value: False
|
||||
initial_value: "${evening_off_default}"
|
||||
|
||||
# Boost Duration (minutes),
|
||||
- id: boost_duration
|
||||
@@ -281,137 +287,124 @@ text_sensor:
|
||||
############################
|
||||
# MQTT Subscriptions
|
||||
############################
|
||||
####################################################
|
||||
# Subscribe to the Morning On time, format "HH:MM"
|
||||
# We check x.size() == 5 and x[2] == ':',
|
||||
# then parse x.substr(0,2) and x.substr(3,2)
|
||||
# std::string uses 'substr', not 'substring'.
|
||||
####################################################
|
||||
############################
|
||||
# Morning On time => "HH:MM"
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning On Time Setting"
|
||||
id: morning_on_topic
|
||||
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
|
||||
internal: True
|
||||
topic: "${mqtt_timer_topic}/morning-on"
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
// Expect "HH:MM" => total length = 5, with ':'
|
||||
if (x.size() == 5 && x[2] == ':') {
|
||||
int hour = atoi(x.substr(0, 2).c_str()); // "HH"
|
||||
int minute = atoi(x.substr(3, 2).c_str()); // "MM"
|
||||
id(morning_on) = hour * 60 + minute;
|
||||
ESP_LOGI("timer","Received new Morning On: %02d:%02d", hour, minute);
|
||||
int h = 0, m = 0;
|
||||
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
|
||||
id(morning_on) = h * 60 + m;
|
||||
ESP_LOGI("timer","Received new Morning On: %02d:%02d", h, m);
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
|
||||
}
|
||||
####################################################
|
||||
|
||||
############################
|
||||
# Morning Off time => "HH:MM"
|
||||
####################################################
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning Off Time Setting"
|
||||
id: morning_off_topic
|
||||
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
topic: "${mqtt_timer_topic}/morning-off"
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
if (x.size() == 5 && x[2] == ':') {
|
||||
int hour = atoi(x.substr(0, 2).c_str());
|
||||
int minute = atoi(x.substr(3, 2).c_str());
|
||||
id(morning_off) = hour * 60 + minute;
|
||||
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", hour, minute);
|
||||
int h = 0, m = 0;
|
||||
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
|
||||
id(morning_off) = h * 60 + m;
|
||||
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", h, m);
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
|
||||
}
|
||||
####################################################
|
||||
|
||||
############################
|
||||
# Evening On time => "HH:MM"
|
||||
####################################################
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening On Time Setting"
|
||||
id: evening_on_topic
|
||||
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
topic: "${mqtt_timer_topic}/evening-on"
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
if (x.size() == 5 && x[2] == ':') {
|
||||
int hour = atoi(x.substr(0, 2).c_str());
|
||||
int minute = atoi(x.substr(3, 2).c_str());
|
||||
id(evening_on) = hour * 60 + minute;
|
||||
ESP_LOGI("timer","Received new Evening On: %02d:%02d", hour, minute);
|
||||
int h = 0, m = 0;
|
||||
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
|
||||
id(evening_on) = h * 60 + m;
|
||||
ESP_LOGI("timer","Received new Evening On: %02d:%02d", h, m);
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
|
||||
}
|
||||
####################################################
|
||||
|
||||
############################
|
||||
# Evening Off time => "HH:MM"
|
||||
####################################################
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening Off Time Setting"
|
||||
id: evening_off_topic
|
||||
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
topic: "${mqtt_timer_topic}/evening-off"
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
if (x.size() == 5 && x[2] == ':') {
|
||||
int hour = atoi(x.substr(0, 2).c_str());
|
||||
int minute = atoi(x.substr(3, 2).c_str());
|
||||
id(evening_off) = hour * 60 + minute;
|
||||
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", hour, minute);
|
||||
int h = 0, m = 0;
|
||||
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
|
||||
id(evening_off) = h * 60 + m;
|
||||
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", h, m);
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
|
||||
}
|
||||
####################################################
|
||||
# Boost duration => 1 - 1439
|
||||
####################################################
|
||||
|
||||
############################
|
||||
# Boost duration => integer minutes (1–1439)
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Boost Duration"
|
||||
id: boost_time_topic
|
||||
topic: "${mqtt_timer_topic}/boost-time" # Stored as an integer from 1-1439
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
topic: "${mqtt_timer_topic}/boost-time"
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
// parse as integer
|
||||
char *endptr;
|
||||
long v = strtol(x.c_str(), &endptr, 10);
|
||||
|
||||
// invalid if nothing parsed, trailing chars, or out of 0–1439
|
||||
if (endptr == x.c_str() || *endptr != '\0' || v < 0 || v > 1439) {
|
||||
ESP_LOGE("boost_time", "Invalid boost_time '%s'", x.c_str());
|
||||
int v = 0;
|
||||
// Parse as integer
|
||||
if (sscanf(x.c_str(), "%d", &v) == 1 && v >= 1 && v <= 1439) {
|
||||
id(boost_duration) = v;
|
||||
ESP_LOGI("boost_time","Received new Boost Duration: %d mins", v);
|
||||
} else {
|
||||
id(boost_duration) = static_cast<int>(v);
|
||||
ESP_LOGW("boost_time","Invalid boost_time '%s'", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Subscribe to operation mode:
|
||||
# OFF, ON, TIMER, BOOST
|
||||
# We do case-insensitive compare using strcasecmp
|
||||
# (Requires <strings.h> typically included in ESPHome)
|
||||
# Subscribe to operation mode: OFF, ON, TIMER, BOOST
|
||||
####################################################
|
||||
# MQTT subscription: set mode, then immediately re-evaluate relay
|
||||
- platform: mqtt_subscribe
|
||||
id: timer_operation_mode_topic
|
||||
topic: "${mqtt_timer_topic}/operation"
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
if (strcasecmp(x.c_str(), "TIMER") == 0) {
|
||||
// Check only the first character for mode
|
||||
char c = x.c_str()[0];
|
||||
if (c == 'T') { // “TIMER”
|
||||
id(operation_mode) = 2;
|
||||
ESP_LOGI("timer","Operation mode set to TIMER");
|
||||
} else if (strcasecmp(x.c_str(), "ON") == 0) {
|
||||
id(operation_mode) = 1;
|
||||
ESP_LOGI("timer","Operation mode set to ON");
|
||||
} else if (strcasecmp(x.c_str(), "OFF") == 0) {
|
||||
id(operation_mode) = 0;
|
||||
ESP_LOGI("timer","Operation mode set to OFF");
|
||||
} else if (strcasecmp(x.c_str(), "BOOST") == 0) {
|
||||
} else if (c == 'O') { // “ON” or “OFF”
|
||||
// second letter N→ON, F→OFF
|
||||
id(operation_mode) = (x.size() > 1 && x[1] == 'N') ? 1 : 0;
|
||||
} else if (c == 'B') { // “BOOST”
|
||||
id(operation_mode) = 3;
|
||||
id(boost_timer) = 0;
|
||||
ESP_LOGI("timer","Operation mode set to BOOST");
|
||||
id(boost_timer) = 0;
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
|
||||
ESP_LOGW("timer","Invalid mode: %s", x.c_str());
|
||||
}
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
|
Reference in New Issue
Block a user