Lua "less than or equal" question

perhaps my syntax is wrong, but in checking here [url=http://lua-users.org/wiki/ExpressionsTutorial]http://lua-users.org/wiki/ExpressionsTutorial[/url] it seems right. i have a simple function that gets some variables, and then does a simple >= or <= comparison. for some reason it only evaluates on the less than and ignores the equal. here is my code:

function isNightMode() local funct = "isNightMode()" --get variables local nightStart = tonumber((luup.variable_get(SEB_URN, "nightStart", DEV[SEB_DEV]))) or 11 local nightEnd = tonumber((luup.variable_get(SEB_URN, "nightEnd", DEV[SEB_DEV]))) or 5 -- Get current time local t = os.date('*t') local hour = tonumber(t.hour) vLog(funct, "The hour we got was: " .. hour .. ", it is a: ".. type(hour) .. ". Comparing it to Night Start: " .. nightStart .. ", it is a: ".. type(nightStart) .. ", or night End:" .. nightEnd .. ", it is a: ".. type(nightEnd)) --NIGHT if (hour >= nightStart) or (hour <= nightEnd) then vLog(funct, "It is after hours") return true else vLog(funct, "It is daytime hours") return false end end

in my logs, i see the below entries. seems like the “less than or equal to” expression only does greater than. anyone else see this?

50 11/12/18 6:52:29.051 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 06:52:29 MESSAGE: The hour we got was: 6, it is a: number. Comparing it to Night Start: 23, it is a: number, or night End:6, it is a: number <0x77188520> 50 11/12/18 6:52:29.052 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 06:52:29 MESSAGE: It is after hours <0x77188520> 50 11/12/18 7:00:00.122 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 07:00:00 MESSAGE: The hour we got was: 7, it is a: number. Comparing it to Night Start: 23, it is a: number, or night End:6, it is a: number <0x73988520> 50 11/12/18 7:00:00.123 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 07:00:00 MESSAGE: It is daytime hours <0x73988520>

apparently the greater than or equal to function works just fine. see logs below:

50 11/11/18 22:57:23.673 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 22:57:23 MESSAGE: The hour we got was: 22. Comparing it to Night Start: 23, or night End:6 <0x75016520> 50 11/11/18 22:57:23.673 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 22:57:23 MESSAGE: It is daytime hours <0x75016520> 50 11/11/18 23:00:26.541 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 23:00:26 MESSAGE: The hour we got was: 23. Comparing it to Night Start: 23, or night End:6 <0x75016520> 50 11/11/18 23:00:26.542 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 23:00:26 MESSAGE: It is after hours <0x75016520>

[quote=“sebby, post:1, topic:200075”]perhaps my syntax is wrong, but in checking here [url=http://lua-users.org/wiki/ExpressionsTutorial]http://lua-users.org/wiki/ExpressionsTutorial[/url] it seems right. i have a simple function that gets some variables, and then does a simple >= or <= comparison. for some reason it only evaluates on the less than and ignores the equal. here is my code:

...snip... if (hour >= nightStart) or (hour <= nightEnd) then vLog(funct, "It is after hours") return true else vLog(funct, "It is daytime hours") return false end ...

in my logs, i see the below entries. seems like the “less than or equal to” expression only does greater than. anyone else see this?

50 11/12/18 6:52:29.051 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 06:52:29 MESSAGE: The hour we got was: 6, it is a: number. Comparing it to Night Start: 23, it is a: number, or night End:6, it is a: number <0x77188520> 50 11/12/18 6:52:29.052 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 06:52:29 MESSAGE: It is after hours <0x77188520>
[/quote]

This is correct. 6 <= 6 is true, 6 is not less than but is equal to 6, so it reports “after hours” as your code instructs.

50 11/12/18 7:00:00.122 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 07:00:00 MESSAGE: The hour we got was: 7, it is a: number. Comparing it to Night Start: 23, it is a: number, or night End:6, it is a: number <0x73988520> 50 11/12/18 7:00:00.123 luup_log:0: SEBBY_LOG FUNCTION: isNightMode() TIME: 07:00:00 MESSAGE: It is daytime hours <0x73988520>
[/quote]

And now 7 is neither less than nor equal to 6, so your code reports its daytime, also correct as written.

Am I missing something?

nope, you are not missing anything, i am… i basically copied the code from another function i use to determine the dates for winter or summer heating. that last comparison should be a “less than” only. i could kick myself…

Unrelated to this thread, but perhaps helpful, here is how I switch from heating to cooling modes. Instead of hard dates, I look at outside temperature. If > 65F, switch to cooling mode, and if < 60F, switch to heat. Really helpful during spring and fall when you may be heating in the early morning and cooling in the afternoon.

thanks Don, what are you using for outside temp? i find that the temperature the Vera gives me is very inaccurate. Also, what do you do in the in-between temps, shut the ac off?

For outside temp sensor, I use one of the vision/monoprice 4 in 1 motion sensor tucked under the roof of my porch. Even though it is indoor rated, as long as it stays dry it?s been working fine, it gives me humidity and luminosity outdoor… Another alternative is to use one of the weather plugins.

thanks Don, what are you using for outside temp? i find that the temperature the Vera gives me is very inaccurate. Also, what do you do in the in-between temps, shut the ac off?[/quote]

I am currently using the Weatherunderground plug-in but the API will be shut down at the end of the year. I have the MIOS plug-in installed and will likely use it until I have time to mess with something else - perhaps Dark Sky.

Weatherunderground reports 32.9F and MIOS reports 35.0. Not wildly inaccurate but understandable. Weatherunderground is reporting temperature from a station less than a mile from my house, and I suspect MIOS reports the central Ohio temperature - probably from the international airport, which is like 20 miles south and east from my house. But it is close enough for my purposes.

I am trying to figure out how to harvest sky conditions from Vera so I can just not use a weather service. I use it for exterior lights to offset from dawn and dusk. Cloudy and I offset 15 minutes. Precipitation and I offset 30 minutes. But like Rafale, ultimately I will lightly go with a light sensor so if it gets really dark mid-afternoon due to a storm, the lights would go on regardless of the sun’s position.

I’ve used a number of battery powered 4 in 1 sensors for outside temp, but the most dependable has been the EZMultiPli Z-Wave Multi-Sensor. It’s a plug-in (AC powered) unit but has worked flawlessly. No batteries to change and very accurate. I tried the Weather Underground plug in, but the reporting stations (neighbors) would go off line frequently due to power and/or internet outages.
My Vera is powered via a UPS, and the critical sensors are also powered via UPS’s.

Thanks for the LUA code, I’ll be using snippets in a number of scenes