Weekday() problem

Below I have wrote a short code to check if there is any activity today.
For testing, I insert weekday 4 (Thursday). I can see the correct weekday is selected in Timeinput Widget.
The output value continuesly say 5 when do line Serial.println(weekday());
Why can’t I compare actally weekday with Blynk selected weekday?

BLYNK_WRITE(V2)  // Read timer value and check
{
   TimeInputParam t(param);
   
 for (int i = 1; i <= 7; i++) 
  {
   if (t.isWeekdaySelected(i)) 
    {
      Serial.println(String("Day ") + i + " is selected");
      Serial.println(weekday());
    }
  }
 if (weekday() == 4)
  {
    Serial.println("Activity today!");
  }
}

@SCH Blynk days of the week are different to the rest of the world.
Fix:

  int dayadjustment = -1;  
  if(weekday() == 1){
    dayadjustment = 6; // needed for Sunday Time library is day 1 and Blynk is day 7
  }
  if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
  // etc

Thanks, I know the issue about Sunday adjustment. But this is not the problem.
The problem is, I do not get current weekday. If today is Monday (weekday 1).
Line in code: Serial.println (weekday()); shows output 5. Same for Saturday and Sunday, is output = 5.

Are you using Android?

Yes, Android 5.1.

I think it hs nothing todo with Android, because I got the values correct from Arduino.
This code below, output correct weekday as String, but not at Int.
I want to compare actal weekday with Blynk selected weekday.

BLYNK_WRITE(V2)  // Read timer value and check
{
  TimeInputParam t(param);
  String td; 
 for (int i = 1; i <= 7; i++) 
  {
   if (t.isWeekdaySelected(i)) 
    {
      Serial.println(String("Day ") + i + " is selected");
      td = (dayStr(weekday()));
      Serial.println(td);
      Serial.println(dayStr(weekday()));
    }
  }
 if (td == "Thursday")
  {
    Serial.println("Activity today!");
  }
}

I have been doing some scheduling from a friend recently and when I include your code Serial Monitor now looks like this with 6 Time Input widgets:

13:27:21
Day 1 is selected
5
Day 2 is selected
5
Day 3 is selected
5
Day 4 is selected
5
Day 5 is selected
5
Day 6 is selected
5
Day 7 is selected
5
Sch2 is active today	48441	82500	82560
Sch3 is active today	48441	82620	82680
Sch4 is active today	48441	82740	82800
Sch5 is active today	48441	82860	82920
Sch6 is active today	48441	82980	83040
13:27:31
Day 1 is selected
5
Day 2 is selected
5
Day 3 is selected
5
Day 4 is selected
5
Day 5 is selected
5
Day 6 is selected
5
Day 7 is selected
5

Actually day 5 looks to be fine i.e. the day number allocated to Thursday for most time libraires, but not Blynk’s. Not sure I see a problem when you do the day adjustment hack.

I have done a new test as my code below:
If I select all weekdays (colored) in Widget. it print out from Monday to Sunday, it’s OK.
If I deselect all, it still print out all weekdays.
If I select one or two weekdays, it print out correct.

BLYNK_WRITE(V2)  // Read timer value and check
{
  TimeInputParam t(param);
  String td; 
 for (int i = 1; i <= 7; i++) 
  {
   if (t.isWeekdaySelected(i)) 
    {
      Serial.println(String("Day ") + i + " is selected");
      if(i == 1)
      {
        wd1 = "Monday";
      }
      if(i == 2)
      {
        wd2 = "Tuesday";
      }
      if(i == 3)
      {
        wd3 = "Wednesday";
      }
       if(i == 4)
      {
        wd4 = "Thursday";
      }
       if(i == 5)
      {
        wd5 = "Friday";
      }
       if(i == 6)
      {
        wd6 = "Saturday";
      }
       if(i == 7)
      {
        wd7 = "Sunday";
      }
      td = (dayStr(weekday()));
      Serial.println(td);
      Serial.println(dayStr(weekday()));
      Serial.println(weekday());
    }
  }
  Serial.println(wd1+" "+wd2+" "+wd3+" "+wd4+" "+wd5+" "+wd6+" "+wd7);
 if (td == "Thursday")
  {
    Serial.println("Activity today!");
  }
}

Actually I tested a simple code yesterday (Wednesday) and it still output value 5 when using code line:
Serial.println(weekday());
No matter which weekday, the output is 5.

Blynk “designed” it that way, although some might call it a bug.
There is a 2 character library hack to fix that. Have a good search of the site and come back to us if you don’t find the hack.

I kindly asking you to test following code, today and tomorrow.
My guess is value (tdw) shows 5 tomorrow also. It shows 5 yesterday and today.

BLYNK_WRITE(V2)  // Read timer value and check
{
  TimeInputParam t(param);
  Serial.println(dayStr(weekday()));
  int twd = weekday();
  Serial.println(twd);
}

It shows:

Thursday
5

That’s because today IS day 5 for weekday() variable in time library.
When the servers get to be Friday the value will be 6. OK?

I will run the code tomorrow, and looking forward to see 6 (Friday).
Thanks for your help.

@SCH are you on day 6 now?

Todays test show as expected 5, not 6. (today is Friday).
Code I use as below:

 BLYNK_WRITE(V2)  // Read timer value and check
{
  TimeInputParam t(param);
  Serial.println(dayStr(weekday()));
  int td = weekday();
  Serial.println(td);
}

Output to Serial shows:
Thursday
5

@SCH if you ask Google what day the 1st Jan 1970 was it will confirm it was a Thursday.

Thursday is day 5 with “regular” time libraries, like the one you are using for weekday().

So it will always be day 5 if you don’t set your clock.

Thanks, I solved the problem. I realized I have to add the RTC widget to get the correct time.
If no time fixed, you always get weekday 5. Thanks for your help. It works now.
Case closed.

I could say the same :slight_smile:

1 Like

Of cause, I give you the credit.

Sorry, what do you mean by “no time fixed”? In my case, I got current time but not current day. Same as you always display value 5 no matter what today is