Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
optimize thermostat modes - #138
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Mar 8, 2020
1 parent b8fb03a commit c55ed82
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- External sensors (like Dallas DS18*) are sent as a nested MQTT topic including their unqiue identifier
- `mqttlog` console command renamed to `mqttqueue` to only show the current publish queue
- `status` payload on start-up shows the IP and Version of EMS-ESP
- `thermostat mode` takes a string like manual,auto,heat,day,night,eco,comfort,holiday,nofrost

### Removed
- `autodetect scan`
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default_envs = release
; -DFORCE_SERIAL
; -DMYESP_DEBUG
; -DSENSOR_MQTT_USEID
;custom_flags = -DFORCE_SERIAL -DMYESP_DEBUG
;custom_flags = -DFORCE_SERIAL -DMYESP_DEBUG -DEMSESP_SIMULATE
custom_flags =

# Available lwIP variants (macros):
Expand Down
41 changes: 15 additions & 26 deletions src/ems-esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ static const command_t project_cmds[] PROGMEM = {
{false, "queue", "show current Tx queue"},
{false, "send XX ...", "send raw telegram data to EMS bus (XX are hex values)"},
{false, "thermostat read <type ID>", "send read request to the thermostat for heating circuit hc 1-4"},
{false, "thermostat temp <degrees> [mode] [hc]", "set current thermostat temperature. mode=0-6 (see wiki), hc=1-4"},
{false, "thermostat mode <hc> <mode>", "set mode (0=off, 1=manual, 2=auto) for heating circuit hc 1-4"},
{false, "thermostat temp <degrees> [mode] [hc]", "set current thermostat temperature. mode=0-6 (see wiki) for hc=1-4"},
{false, "thermostat mode <mode> [hc]", "set mode (manual,auto,heat,day,night,eco,comfort,holiday,nofrost)"},
{false, "boiler read <type ID>", "send read request to boiler"},
{false, "boiler wwtemp <degrees>", "set boiler warm water temperature"},
{false, "boiler wwactive <on | off>", "set boiler warm water on/off"},
Expand Down Expand Up @@ -1618,14 +1618,15 @@ void TelnetCommandCallback(uint8_t wc, const char * commandLine) {
ok = true;
} else {
// get modevalue and heatingcircuit
_THERMOSTAT_TEMP_MODE temp_mode = (_THERMOSTAT_TEMP_MODE)_readIntNumber(); // next parameter is the temp mode type
uint8_t hc = _readIntNumber(); // next parameter is the heating circuit
_EMS_THERMOSTAT_MODE temp_mode = (_EMS_THERMOSTAT_MODE)_readIntNumber(); // next parameter is the temp mode type
uint8_t hc = _readIntNumber(); // next parameter is the heating circuit
ems_setThermostatTemp(temp, hc, temp_mode);
ok = true;
}
} else if (strcmp(second_cmd, "mode") == 0) {
uint8_t hc = _readIntNumber(); // next parameter is the heating circuit
ems_setThermostatMode(_readIntNumber(), hc);
char * mode_s = _readWord();
uint8_t hc = (wc == 3) ? EMS_THERMOSTAT_DEFAULTHC : _readIntNumber();
ems_setThermostatMode(mode_s, hc);
ok = true;
} else if (strcmp(second_cmd, "read") == 0) {
ems_doReadCommand(_readHexNumber(), EMS_Thermostat.device_id);
Expand Down Expand Up @@ -1951,13 +1952,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_MODE_HA, topic);
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
if (strncmp(message, "auto", 4) == 0) {
ems_setThermostatMode(2, hc);
} else if ((strncmp(message, "day", 4) == 0) || (strncmp(message, "manual", 6) == 0) || (strncmp(message, "heat", 4) == 0)) {
ems_setThermostatMode(1, hc);
} else if ((strncmp(message, "night", 5) == 0) || (strncmp(message, "off", 3) == 0)) {
ems_setThermostatMode(0, hc);
}
ems_setThermostatMode(message, hc);
return;
}
}
Expand Down Expand Up @@ -1997,13 +1992,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (data_cmd == nullptr) {
return;
}
if (strncmp(data_cmd, "auto", 4) == 0) {
ems_setThermostatMode(2, hc);
} else if ((strncmp(data_cmd, "day", 4) == 0) || (strncmp(data_cmd, "manual", 6) == 0) || (strncmp(data_cmd, "heat", 4) == 0)) {
ems_setThermostatMode(1, hc);
} else if ((strncmp(data_cmd, "night", 5) == 0) || (strncmp(data_cmd, "off", 3) == 0)) {
ems_setThermostatMode(0, hc);
}
ems_setThermostatMode(data_cmd, hc);
return;
}
}
Expand All @@ -2014,7 +2003,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_NIGHT); // night
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_NIGHT); // night
return;
}
}
Expand All @@ -2026,7 +2015,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_DAY); // day
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_DAY); // day
}
return;
}
Expand All @@ -2038,7 +2027,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_HOLIDAY); // holiday
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_HOLIDAY); // holiday
}
return;
}
Expand All @@ -2050,7 +2039,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_ECO); // holiday
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_ECO); // holiday
}
return;
}
Expand All @@ -2062,7 +2051,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_HEAT); // holiday
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_HEAT); // holiday
}
return;
}
Expand All @@ -2074,7 +2063,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, THERMOSTAT_TEMP_MODE_NOFROST); // holiday
ems_setThermostatTemp(f, hc, EMS_THERMOSTAT_MODE_NOFROST); // holiday
}
return;
}
Expand Down
150 changes: 117 additions & 33 deletions src/ems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,7 @@ void ems_sendRawTelegram(char * telegram) {
* hc_num is 1 to 4
* temptype 0=normal, 1=night temp, 2=day temp, 3=holiday temp
*/
void ems_setThermostatTemp(float temperature, uint8_t hc_num, _THERMOSTAT_TEMP_MODE temptype) {
void ems_setThermostatTemp(float temperature, uint8_t hc_num, _EMS_THERMOSTAT_MODE temptype) {
if (!ems_getThermostatEnabled()) {
myDebug_P(PSTR("Thermostat not online."));
return;
Expand Down Expand Up @@ -2425,17 +2425,17 @@ void ems_setThermostatTemp(float temperature, uint8_t hc_num, _THERMOSTAT_TEMP_M

else if ((model == EMS_DEVICE_FLAG_RC35) || (model == EMS_DEVICE_FLAG_RC30N)) {
switch (temptype) {
case THERMOSTAT_TEMP_MODE_NIGHT: // change the night temp
case EMS_THERMOSTAT_MODE_NIGHT: // change the night temp
EMS_TxTelegram.offset = EMS_OFFSET_RC35Set_temp_night;
break;
case THERMOSTAT_TEMP_MODE_DAY: // change the day temp
case EMS_THERMOSTAT_MODE_DAY: // change the day temp
EMS_TxTelegram.offset = EMS_OFFSET_RC35Set_temp_day;
break;
case THERMOSTAT_TEMP_MODE_HOLIDAY: // change the holiday temp
case EMS_THERMOSTAT_MODE_HOLIDAY: // change the holiday temp
EMS_TxTelegram.offset = EMS_OFFSET_RC35Set_temp_holiday;
break;
default:
case THERMOSTAT_TEMP_MODE_AUTO: // automatic selection, if no type is defined, we use the standard code
case EMS_THERMOSTAT_MODE_AUTO: // automatic selection, if no type is defined, we use the standard code
if (model == EMS_DEVICE_FLAG_RC35) {
EMS_TxTelegram.offset = EMS_OFFSET_RC35Set_seltemp; // https://github.com/proddy/EMS-ESP/issues/310
} else {
Expand Down Expand Up @@ -2468,17 +2468,17 @@ void ems_setThermostatTemp(float temperature, uint8_t hc_num, _THERMOSTAT_TEMP_M
// see https://github.com/proddy/EMS-ESP/issues/335#issuecomment-593324716)
if (model == EMS_DEVICE_FLAG_JUNKERS1) {
switch (temptype) {
case THERMOSTAT_TEMP_MODE_NOFROST:
case EMS_THERMOSTAT_MODE_NOFROST:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage_no_frost_temp;
break;
case THERMOSTAT_TEMP_MODE_NIGHT:
case EMS_THERMOSTAT_MODE_NIGHT:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage_night_temp;
break;
case THERMOSTAT_TEMP_MODE_DAY:
case EMS_THERMOSTAT_MODE_DAY:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage_day_temp;
break;
default:
case THERMOSTAT_TEMP_MODE_AUTO: // automatic selection, if no type is defined, we use the standard code
case EMS_THERMOSTAT_MODE_AUTO: // automatic selection, if no type is defined, we use the standard code
EMS_TxTelegram.offset =
(EMS_Thermostat.hc[hc_num - 1].mode_type == 0) ? EMS_OFFSET_JunkersSetMessage_night_temp : EMS_OFFSET_JunkersSetMessage_day_temp;
break;
Expand All @@ -2488,14 +2488,14 @@ void ems_setThermostatTemp(float temperature, uint8_t hc_num, _THERMOSTAT_TEMP_M
} else {
// EMS_DEVICE_FLAG_JUNKERS2
switch (temptype) {
case THERMOSTAT_TEMP_MODE_NOFROST:
case EMS_THERMOSTAT_MODE_NOFROST:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage2_no_frost_temp;
break;
case THERMOSTAT_TEMP_MODE_ECO:
case EMS_THERMOSTAT_MODE_ECO:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage2_eco_temp;
break;
default:
case THERMOSTAT_TEMP_MODE_HEAT:
case EMS_THERMOSTAT_MODE_HEAT:
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage3_heat;
break;
}
Expand All @@ -2515,13 +2515,38 @@ void ems_setThermostatTemp(float temperature, uint8_t hc_num, _THERMOSTAT_TEMP_M
EMS_TxQueue.push(EMS_TxTelegram);
}

// wrapper for setting thermostat mode, taking a string as an argument
void ems_setThermostatMode(const char * mode_s, uint8_t hc_num) {
if (strncmp(mode_s, "auto", 4) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_AUTO, hc_num);
} else if (strncmp(mode_s, "day", 3) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_DAY, hc_num);
} else if (strncmp(mode_s, "manual", 6) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_MANUAL, hc_num);
} else if (strncmp(mode_s, "heat", 4) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_HEAT, hc_num);
} else if (strncmp(mode_s, "night", 5) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_NIGHT, hc_num);
} else if (strncmp(mode_s, "off", 3) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_OFF, hc_num);
} else if (strncmp(mode_s, "comfort", 7) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_COMFORT, hc_num);
} else if (strncmp(mode_s, "holiday", 7) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_HOLIDAY, hc_num);
} else if (strncmp(mode_s, "nofrost", 7) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_NOFROST, hc_num);
} else if (strncmp(mode_s, "eco", 3) == 0) {
ems_setThermostatMode(EMS_THERMOSTAT_MODE_ECO, hc_num);
}
}

/**
* Set the thermostat working mode
* 0xA8 on a RC20 and 0xA7 on RC30
* 0x01B9 for EMS+ 300/1000/3000, Auto=0xFF Manual=0x00. See https://github.com/proddy/EMS-ESP/wiki/RC3xx-Thermostats
* hc_num is 1 to 4
*/
void ems_setThermostatMode(uint8_t mode, uint8_t hc_num) {
void ems_setThermostatMode(_EMS_THERMOSTAT_MODE mode, uint8_t hc_num) {
if (!ems_getThermostatEnabled()) {
myDebug_P(PSTR("Thermostat not online."));
return;
Expand All @@ -2537,38 +2562,95 @@ void ems_setThermostatMode(uint8_t mode, uint8_t hc_num) {
return;
}

uint8_t model = ems_getThermostatFlags();
uint8_t device_id = EMS_Thermostat.device_id;
uint8_t set_mode;

// RC300/1000/3000 have different settings
if ((model == EMS_DEVICE_FLAG_RC300) || (model == EMS_DEVICE_FLAG_RC100)) {
if (mode == 1) {
set_mode = 0; // manual
// set the value to send via EMS depending on the mode type
uint8_t set_mode_value = 0x02; // default is 2 which is usually auto
char mode_str[20];
switch (mode) {
case EMS_THERMOSTAT_MODE_AUTO:
strlcpy(mode_str, "auto", sizeof(mode_str));
set_mode_value = 2;
break;
case EMS_THERMOSTAT_MODE_NIGHT:
strlcpy(mode_str, "night", sizeof(mode_str));
set_mode_value = 0;
break;
case EMS_THERMOSTAT_MODE_DAY:
strlcpy(mode_str, "day", sizeof(mode_str));
set_mode_value = 1;
break;
case EMS_THERMOSTAT_MODE_HOLIDAY:
strlcpy(mode_str, "holiday", sizeof(mode_str));
set_mode_value = 2; // auto
break;
case EMS_THERMOSTAT_MODE_NOFROST:
strlcpy(mode_str, "nofrost", sizeof(mode_str));
set_mode_value = 1;
break;
case EMS_THERMOSTAT_MODE_ECO:
strlcpy(mode_str, "eco", sizeof(mode_str));
set_mode_value = 2; // same as auto
break;
case EMS_THERMOSTAT_MODE_HEAT:
strlcpy(mode_str, "heat", sizeof(mode_str));
set_mode_value = 1;
break;
case EMS_THERMOSTAT_MODE_OFF:
strlcpy(mode_str, "off", sizeof(mode_str));
set_mode_value = 0;
break;
case EMS_THERMOSTAT_MODE_MANUAL:
strlcpy(mode_str, "manual", sizeof(mode_str));
set_mode_value = 1;
break;
case EMS_THERMOSTAT_MODE_UNKNOWN:
default:
strlcpy(mode_str, "unknown", sizeof(mode_str));
set_mode_value = 2; // auto
break;
}

// now override the mode value setting depending on the thermostat type
// handle the different mode values to send per thermostat type
uint8_t model = ems_getThermostatFlags();
switch (model) {
case EMS_DEVICE_FLAG_RC300:
case EMS_DEVICE_FLAG_RC100:
if (mode == EMS_THERMOSTAT_MODE_AUTO) {
set_mode_value = 0xFF; // special value for auto
} else {
set_mode = 0xFF; // auto
set_mode_value = 0; // everything else, like manual/day etc..
}
} else {
set_mode = mode;
break;
case EMS_DEVICE_FLAG_JUNKERS1:
case EMS_DEVICE_FLAG_JUNKERS2:
if (mode == EMS_THERMOSTAT_MODE_NOFROST) {
set_mode_value = 0x01;
} else if (mode == EMS_THERMOSTAT_MODE_ECO) {
set_mode_value = 0x02;
} else if ((mode == EMS_THERMOSTAT_MODE_DAY) || (mode == EMS_THERMOSTAT_MODE_HEAT)) {
set_mode_value = 0x03; // comfort
} else if (mode == EMS_THERMOSTAT_MODE_AUTO) {
set_mode_value = 0x04;
}
break;
default:
break;
}

// 0=off, 1=manual, 2=auto
if (mode == 0) {
myDebug_P(PSTR("Setting thermostat mode to off for heating circuit %d"), hc_num);
} else if (set_mode == 1) {
myDebug_P(PSTR("Setting thermostat mode to manual for heating circuit %d"), hc_num);
} else if (set_mode == 2) {
myDebug_P(PSTR("Setting thermostat mode to auto for heating circuit %d"), hc_num);
if (hc_num == 1) {
myDebug_P(PSTR("Setting thermostat mode to %s"), mode_str);
} else {
myDebug_P(PSTR("Setting thermostat mode to %s for heating circuit %d"), mode_str, hc_num);
}

_EMS_TxTelegram EMS_TxTelegram = EMS_TX_TELEGRAM_NEW; // create new Tx
EMS_TxTelegram.timestamp = millis(); // set timestamp
EMS_Sys_Status.txRetryCount = 0; // reset retry counter

EMS_TxTelegram.action = EMS_TX_TELEGRAM_WRITE;
EMS_TxTelegram.dest = device_id;
EMS_TxTelegram.dest = EMS_Thermostat.device_id;
EMS_TxTelegram.length = EMS_MIN_TELEGRAM_LENGTH;
EMS_TxTelegram.dataValue = set_mode;
EMS_TxTelegram.dataValue = set_mode_value;

// handle different thermostat types
if (model == EMS_DEVICE_FLAG_RC20) {
Expand Down Expand Up @@ -2602,11 +2684,13 @@ void ems_setThermostatMode(uint8_t mode, uint8_t hc_num) {

// Junkers
} else if (model == EMS_DEVICE_FLAG_JUNKERS1) {
EMS_TxTelegram.emsplus = true; // Assuming here that all Junkers use EMS+
EMS_TxTelegram.type = EMS_TYPE_JunkersSetMessage1_HC1 + hc_num - 1;
EMS_TxTelegram.comparisonPostRead = EMS_TYPE_JunkersStatusMessage_HC1;
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage_set_mode;
EMS_TxTelegram.type_validate = EMS_TxTelegram.type;
} else if (model == EMS_DEVICE_FLAG_JUNKERS2) {
EMS_TxTelegram.emsplus = true; // Assuming here that all Junkers use EMS+
EMS_TxTelegram.type = EMS_TYPE_JunkersSetMessage2_HC1 + hc_num - 1;
EMS_TxTelegram.comparisonPostRead = EMS_TYPE_JunkersStatusMessage_HC1;
EMS_TxTelegram.offset = EMS_OFFSET_JunkersSetMessage_set_mode;
Expand Down
Loading

0 comments on commit c55ed82

Please sign in to comment.