Hi all, I’ve been struggling with this code for some time now but I’m out of ideas and need help!
Basically, I have a program that gets uses a webhook to get weather data from Weather Underground. Specifically in this particular instance, I’m getting the astronomy information to get the sunset and sunrise time for each day so I can turn on a relay when the time is between sunset and sunrise. I’m using the sparktime library to set the variables “hour” and “minute” each time I loop through the code, and I have confirmed that it’s updating correctly. I’ve also been able to confirm via Publish events that I’m storing the correct sunrise and sunset hour and minute variables. My problem is simply that I can’t seem to get the code to set my “daytime” variable TRUE when the current time of day is between sunrise and sunset, and FALSE when between sunset and sunrise.
This had a strange side effect though: whenever the current MINUTE (not hour) was between the sunsetMinute and the sunriseMinute, daytime would be set to true. As sunset has been falling around the 45th minute and sunrise around the 15th minute here lately, I would see my output turn on for this odd 30-ish minute period. I’m wondering if perhaps this statement was evaluating as true because both conditions were false?
Apparently my boolean logic skills in C are rusty so I could use more eyes on this. Please let me know if I’ve left out any important details or if you want to see the complete code. Thanks!
I haven’t looked too close into your code since I usually tell people to spare themselves the hassle and just convert hh:mm:ss timestamps to seconds and just do this
// start and stop need to be on same day otherwise extra care needs to be taken
const int startSec = hStart*3600 + mStart*60 + sStart;
const int stopSec = hStop*3600 + mStop*60 + sStop;
int localSec = Time.local() % 86400; // keep the seconds of current day and drop date info
if (startSec <= localSec && localSec < stopSec) {
// time is between range
}
else {
// time is outside range
}
That won’t do either since for the case (hour != sunriseHour && hour != sunsetHour) the minute needs to be taken out of the condition as it becomes irrelevant but will render your result wrong when taken into account.
You can write that out as extra conditions but it becomes rather cumbersome and messy IMHO.
The first term should render to true for 5:20 but it won’t just as 21:15 wouldn’t render the second term true although it should (= or not doesn’t change the fact)
? I would think that they would evaluate the same way, unless I’m misunderstanding how the less than or equal to expression evaluates in C versus mathematics.
The problem here is that Weather Underground gives me the time in 24 hour format, so I would have to convert that to, say, Unix time...not sure how to do that?
You need to consider && has precedence before || hence I can leave the parentheses out (similar to multiplications/divisions have precedence before additions/subtractions)
Got it, I follow now. I’m trying the long and ugly method first, but I may give the unix time option a shot (note that I’m using actual unix time from NTP; I know it’s not a difficult conversion from hh:mm, I just didn’t want to add more conversions than I needed).
That’s a pretty slick method! The sparktime library I’m using actually returns unix time by default, so I could just directly set now_time to be the unix time returned from sparktime (no time_t conversion needed for that step). I may just give this a shot!