Page 1 of 1

timeofday[‘Nighttime’] and time.hour < 7

Posted: Tuesday 11 February 2020 9:50
by binbo
Hi all,

Can someone help me with the logic of this Lua code...

Code: Select all

If timeofday[‘Nighttime’] then

If time.hour < 7 then
 commandArray[‘light’] = “On”
end

end
I want the light to come on from night time — but not come on after 7am - as it is already light. (Night time ends at 7:36am)

My logic is wrong though as 23 is higher then 7 so my if statement will fail. I didn’t want to put a set hour in my if statement like this:

Code: Select all

If time.hour > 18 and time.hour < 7
Any help would be great.

Many thanks.

Re: timeofday[‘Nighttime’] and time.hour < 7

Posted: Friday 14 February 2020 7:24
by bewo
Hi binbo,

if you really wan't to use the hard time 7 o'clock it's possible, but i think it would be an nicer way to use sunset and sunrise...

Code: Select all

commandArray = {}

-- Set the light device:
light_device = 'light'


-- Set the time:
now = os.date("%H") * 60 + os.date("%M")
sunrise = timeofday['SunsetInMinutes']
sunset = timeofday['SunriseInMinutes']


-- Turn on:
if now >= sunset or now <= sunrise then
    if otherdevices[light_device] == 'Off' then
        commandArray[light_device] = 'On'
    end    

-- Turn off:
else
    if otherdevices[light_device] == 'On' then
        commandArray[light_device] = 'Off'
    end 

end

return commandArray
As i understood the light should always be on in nighttime. So this is what the script will do. It wouldn't be possible to turn off the light by hand.

Re: timeofday[‘Nighttime’] and time.hour < 7

Posted: Friday 14 February 2020 8:40
by DaanT
Hi binbo,

I don't rely understand you last sentence. But the last code you gave is a solution I would use too, but you need an 'OR', not an 'AND'.

Code: Select all

If time.hour > 18 or time.hour < 7
Regards
DaanT