user73821: I'm trying to simulate dynamic temperature using a small set of data with ESP32. I've just realized that the relay doesn't change the status if there is some variation of the temperature upper or lower the threshold.
If the temperature starts below the modeled temperature, the relay will turn on but, once it reaches the model's temperature or exceeds it, the status doesn't change, and the system will warm up till the next model.
Am I missing something in my code?
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiUdp.h>


WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 7200, 60000); //UTC+1 (Europe)


unsigned long current_time;

#define WIFI_AP "xxxxx"
#define WIFI_PASSWORD "xxxxx"
//WiFi SETUP
WiFiClient wifiClient;
int status = WL_IDLE_STATUS;
unsigned long lastSend;


const int oneWireBus1 = 0;
OneWire oneWire1 (oneWireBus1);
DallasTemperature sensors1 (&oneWire1);

const int relay1 = 02;


struct SetPoint {
  unsigned long time;
  float temperature;
};


#define DATA_SIZE  17

SetPoint scen1[DATA_SIZE] = {
  {1622652330,26.97},//18h
  {1622652390,25.62},
  {1622678400,24.87},
  {1622689200,24.16},
  {1622700000,23.86},
  {1622710800,25.17},
  {1622721600,26.41},
  {1622732400,27.22},
  {1622743200,26.78},
  {1622754000,25.83},
  {1622764800,25.1},
  {1622775600,24.6},
  {1622786400,24.24},
  {1622797200,26.81},
  {1622808000,29.35},
  {1622818800,29.38},
  {1622829600,30.5}
};

unsigned int pos = 0;


void setup() {
  Serial.begin(115200);
  sensors1.begin();
  pinMode(relay1, OUTPUT);
  InitWiFi();
  lastSend = 0;
  timeClient.begin();

  

void loop() {

  sensors1.requestTemperatures();
  float temperature1 = sensors1.getTempCByIndex(0);

  timeClient.update();

  long current_time =  timeClient.getEpochTime();

  if (pos < DATA_SIZE && current_time >= scen1[pos].time) {
    if (temperature1 < scen1[pos].temperature) {
      digitalWrite (relay1, HIGH);
    }
    else if (temperature1 > scen1[pos].temperature) {
      digitalWrite (relay1, LOW);
    }
    pos++;
  }

  Serial.println(timeClient.getEpochTime());
  Serial.println(timeClient.getFormattedTime());
  Serial.println(pos);
  Serial.println(scen1[pos].temperature);
  Serial.println(scen1[pos-1].temperature);
  Serial.print("Temperature Censius - Sensor 1: ");
  Serial.print(temperature1);
  Serial.println(" ºC ");
  delay(3000);


}

(UPDATE)
Replacing the first if with while doesn't change anything.
The code works when taking if / else if out of nested if, but in this case I'm not able to increase time (the pos++ is also commented), and define limits according to the array size.
//  if (pos < DATA_SIZE && current_time >= scen1[pos].time) {
    if ((temperature1 < scen1[pos].temperature)&& (current_time >= scen1[pos].time)) {
      digitalWrite (relay1, HIGH);
    }
    else if ((temperature1 > scen1[pos].temperature)&&(current_time >= scen1[pos].time)) {
      digitalWrite (relay1, LOW);
    }
//    pos++;
//  }


user73821: I have to remove the pos++ from if statement to have digitalWrite working again according to captured temperature:
  if (pos < DATA_SIZE && current_time >= scen1[pos].time) {
    if (temperature1 < scen1[pos].temperature) {
    digitalWrite (relay1, HIGH);
    }
    else if (temperature1 > scen1[pos].temperature) {
    digitalWrite (relay1, LOW);
    }
   }

Obviously this isn't updating time and the system gets stuck at time 0 of the array.
(UPDATE)
As well observed by Dave Newton, the previous observation is pointless to really fix the problem.
The problem was fixed taking the if statement that control the relay from inside the one controlling the time.
Now I can simulate a time interval defined with the array and use the temperature (also present in the array) to turn on/off the relay that controls the heater and simulates environment temperature:
First I created a function that will work as a thermostat:
void set_thermostat() {
  sensors1.requestTemperatures();
  float temperature1 = sensors1.getTempCByIndex(0);

  if (temperature1 < scen1[pos].temperature) {
    digitalWrite (relay1, HIGH);
  }
  else if (temperature1 > scen1[pos].temperature) {
    digitalWrite (relay1, LOW);
  }
}

Then I can it at the loop function, outside the statement that increases the time, based on the one defined by the array:
  if (pos < DATA_SIZE && current_time >= scen1[pos].times) {
    pos++;
  }

  set_thermostat(); 

This reproduces exactly what I've been looking for, with real-time control of the temperature comparing the environmental one with the model.

73821: <p>I'm trying to simulate dynamic temperature using a small set of data with ESP32. I've just realized that the relay doesn't change the status if there is some variation of the temperature upper or lower the threshold.</p>
<p>If the temperature starts below the modeled temperature, the relay will turn on but, once it reaches the model's temperature or exceeds it, the status doesn't change, and the system will warm up till the next model.</p>
<p>Am I missing something in my code?</p>
<pre><code>#include &lt;OneWire.h&gt;
#include &lt;DallasTemperature.h&gt;
#include &lt;NTPClient.h&gt;
#include &lt;WiFi.h&gt;
#include &lt;WiFiUdp.h&gt;


WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, &quot;europe.pool.ntp.org&quot;, 7200, 60000); //UTC+1 (Europe)


unsigned long current_time;

#define WIFI_AP &quot;xxxxx&quot;
#define WIFI_PASSWORD &quot;xxxxx&quot;
//WiFi SETUP
WiFiClient wifiClient;
int status = WL_IDLE_STATUS;
unsigned long lastSend;


const int oneWireBus1 = 0;
OneWire oneWire1 (oneWireBus1);
DallasTemperature sensors1 (&amp;oneWire1);

const int relay1 = 02;


struct SetPoint {
  unsigned long time;
  float temperature;
};


#define DATA_SIZE  17

SetPoint scen1[DATA_SIZE] = {
  {1622652330,26.97},//18h
  {1622652390,25.62},
  {1622678400,24.87},
  {1622689200,24.16},
  {1622700000,23.86},
  {1622710800,25.17},
  {1622721600,26.41},
  {1622732400,27.22},
  {1622743200,26.78},
  {1622754000,25.83},
  {1622764800,25.1},
  {1622775600,24.6},
  {1622786400,24.24},
  {1622797200,26.81},
  {1622808000,29.35},
  {1622818800,29.38},
  {1622829600,30.5}
};

unsigned int pos = 0;


void setup() {
  Serial.begin(115200);
  sensors1.begin();
  pinMode(relay1, OUTPUT);
  InitWiFi();
  lastSend = 0;
  timeClient.begin();

  

void loop() {

  sensors1.requestTemperatures();
  float temperature1 = sensors1.getTempCByIndex(0);

  timeClient.update();

  long current_time =  timeClient.getEpochTime();

  if (pos &lt; DATA_SIZE &amp;&amp; current_time &gt;= scen1[pos].time) {
    if (temperature1 &lt; scen1[pos].temperature) {
      digitalWrite (relay1, HIGH);
    }
    else if (temperature1 &gt; scen1[pos].temperature) {
      digitalWrite (relay1, LOW);
    }
    pos++;
  }

  Serial.println(timeClient.getEpochTime());
  Serial.println(timeClient.getFormattedTime());
  Serial.println(pos);
  Serial.println(scen1[pos].temperature);
  Serial.println(scen1[pos-1].temperature);
  Serial.print(&quot;Temperature Censius - Sensor 1: &quot;);
  Serial.print(temperature1);
  Serial.println(&quot; ºC &quot;);
  delay(3000);


}
</code></pre>
<p>(UPDATE)</p>
<p>Replacing the first <code>if</code> with <code>while</code> doesn't change anything.</p>
<p>The code works when taking <code>if</code> / <code>else if</code> out of nested <code>if</code>, but in this case I'm not able to increase time (the <code>pos++</code> is also commented), and define limits according to the array size.</p>
<pre><code>//  if (pos &lt; DATA_SIZE &amp;&amp; current_time &gt;= scen1[pos].time) {
    if ((temperature1 &lt; scen1[pos].temperature)&amp;&amp; (current_time &gt;= scen1[pos].time)) {
      digitalWrite (relay1, HIGH);
    }
    else if ((temperature1 &gt; scen1[pos].temperature)&amp;&amp;(current_time &gt;= scen1[pos].time)) {
      digitalWrite (relay1, LOW);
    }
//    pos++;
//  }
</code></pre>
 73821: <p>I have to remove the <code>pos++</code> from if statement to have digitalWrite working again according to captured temperature:</p>
<pre><code>  if (pos &lt; DATA_SIZE &amp;&amp; current_time &gt;= scen1[pos].time) {
    if (temperature1 &lt; scen1[pos].temperature) {
    digitalWrite (relay1, HIGH);
    }
    else if (temperature1 &gt; scen1[pos].temperature) {
    digitalWrite (relay1, LOW);
    }
   }
</code></pre>
<p>Obviously this isn't updating time and the system gets stuck at time 0 of the array.</p>
<p>(UPDATE)</p>
<p>As well observed by Dave Newton, the previous observation is pointless to really fix the problem.</p>
<p>The problem was fixed taking the <code>if</code> statement that control the relay from inside the one controlling the time.</p>
<p>Now I can simulate a time interval defined with the array and use the temperature (also present in the array) to turn on/off the relay that controls the heater and simulates environment temperature:</p>
<p>First I created a function that will work as a thermostat:</p>
<pre><code>void set_thermostat() {
  sensors1.requestTemperatures();
  float temperature1 = sensors1.getTempCByIndex(0);

  if (temperature1 &lt; scen1[pos].temperature) {
    digitalWrite (relay1, HIGH);
  }
  else if (temperature1 &gt; scen1[pos].temperature) {
    digitalWrite (relay1, LOW);
  }
}
</code></pre>
<p>Then I can it at the <code>loop</code> function, outside the statement that increases the time, based on the one defined by the array:</p>
<pre><code>  if (pos &lt; DATA_SIZE &amp;&amp; current_time &gt;= scen1[pos].times) {
    pos++;
  }

  set_thermostat(); 
</code></pre>
<p>This reproduces exactly what I've been looking for, with real-time control of the temperature comparing the environmental one with the model.</p>
