os.time with %X

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Mikey
Posts: 31
Joined: Friday 13 January 2017 7:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

os.time with %X

Post by Mikey »

I have multiple scripts where I use the time, but I normally use the time like this:

Code: Select all

dayNow = tonumber(os.date('%w'))
hourNow = tonumber(os.date('%H'))
Within this I use integers to use the time like this:

Code: Select all

elseif ((dayNow > 0) and (dayNow < 6)) and (((hourNow > 23) and (hourNow < 0)) or ((hourNow >= 0) and (hourNow < 7))) then
But now I would like to make it different with the %X, means:

Code: Select all

timeExactly = (os.time('%X'))
But how can I use the %X? I found an example for '23:48:10', but how can I use this in script? Something like this does not work.

Code: Select all

if timeExactly == '06:28:00' then
Thank you very much for your help.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: os.time with %X

Post by Nautilus »

Well, it might work with

Code: Select all

if timeExactly == '06:28:00' then
if your script happens to run on that exact second :) But I doubt you'd be that lucky. Maybe it is better to start with explaining what you are trying to accomplish as I have a hunch you might be going at it from a wrong direction :)

You can easily add a condition for minute by adding something like

Code: Select all

minuteNow = tonumber(os.date("%M"))
and use it as a condition on time scripts (which trigger each minute), but I cannot think of any situation where knowing the current second would "add value" in accomplishing some task with Domoticz...
Mikey
Posts: 31
Joined: Friday 13 January 2017 7:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: os.time with %X

Post by Mikey »

Thanks for your reply. Actually I would like to do two things:

1. Trigger a switch to turn on at this time to make a back up. I want to turn on the switch at a time and turn the switch off again after two minutes.
2. I want to trigger a motion sensor and turn on a light when there's motion. But I want to do different scenes if there's different daytime. I already have this script, but I only trigger it with days and hours like:

Code: Select all

if (globalvariables['Security']) == 'Disarmed' or devicechanged['Dummy Disarmed'] == 'On' then

	if ((otherdevices['Bewegungssensor_WZ'] == 'On' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'Off') then
		commandArray['Licht_WZ'] = 'On'
		print('Erste Moeglichkeit')
	elseif ((otherdevices['Bewegungssensor_WZ'] == 'Off' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'On') then
		print('Zweite Moeglichkeit')
		if (((dayNow > 0) and (dayNow < 6)) and ((hourNow >= 7) and (hourNow < 23))) then
			commandArray['Licht_WZ'] = 'Off AFTER 3600'
			print('Es ist Wochentag zwischen 7 und 23 Uhr')
		elseif ((dayNow > 0) and (dayNow < 6)) and (((hourNow > 23) and (hourNow < 0)) or ((hourNow >= 0) and (hourNow < 7))) then
			commandArray['Licht_WZ'] = 'Off AFTER 300'
			print('Es ist Wochentag zwischen 23 und 7 Uhr')
		elseif (((dayNow == 0) or (dayNow == 6)) and ((hourNow >= 7) and (hourNow < 23))) then
		    commandArray['Licht_WZ'] = 'Off AFTER 3600'
			print('Es ist Wochenende zwischen 7 und 23 Uhr')
		elseif ((dayNow == 0) or (dayNow == 6)) and (((hourNow > 23) and (hourNow < 0)) or ((hourNow >= 0) and (hourNow < 7))) then
			commandArray['Licht_WZ'] = 'Off AFTER 1200'
			print('Es ist Wochenende zwischen 23 und 7 Uhr')
	    end
But I would like to trigger it more exactly.
Do you have any idea
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: os.time with %X

Post by Nautilus »

Mikey wrote:Thanks for your reply. Actually I would like to do two things:
1. Trigger a switch to turn on at this time to make a back up. I want to turn on the switch at a time and turn the switch off again after two minutes.
For this I would add the minute condition mentioned in the previous post. Lua time scripts run every minute (usually right after the minute changes). You can add an off delay of two minutes to the switch or use the same script to accomplish the "off" command. However, cannot see any benefit of having the dummy switch in Domoticz for this. I'd just run the backup script and that's that. If you want to keep the dummy, you can add a schedule for the switch as well (turn on/off at certain time) :)
Mikey wrote:2. I want to trigger a motion sensor and turn on a light when there's motion. But I want to do different scenes if there's different daytime. I already have this script, but I only trigger it with days and hours like:

Code: Select all

if (globalvariables['Security']) == 'Disarmed' or devicechanged['Dummy Disarmed'] == 'On' then

	if ((otherdevices['Bewegungssensor_WZ'] == 'On' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'Off') then
		commandArray['Licht_WZ'] = 'On'
		print('Erste Moeglichkeit')
	elseif ((otherdevices['Bewegungssensor_WZ'] == 'Off' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'On') then
		print('Zweite Moeglichkeit')
		if (((dayNow > 0) and (dayNow < 6)) and ((hourNow >= 7) and (hourNow < 23))) then
			commandArray['Licht_WZ'] = 'Off AFTER 3600'
			print('Es ist Wochentag zwischen 7 und 23 Uhr')
		elseif ((dayNow > 0) and (dayNow < 6)) and (((hourNow > 23) and (hourNow < 0)) or ((hourNow >= 0) and (hourNow < 7))) then
			commandArray['Licht_WZ'] = 'Off AFTER 300'
			print('Es ist Wochentag zwischen 23 und 7 Uhr')
		elseif (((dayNow == 0) or (dayNow == 6)) and ((hourNow >= 7) and (hourNow < 23))) then
		    commandArray['Licht_WZ'] = 'Off AFTER 3600'
			print('Es ist Wochenende zwischen 7 und 23 Uhr')
		elseif ((dayNow == 0) or (dayNow == 6)) and (((hourNow > 23) and (hourNow < 0)) or ((hourNow >= 0) and (hourNow < 7))) then
			commandArray['Licht_WZ'] = 'Off AFTER 1200'
			print('Es ist Wochenende zwischen 23 und 7 Uhr')
	    end
But I would like to trigger it more exactly.
Do you have any idea
Probably you mean the trigger is a motion sensor and action is turning on the light? This needs a device script that triggers exactly when the motion sensor turns on (/ or off in case you need). It would be something like:

Code: Select all

if devicechanged['Bewegungssensor_W'] == 'On' and otherdevices['Licht_WZ'] == 'Off' then
 	if dayNow > 0 and dayNow < 6 and hourNow >= 7 and hourNow < 23 and minuteNow > XX and minuteNow < XX then
 		...
 	end
 end
Maybe you do not need the minute condition anymore when the script runs only when motion sensor is active?

So, if you want to react on device change use a device script with "if devicechanged..." condition at the start and additional time based condition to have a different action based on the current time. Then if you want to run or check something each minute, use a time script....:)
Mikey
Posts: 31
Joined: Friday 13 January 2017 7:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: os.time with %X

Post by Mikey »

Thanks Nautilus

I need both. I turn on the light when theres motion. Then I turn off the light when there has been no motion for a meaning time. But the meaning time is different for the different daytimes. I did it like you mentioned above. Now I can try to add minutes. But it makes the script very complicated. That's why I wanted to write it in the "23:58:00" format or similar. This would make the script much easier to read. Isn't there a possibility?
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: os.time with %X

Post by Nautilus »

Mikey wrote:Thanks Nautilus

I need both. I turn on the light when theres motion. Then I turn off the light when there has been no motion for a meaning time. But the meaning time is different for the different daytimes. I did it like you mentioned above. Now I can try to add minutes. But it makes the script very complicated. That's why I wanted to write it in the "23:58:00" format or similar. This would make the script much easier to read. Isn't there a possibility?
Surely there's some way but I have not tried to use that type of time in my scripts. One option would be to turn the time to seconds from midnight. e.g:

Code: Select all

function currentTimeInSeconds()
	s = os.time()
	hours = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	return (hours * 3600 + minutes * 60 + seconds)
end
and then use this number in your scripts:

Code: Select all

currentTime = currentTimeInSeconds()
if currentTimeInSeconds >= 86280 then
But still, I think seconds are too detailed to have any meaningful use in this type of scripts, minutes should be enough.

You could also add some dummy switches and add schedules for them to go on/off at certain times and then base the motion detection actions on the state of those dummy switches. Also, I don't think it looks that much complicated if you have a condition:

Code: Select all

if hour >= and minute >= 30 then
compared to

Code: Select all

if time >= "23:58:00" then
Mikey
Posts: 31
Joined: Friday 13 January 2017 7:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: os.time with %X

Post by Mikey »

Perfect, I will try that, thanks a lot :D
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 1 guest