thermostat_mqtt.ino 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  2. float thermostat_setTemp_lastPublished;
  3. float thermostat_currSetTemp_lastPublished;
  4. int thermostat_heatingMode_lastPublished;
  5. int thermostat_preset_lastPublished;
  6. bool thermostat_turnHeatingOn_lastPublished;
  7. void thermostat_publishCurrentValues(bool force) {
  8. //void thermostat_publishCurrentValues(bool force = false) {
  9. // most values are only published if changed
  10. // call publishCurrentThermostatValues(true); to force publishing an update
  11. char _tmp_topic_out[50];
  12. char _ch_valuebuf[7];
  13. thermostat_updateCurrentHeatingModeName();
  14. thermostat_updateCurrentPresetName();
  15. char logBuf[101];
  16. sprintf_P(logBuf, "%s: %s=%u, %s=%u, %s=%2.1f, %s=%2.1f", PGMStr_thermostat, PGMStr_heatingMode, thermostat_heatingMode, PGMStr_preset, thermostat_preset, PGMStr_setTemp, thermostat_setTemp, PGMStr_currentSetTemp, thermostat_currSetTemp);
  17. sendLog(logBuf, LOGLEVEL_INFO);
  18. if (force || !confTherm.saveToMqttRetained || thermostat_setTemp != thermostat_setTemp_lastPublished)
  19. {
  20. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "setTemp");
  21. sprintf(_ch_valuebuf, "%2.1f", thermostat_setTemp);
  22. mqttclient.publish(_tmp_topic_out, _ch_valuebuf, confMqtt.mqtt_outRetain);
  23. if (confTherm.saveToMqttRetained && thermostat_setTemp != thermostat_setTemp_lastPublished)
  24. {
  25. // MQTT retained save setTemp
  26. mqttclient.publish(mqtt_topic_in_setTemp, _ch_valuebuf, true);
  27. char buf1[30];
  28. sprintf_P(buf1, "%s %s %2.1f", PGMStr_mqttRetainedSave, PGMStr_setTemp, thermostat_setTemp);
  29. sendLog(buf1);
  30. }
  31. thermostat_setTemp_lastPublished = thermostat_setTemp;
  32. yield();
  33. }
  34. if (force || thermostat_currSetTemp != thermostat_currSetTemp_lastPublished || !confMqtt.mqtt_outRetain)
  35. {
  36. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "currSetTemp");
  37. sprintf(_ch_valuebuf, "%2.1f", thermostat_currSetTemp);
  38. mqttclient.publish(_tmp_topic_out, _ch_valuebuf, confMqtt.mqtt_outRetain);
  39. thermostat_currSetTemp_lastPublished = thermostat_currSetTemp;
  40. yield();
  41. }
  42. if (force || !confTherm.saveToMqttRetained || thermostat_heatingMode != thermostat_heatingMode_lastPublished)
  43. {
  44. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "mode");
  45. char _ch_heatingMode[3];
  46. sprintf(_ch_heatingMode, "%d", thermostat_heatingMode);
  47. mqttclient.publish(_tmp_topic_out, _ch_heatingMode, confMqtt.mqtt_outRetain);
  48. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "modeName");
  49. mqttclient.publish(_tmp_topic_out, thermostat_currentModeName, confMqtt.mqtt_outRetain);
  50. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "modeHA");
  51. if(thermostat_heatingMode == 0) mqttclient.publish(_tmp_topic_out, "off", confMqtt.mqtt_outRetain);
  52. else if(thermostat_heatingMode == 1) mqttclient.publish(_tmp_topic_out, "heat", confMqtt.mqtt_outRetain);
  53. if (confTherm.saveToMqttRetained && thermostat_heatingMode != thermostat_heatingMode_lastPublished)
  54. {
  55. // MQTT retained save setMode
  56. mqttclient.publish(mqtt_topic_in_setMode, _ch_heatingMode, true);
  57. char buf1[30];
  58. sprintf_P(buf1, "%s %s %u", PGMStr_mqttRetainedSave, PGMStr_heatingMode, thermostat_heatingMode);
  59. sendLog(buf1);
  60. }
  61. thermostat_heatingMode_lastPublished = thermostat_heatingMode;
  62. yield();
  63. }
  64. if (force || !confTherm.saveToMqttRetained || thermostat_preset != thermostat_preset_lastPublished)
  65. {
  66. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "preset");
  67. char _ch_preset[3];
  68. sprintf(_ch_preset, "%d", thermostat_preset);
  69. mqttclient.publish(_tmp_topic_out, _ch_preset, confMqtt.mqtt_outRetain);
  70. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "presetName");
  71. mqttclient.publish(_tmp_topic_out, thermostat_currentPresetName, confMqtt.mqtt_outRetain);
  72. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "presetHA");
  73. if (thermostat_preset == 0)
  74. mqttclient.publish(_tmp_topic_out, "norm", confMqtt.mqtt_outRetain);
  75. else if (thermostat_preset == 1)
  76. mqttclient.publish(_tmp_topic_out, "red1", confMqtt.mqtt_outRetain);
  77. else if (thermostat_preset == 2)
  78. mqttclient.publish(_tmp_topic_out, "red2", confMqtt.mqtt_outRetain);
  79. if (confTherm.saveToMqttRetained && thermostat_preset != thermostat_preset_lastPublished)
  80. {
  81. // MQTT retained save setPreset
  82. mqttclient.publish(mqtt_topic_in_setPreset, _ch_preset, true);
  83. char buf1[30];
  84. sprintf_P(buf1, "%s %s %u", PGMStr_mqttRetainedSave, PGMStr_preset, thermostat_preset);
  85. sendLog(buf1);
  86. }
  87. thermostat_preset_lastPublished = thermostat_preset;
  88. yield();
  89. }
  90. yield();
  91. // turnHeatingOn
  92. char _ch_turnHeatingOn[5];
  93. unsigned int _currOnOffTime;
  94. if (thermostat_turnHeatingOn)
  95. {
  96. strcpy(_ch_turnHeatingOn, "on");
  97. _currOnOffTime = thermostat_heatingOnTime;
  98. }
  99. else
  100. {
  101. strcpy(_ch_turnHeatingOn, "off");
  102. _currOnOffTime = thermostat_heatingOffTime;
  103. }
  104. if (force || thermostat_turnHeatingOn != thermostat_turnHeatingOn_lastPublished)
  105. {
  106. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heating");
  107. mqttclient.publish(_tmp_topic_out, _ch_turnHeatingOn, confMqtt.mqtt_outRetain);
  108. thermostat_turnHeatingOn_lastPublished = thermostat_turnHeatingOn;
  109. }
  110. //sendLog(F("heating ON"));
  111. //char logBuf[40];
  112. sprintf_P(logBuf, "%s: %s=%s (%s)", PGMStr_thermostat, PGMStr_heating, _ch_turnHeatingOn, getTimeStringFromSeconds(_currOnOffTime));
  113. sendLog(logBuf, LOGLEVEL_INFO);
  114. // END turnHeatingOn
  115. yield();
  116. char _buf[21];
  117. sprintf(_buf, "%lu", thermostat_heatingOnTime);
  118. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime");
  119. mqttclient.publish(_tmp_topic_out, _buf);
  120. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime_Hms");
  121. mqttclient.publish(_tmp_topic_out, getTimeStringFromSeconds(thermostat_heatingOnTime));
  122. yield();
  123. sprintf(_buf, "%lu", thermostat_heatingOffTime);
  124. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime");
  125. mqttclient.publish(_tmp_topic_out, _buf);
  126. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime_Hms");
  127. mqttclient.publish(_tmp_topic_out, getTimeStringFromSeconds(thermostat_heatingOffTime));
  128. thermostat_publish_heatingLockTime();
  129. thermostat_publish_heatingPauseTime();
  130. yield();
  131. }
  132. void thermostat_publish_heatingLockTime() {
  133. char _tmp_topic_out[50];
  134. char _buf[21];
  135. sprintf(_buf, "%i", thermostat_heatingLockTime);
  136. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingLockTime");
  137. mqttclient.publish(_tmp_topic_out, _buf);
  138. }
  139. void thermostat_publish_heatingPauseTime() {
  140. char _tmp_topic_out[50];
  141. char _buf[21];
  142. sprintf(_buf, "%i", thermostat_heatingPause);
  143. sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingPause");
  144. mqttclient.publish(_tmp_topic_out, _buf);
  145. }
  146. #endif