#ifdef FIRMWARE_VARIANT_THERMOSTAT

float thermostat_setTemp_lastPublished;
float thermostat_currSetTemp_lastPublished;
int thermostat_heatingMode_lastPublished;
int thermostat_preset_lastPublished;
bool thermostat_turnHeatingOn_lastPublished;


void thermostat_publishCurrentValues(bool force) {
//void thermostat_publishCurrentValues(bool force = false) {
  // most values are only published if changed
  // call publishCurrentThermostatValues(true); to force publishing an update
  char _tmp_topic_out[50];
  char _ch_valuebuf[7];
  
  thermostat_updateCurrentHeatingModeName();
  thermostat_updateCurrentPresetName();

  char logBuf[101];
  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);
  sendLog(logBuf, LOGLEVEL_INFO);

  if (force || !confTherm.saveToMqttRetained || thermostat_setTemp != thermostat_setTemp_lastPublished)
  {
    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "setTemp");
    sprintf(_ch_valuebuf, "%2.1f", thermostat_setTemp);
    mqttclient.publish(_tmp_topic_out, _ch_valuebuf, confMqtt.mqtt_outRetain);

    if (confTherm.saveToMqttRetained && thermostat_setTemp != thermostat_setTemp_lastPublished)
    {
      // MQTT retained save setTemp
      mqttclient.publish(mqtt_topic_in_setTemp, _ch_valuebuf, true);
      char buf1[30];
      sprintf_P(buf1, "%s %s %2.1f", PGMStr_mqttRetainedSave, PGMStr_setTemp, thermostat_setTemp);
      sendLog(buf1);
    }

    thermostat_setTemp_lastPublished = thermostat_setTemp;
    yield();
  }

  if (force || thermostat_currSetTemp != thermostat_currSetTemp_lastPublished || !confMqtt.mqtt_outRetain)
  {
    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "currSetTemp");
    sprintf(_ch_valuebuf, "%2.1f", thermostat_currSetTemp);
    mqttclient.publish(_tmp_topic_out, _ch_valuebuf, confMqtt.mqtt_outRetain); 

    thermostat_currSetTemp_lastPublished = thermostat_currSetTemp;
    yield();
  }

  if (force || !confTherm.saveToMqttRetained || thermostat_heatingMode != thermostat_heatingMode_lastPublished)
  {
    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "mode");
    char _ch_heatingMode[3];
    sprintf(_ch_heatingMode, "%d", thermostat_heatingMode);
    mqttclient.publish(_tmp_topic_out, _ch_heatingMode, confMqtt.mqtt_outRetain);

    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "modeName");
    mqttclient.publish(_tmp_topic_out, thermostat_currentModeName, confMqtt.mqtt_outRetain);


    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "modeHA");
    if(thermostat_heatingMode == 0) mqttclient.publish(_tmp_topic_out, "off", confMqtt.mqtt_outRetain);
    else if(thermostat_heatingMode == 1) mqttclient.publish(_tmp_topic_out, "heat", confMqtt.mqtt_outRetain);

    if (confTherm.saveToMqttRetained && thermostat_heatingMode != thermostat_heatingMode_lastPublished)
    {
      // MQTT retained save setMode
      mqttclient.publish(mqtt_topic_in_setMode, _ch_heatingMode, true);
      char buf1[30];
      sprintf_P(buf1, "%s %s %u", PGMStr_mqttRetainedSave, PGMStr_heatingMode, thermostat_heatingMode);
      sendLog(buf1);
    }

    thermostat_heatingMode_lastPublished = thermostat_heatingMode;
    yield();
  }

  if (force || !confTherm.saveToMqttRetained || thermostat_preset != thermostat_preset_lastPublished)
  {
    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "preset");
    char _ch_preset[3];
    sprintf(_ch_preset, "%d", thermostat_preset);
    mqttclient.publish(_tmp_topic_out, _ch_preset, confMqtt.mqtt_outRetain);

    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "presetName");
    mqttclient.publish(_tmp_topic_out, thermostat_currentPresetName, confMqtt.mqtt_outRetain);

    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "presetHA");
    if (thermostat_preset == 0)
      mqttclient.publish(_tmp_topic_out, "norm", confMqtt.mqtt_outRetain);
    else if (thermostat_preset == 1)
      mqttclient.publish(_tmp_topic_out, "red1", confMqtt.mqtt_outRetain);
    else if (thermostat_preset == 2)
      mqttclient.publish(_tmp_topic_out, "red2", confMqtt.mqtt_outRetain);

    if (confTherm.saveToMqttRetained && thermostat_preset != thermostat_preset_lastPublished)
    {
      // MQTT retained save setPreset
      mqttclient.publish(mqtt_topic_in_setPreset, _ch_preset, true);
      char buf1[30];
      sprintf_P(buf1, "%s %s %u", PGMStr_mqttRetainedSave, PGMStr_preset, thermostat_preset);
      sendLog(buf1);
    }

    thermostat_preset_lastPublished = thermostat_preset;
    yield();
  }

  yield();

  // turnHeatingOn
  char _ch_turnHeatingOn[5];
  unsigned int _currOnOffTime;
  if (thermostat_turnHeatingOn)
  {
    strcpy(_ch_turnHeatingOn, "on");
    _currOnOffTime = thermostat_heatingOnTime;
  }
  else
  {
    strcpy(_ch_turnHeatingOn, "off");
    _currOnOffTime = thermostat_heatingOffTime;
  }
  if (force || thermostat_turnHeatingOn != thermostat_turnHeatingOn_lastPublished)
  {
    sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heating");
    mqttclient.publish(_tmp_topic_out, _ch_turnHeatingOn, confMqtt.mqtt_outRetain);
    thermostat_turnHeatingOn_lastPublished = thermostat_turnHeatingOn;
  }

  //sendLog(F("heating ON"));
  //char logBuf[40];

  sprintf_P(logBuf, "%s: %s=%s (%s)", PGMStr_thermostat, PGMStr_heating, _ch_turnHeatingOn, getTimeStringFromSeconds(_currOnOffTime));
  sendLog(logBuf, LOGLEVEL_INFO);
  // END turnHeatingOn

  yield();

  char _buf[21];

  sprintf(_buf, "%lu", thermostat_heatingOnTime);
  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime");
  mqttclient.publish(_tmp_topic_out, _buf);

  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime_Hms");
  mqttclient.publish(_tmp_topic_out, getTimeStringFromSeconds(thermostat_heatingOnTime));
  yield();

  sprintf(_buf, "%lu", thermostat_heatingOffTime);
  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime");
  mqttclient.publish(_tmp_topic_out, _buf);
  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime_Hms");
  mqttclient.publish(_tmp_topic_out, getTimeStringFromSeconds(thermostat_heatingOffTime));

  thermostat_publish_heatingLockTime();
  thermostat_publish_heatingPauseTime();  

  yield();
}

void thermostat_publish_heatingLockTime() {
  char _tmp_topic_out[50];
  char _buf[21];
  sprintf(_buf, "%i", thermostat_heatingLockTime);
  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingLockTime");
  mqttclient.publish(_tmp_topic_out, _buf);
}

void thermostat_publish_heatingPauseTime() {
  char _tmp_topic_out[50];
  char _buf[21];
  sprintf(_buf, "%i", thermostat_heatingPause);
  sprintf(_tmp_topic_out, "%s/%s", confMqtt.mqtt_topic_out, "heatingPause");
  mqttclient.publish(_tmp_topic_out, _buf);
}

#endif
