# Having issues with Time Function

**URL:** <https://community.particle.io/t/having-issues-with-time-function/58993>\
**Category:** Cloud\
**Created:** [January 8, 2021, 12:49am UTC](https://community.particle.io/t/having-issues-with-time-function/58993 "2021-01-08T00:49:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![italex](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/italex/32/26312_2.png) [@italex](https://community.particle.io/u/italex)\
**Post date:** [January 8, 2021, 12:49am UTC](https://community.particle.io/t/having-issues-with-time-function/58993/1 "2021-01-08T00:49:32Z")

</div>

Hi Folks,  
I am kind of lost, so I need some help. The following code is part of my Home Thermostat using the Particle Photon, of course, this is not the whole  
code, this part just tells me how long the heater has been ON or OFF.  
The problem that I am having is with time.

This line is supposed to tell me how long the heater has been ON, the problem is that sometimes it tells me the correct time, like  
4:23, this means 4 minutes and 23 seconds, most of the time, it says like 4:546785, 4 minutes and how many seconds?

```cpp
Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(heater_total_timeS), + String("\n")); 

```

I am using Blynk, that’s why you see: Blynk.virtualWrite…  
I am trying to avoid configuring an external RTC like the DS3231, so using the Particle built in RTC will simplify the hardware design.  
The first screenshot last line shows my issues, the second screenshot last line is what I expect to see, in this case: 2 minutes and 7 seconds.

Thank you.

```cpp
bool heater_now, heater_previous = 1;
unsigned long heater_OFFm, heater_OFFs, heater_ONm, heater_ONs, heater_total_timeM, heater_total_timeS, heater_counter;

void heater_ON_OFF() 
{
    heater_now = digitalRead(HEATING);
   if (heater_now != heater_previous)
    {
    heater_previous = heater_now;
    if (heater_now == HIGH) 
    {
    heater_counter++; // Increment counter everytime the heating system is turned ON
    Blynk.virtualWrite(V12, "clr");
    Blynk.virtualWrite(V12,"=============== Heating ON =================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format(Time.now(), "%m/%d/%y at %I:%M:%S %p \n")); // Cooling System turned ON\n"))); 
    Blynk.virtualWrite(V12, "Heating System Turned ON, \n"); 
    Blynk.virtualWrite(V12, ("Temperature was: ") + String(fTemp,2), "\n"); 
    Blynk.virtualWrite(V12, "Today numbers of ON so far: ", heater_counter, "\n"); 
    heater_ONm = Time.minute(); // Track how long the heater has been ON.
    heater_ONs = Time.second();
    Blynk.virtualWrite(V12, "\n"); // Insert a blank line. 

    //Blynk.virtualWrite(V3, (Time.format(Time.now(), "%m/%d/%y %I:%M:%S %p Heating System turned ON\n"))); 
    }
   
   if (heater_now == LOW)
    {
    heater_OFFm = Time.minute(); // Track how long the heater has been OFF
    heater_OFFs = Time.second();
    Blynk.virtualWrite(V12,"=============== Heater OFF ================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format(Time.now(), "%m/%d/%y at %I:%M:%S %p \n")); 
    Blynk.virtualWrite(V12, "Heating System Turned OFF, \n"); 
    Blynk.virtualWrite(V12, "Temperature was: " + String(fTemp,2), + "\n"); 
    heater_total_timeM = heater_OFFm - heater_ONm;
    heater_total_timeS = heater_OFFs - heater_ONs;
    Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(heater_total_timeS), + String("\n")); 
   heater_total_timeM = 0;
   heater_total_timeS = 0;
   
    }  
    
  }
}

```

 ![IMG_3341.PNG](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/e/a/ea42bacd6e1723ba1606db6269c558824eed24fe.jpeg)! ![IMG_3349.PNG](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/2/e/2e42b3df9649a194f55eea2f5499b92abbacab64.jpeg)!

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 8, 2021, 9:45am UTC](https://community.particle.io/t/having-issues-with-time-function/58993/2 "2021-01-08T09:45:24Z")

</div>

Just a few notes

> [@italex](#):
>
> `Time.format(Time.now(), "%m/%d/%y at %I:%M:%S %p \n")`

When only working with the current time, the better way to write this would be

```cpp
Time.format("%D at %r \n")

```

When _not_ providing a distinct date to format the current time will be used automatically (the alternative format is optional for brevity)

But for your actual issue:  
You cannot blindly subtract `heater_ONm` / `heaterONs` from `heater_OFFm` / `heaterOFFs` as these may well render _negative_ numbers.  
That's probably also the reason for your odd numbers as you explicitly declare your `heater_total_timeM` / `heater_total_timeS` as _ **`unsigned long`** _.

The better approach would be to store the `onTime = Time.local()` and then calculate `secondsRunning = Time.local() - onTime` - this will always render a positive result from which you can derive your desired minutes and seconds values.  
If you can be sure that your minute count will never exceed 59 you can use `Time.format(secondsRunning, "%M:%S")`.

---

<div class="post-metadata">

**Author:** ![italex](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/italex/32/26312_2.png) [@italex](https://community.particle.io/u/italex)\
**Post date:** [January 8, 2021, 7:16pm UTC](https://community.particle.io/t/having-issues-with-time-function/58993/3 "2021-01-08T19:16:32Z")

</div>

Thank you very much for your quick reply, ScruffR. I implement the changes you suggested, they work. Very much appreciated. By the way, the variables are listed in variables.h header file, that’s why you dont see them here. Here is the updated code:

```auto
void heater_ON_OFF() 
{
    heater_now = digitalRead(HEATING);
   if (heater_now != heater_previous)
    {
    heater_previous = heater_now;
    if (heater_now == HIGH) 
    {
    heater_counter++; // Increment counter everytime the heating system is turned ON
    Blynk.virtualWrite(V12, "clr"); // Clear terminal. 
    Blynk.virtualWrite(V12,"=============== Heater ON =================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format("%D at %r, \n"));
    Blynk.virtualWrite(V12, "Heating System Turned ON, \n"); 
    Blynk.virtualWrite(V12, ("Temperature was: ") + String(fTemp,2), "\n"); 
    Blynk.virtualWrite(V12, "Today numbers of ON so far: ", heater_counter, "\n"); 
    heater_ONm = Time.minute(); // Track ON time in minutes.
    onTime = Time.local(); // Track ON time in seconds
    Blynk.virtualWrite(V12, "\n"); // Insert a blank line. 
    }
    
   if (heater_now == LOW)
    {
    heater_OFFm = Time.minute(); // Track OFF time in minutes
    Blynk.virtualWrite(V12,"=============== Heater OFF ================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format("%D at %r, \n"));
    Blynk.virtualWrite(V12, "Heating System Turned OFF, \n"); 
    Blynk.virtualWrite(V12, "Temperature was: " + String(fTemp,2), + "\n"); 
    heater_total_timeM = heater_OFFm - heater_ONm;
    secondsRunning = Time.local() - onTime;
    Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(Time.format(secondsRunning, "%S")), + String("\n")); 
    heater_OFFm = 0; // Reset heater OFF time tracker
    heater_ONm = 0; // Reset heater ON time tracker
    onTime = 0; // Reset ON seconds tracker
    }  
    
  }
   if(Time.hour() == 23 && Time.minute() == 59 && Time.second() >= 55) heater_counter = 0; // Reset Heat counter at mid night. 
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [July 10, 2021, 7:16am UTC](https://community.particle.io/t/having-issues-with-time-function/58993/4 "2021-07-10T07:16:49Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
