

float currTemp_lastPublished;
int currHum_lastPublished;


void publishCurrentSensorValues(bool force = false)
{
  if (sensor_DHT_lastUpdate != 0 && (millis() - sensor_DHT_lastUpdate) < 120000)
  { // ensure values are not too old
    char _tmp_topic_out[50];
    char _temp_chararr[6];
    char _hum_chararr[4];

    #ifdef ENABLE_SENSOR_DHT22
      
      dtostrf(sensor_DHT_currTemp, 1, 1, _temp_chararr);
      sprintf(_hum_chararr, "%2i", sensor_DHT_currHum);
  
      // if (serialdebug)
      // {
      //   Serial.print(F("sensors: {"));
      //   Serial.print("'temp':");
      //   Serial.print(_temp_chararr);
      // }
  
      if (force || sensor_DHT_currTemp != currTemp_lastPublished)
      {
        snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
        mqttclient.publish(_tmp_topic_out, _temp_chararr, confMqtt.mqtt_outRetain_sensors);
        currTemp_lastPublished = sensor_DHT_currTemp;
        yield();
      }
  
      // if (serialdebug)
      // {
      //   Serial.print(F(",'hum':"));
      //   Serial.print(sensor_DHT_currHum);
      // }
  
      if (force || sensor_DHT_currHum != currHum_lastPublished)
      {
        snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
        mqttclient.publish(_tmp_topic_out, _hum_chararr, confMqtt.mqtt_outRetain_sensors);
        yield();
      }
  
      dtostrf(sensor_DHT_currTemp_raw, 1, 1, _temp_chararr);
      sprintf(_hum_chararr, "%2i", sensor_DHT_currHum_raw);
  
      // if (serialdebug)
      // {
      //   Serial.print(F(",'temp_raw':"));
      //   Serial.print(_temp_chararr);
      // }
      snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_raw");
      mqttclient.publish(_tmp_topic_out, _temp_chararr);
      yield();
      // if (serialdebug)
      // {
      //   Serial.print(F(",'hum_raw':"));
      //   Serial.print(sensor_DHT_currHum_raw);
      // }
      snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum_raw");
      mqttclient.publish(_tmp_topic_out, _hum_chararr);
      yield();
    #endif // ENABLE_SENSOR_DHT22



    #ifdef ENABLE_SENSORS_ONEWIRE

      if (owTemp_feed > -50) {
        snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_feed");
        dtostrf(owTemp_feed, 1, 0, _temp_chararr);
        mqttclient.publish(_tmp_topic_out, _temp_chararr);
        yield();
      }

      if (owTemp_return > -50) {
        snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_return");
        dtostrf(owTemp_return, 1, 0, _temp_chararr);
        mqttclient.publish(_tmp_topic_out, _temp_chararr);
        yield();
      }

      if (owTemp_out > -50) {
        snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_out");
        dtostrf(owTemp_out, 1, 0, _temp_chararr);
        mqttclient.publish(_tmp_topic_out, _temp_chararr);
        yield();
      }

    #endif // ENABLE_SENSORS_ONEWIRE


    // if (serialdebug)
    //   Serial.println("}");
  }
}

void publishCurrentPIRValue()
{
  char _tmp_topic_out[50];
  snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "PIR");

  // PIR internal topic
  if (PIRSensorOn)
  {
    mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_on);
    sendLog("SENS: PIR=ON", LOGLEVEL_INFO);
  }
  else
  {
    mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_off);
    sendLog("SENS: PIR=OFF", LOGLEVEL_INFO);
  }

  // PIR additional topic
  if (strlen(confAdd.mqtt_topic_pir) >= 4)
  {
    if (PIRSensorOn)
    {
      mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_on);
    }
    else
    {
      mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_off);
    }
  }
}

void publishDeleteRetainedSavedStates()
{
  // after "save states to MQTT retained" is switched off
  // old retained messages are deleted by publishing an empty retained message to the topics
  // to be called from config when confTherm.saveToMqttRetained was changed to false
  #ifdef FIRMWARE_VARIANT_THERMOSTAT
  if (!confTherm.saveToMqttRetained)
  {
    mqttclient.publish(mqtt_topic_in_setTemp, "", true);
    mqttclient.publish(mqtt_topic_in_setMode, "", true);
    mqttclient.publish(mqtt_topic_in_setPreset, "", true);
    thermostat_publishCurrentValues(true); // force publish current values again
    

    sendLog("MQTT: [Thermostat] deleted retained saved states", LOGLEVEL_INFO);
  }
  #endif
}

void publishDeleteRetainedOutMessages()
{
  // after "MQTT-out retained" is switched off
  // old retained messages are deleted by publishing an empty retained message to the topics
  // to be called from config when confMqtt.mqtt_outRetain was changed to false
  if (!confMqtt.mqtt_outRetain)
  {

    #ifdef FIRMWARE_VARIANT_THERMOSTAT
    char _tmp_topic_out[50];
    
    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "setTemp");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "currSetTemp");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "mode");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeName");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeHA");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "preset");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetName");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetHA");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heating");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime");
    mqttclient.publish(_tmp_topic_out, "", true);
    
    thermostat_publishCurrentValues(true); // force publish current values again
    #endif

    sendLog("MQTT: deleted retained messages (states)", LOGLEVEL_INFO);
  }
}

void publishDeleteRetainedOutMessages_sensors()
{
  // after "MQTT-out retained" is switched off
  // old retained messages are deleted by publishing an empty retained message to the topics
  // to be called from config when confMqtt.mqtt_outRetain was changed to false
  if (!confMqtt.mqtt_outRetain)
  {
    char _tmp_topic_out[50];
    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
    mqttclient.publish(_tmp_topic_out, "", true);

    snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
    mqttclient.publish(_tmp_topic_out, "", true);

    sendLog("MQTT: deleted retained messages (sensors)", LOGLEVEL_INFO);

    publishCurrentSensorValues(true); // force publish current values again
  }
}