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

Moderator: leecollings

Post Reply
binbo
Posts: 50
Joined: Sunday 18 June 2017 21:47
Target OS: -
Domoticz version:
Contact:

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

Post 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.
User avatar
bewo
Posts: 74
Joined: Monday 13 July 2015 12:27
Target OS: Linux
Domoticz version: 2021.1
Location: Bavaria - Germany
Contact:

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

Post 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.
Individual projects:
Domoticz on a Intel Xeon Server | AeonLabs Z-Wave Gen.5 | RFXCOM RFXtrx433E USB | ESP-Wifi-Modules | Shellys
Wall-mounted 22" Touch Control Display (self construct) | LUA wind monitor| LUA heating control | and many many more :)
DaanT
Posts: 14
Joined: Friday 23 August 2019 10:11
Target OS: Windows
Domoticz version: 2020.2
Location: Belgium
Contact:

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

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest