Is this IF statement logic correct? (Time between) Topic is solved

Moderator: leecollings

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

Is this IF statement logic correct? (Time between)

Post by binbo »

Hi guys,

does this look correct:

Code: Select all

time    = os.date("*t")
theHour = time.hour
                 if ((theHour > 23 and theHour < 24) or (theHour < 7 and theHour >= 0)) then
                    -- A time between 11pm and 7am???
                end
Does this look like it will trigger between 11pm and 7am?

Is there a better way to do this?

Many thanks.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Is this IF statement logic correct? (Time between)

Post by waaren »

binbo wrote: Saturday 13 June 2020 23:52 Does this look like it will trigger between 11pm and 7am?
Not really

try

Code: Select all

        time = os.date("*t")
	 if time.hour > 22 or time.hour < 7  then
            print ('between 23:00 and 6:59:59')
         else
            print ('NOT between 23:00 and 6:59:59')
         end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
binbo
Posts: 50
Joined: Sunday 18 June 2017 21:47
Target OS: -
Domoticz version:
Contact:

Re: Is this IF statement logic correct? (Time between)

Post by binbo »

Thank you so much.

It seems so obvious. Lol. I don’t know how I came up with such a complicated non-working solution :lol: :lol:
tobydeteckel
Posts: 12
Joined: Wednesday 25 September 2019 14:27
Target OS: Raspberry Pi / ODroid
Domoticz version: V12461
Location: The Netherlands -Hilversum
Contact:

Re: Is this IF statement logic correct? (Time between)

Post by tobydeteckel »

Code:

Code: Select all

	function timebetween(s,e)
	   timenow = os.date("*t")
	   year = timenow.year
	   month = timenow.month
	   day = timenow.day
	   s = s .. ":00"  
	   e = e .. ":00"
	   shour = string.sub(s, 1, 2)
	   sminutes = string.sub(s, 4, 5)
	   sseconds = string.sub(s, 7, 8)
	   ehour = string.sub(e, 1, 2)
	   eminutes = string.sub(e, 4, 5)
	   eseconds = string.sub(e, 7, 8)
	   t1 = os.time()
	   t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
	   t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
	   sdifference = os.difftime (t1, t2)
	   edifference = os.difftime (t1, t3)
	   isbetween = false
	   if sdifference >= 0 and edifference <= 0 then
		  isbetween = true
	   end
	   return isbetween
	end
Example:

Code: Select all

if timebetween("00:01:00","06:00:00") then
Example 2:

Code: Select all

if timebetween("04:14:00","06:23:00") then
Credits:
Siewert Lameijer / Siewert308SW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Is this IF statement logic correct? (Time between)

Post by waaren »

tobydeteckel wrote: Saturday 20 June 2020 7:02 Example:

Code: Select all

if timebetween("00:01:00","06:00:00") then
Example 2:

Code: Select all

if timebetween("04:14:00","06:23:00") then
This function does not work for the question from the op. It does not take date crossings into account.

try

Code: Select all

if timebetween("23:00:00", "11:59:00") then
in the morning
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests