Page 1 of 1
Execute a script only once an hour
Posted: Monday 25 January 2016 23:34
by Noudje
I am new to Domoticz. And I want not only to monitor the energy consumption/production of my house, but also want to monitor the free space on the discs from my NAS. I mounted the discs from my NAS, created a LUA script (script_device_FreeSpace.lua) to calculated the free space for each disc, and created Dummy devices. It works great!
But what I see in the Log, is that this script is executed each second, or more frequently(?):
...
2016-01-25 23:31:02.028 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_FreeSpace.lua
2016-01-25 23:31:02.077 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_FreeSpace.lua
2016-01-25 23:31:02.125 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_FreeSpace.lua
...
Is there a way that this script is only executed e.g. every hour/day?
Re: Execute a script only once an hour
Posted: Tuesday 26 January 2016 0:12
by malarcy
The scheduling is fixed in domoticz.
ALL "device" scripts (scripts with "device" in their name) run everytime ANY device status changes
ALL "time" scripts (scripts with "time" in their name) run every MINUTE.
It looks like you have a device changing value regularly - that is triggering your script.
I don't use the LUA scripting myself - so I can't suggest a "better" way of doing what you need - but this might at least help you understand whats happening - perhaps others can pitch in with some advice
Hope this helps a bit
Re: Execute a script only once an hour
Posted: Tuesday 26 January 2016 0:34
by nayr
yep rename your script to: script_time_FreeSpace.lua and do something like this:
Code: Select all
function timedifference (s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
commandArray = {}
if (timedifference(otherdevices_lastupdate['Free Space']) > 3600) then
-- calculate and update free space here.
end
return commandArray
Re: Execute a script only once an hour
Posted: Tuesday 26 January 2016 0:38
by simonrg
If you have a read through the wiki then you will see lots of examples of using time scripts, which is what you want to use, as others have said.
Something simple like this would work, you don't need to track the time differnce time scripts are only called once a minute:
Code: Select all
--~/domoticz/scripts/lua/script_time_check.lua
commandArray = {}
time = os.date("*t")
-- Only operates when minutes is 0 i.e. on the hour
-- Time scripts are only called every minute, so this script will only operate once an hour
if time.min == 0 then
-- Do what you want to do inside here
end
return commandArray
Re: Execute a script only once an hour
Posted: Tuesday 26 January 2016 9:13
by jannl
Or create a shell script, update Domoricz via json and fire the script via crontab
Re: Execute a script only once an hour
Posted: Tuesday 26 January 2016 9:49
by Noudje
Thnx for all the replies!!!
I will rename my LUA file and test it.
Re: Execute a script only once an hour
Posted: Monday 19 September 2016 20:43
by javed222pi
Thanks simnrg!
how i must to do, if i need wait 20seconds?
Thanks!!
simonrg wrote:If you have a read through the wiki then you will see lots of examples of using time scripts, which is what you want to use, as others have said.
Something simple like this would work, you don't need to track the time differnce time scripts are only called once a minute:
Code: Select all
--~/domoticz/scripts/lua/script_time_check.lua
commandArray = {}
time = os.date("*t")
-- Only operates when minutes is 0 i.e. on the hour
-- Time scripts are only called every minute, so this script will only operate once an hour
if time.min == 0 then
-- Do what you want to do inside here
end
return commandArray
Re: Execute a script only once an hour
Posted: Monday 19 September 2016 23:58
by Egregius
Use cron as a sheduler, much more options than lua.
Code: Select all
#!/bin/bash
while :
do
SECOND=$(date +%S)
if [ $SECOND -eq 20 ]
then
#Do your stuff
fi
sleep 0.9
This will do your stuff at exactly the 20th second.
Re: Execute a script only once an hour
Posted: Tuesday 20 September 2016 2:00
by javed222pi
Thanks man, but Im new in lua...
I post in script board my problem with all the code that i must modify.
Re: Execute a script only once an hour
Posted: Tuesday 18 October 2016 20:35
by dannybloe
Just create a
dzVents timer script and set it to run
once every hour:
Code: Select all
return {
active = true,
on = {
['timer'] = 'every hour'
},
execute = function(domoticz)
-- do your stuff here
end
}
Re: Execute a script only once an hour
Posted: Wednesday 19 October 2016 16:34
by BakSeeDaa
It can be done in a lot of ways. Below is how I did it on my system:
script_time_misc.lua
Code: Select all
-- script_time_misc.lua
-------------------------------------
commandArray = {}
local function bathroomHum()
-- Do something here
end
local function warnComputerTimeExceeded()
-- Do something here
end
local m = os.date('%M')
if (m % 5 == 0) then
--print('The 5 minute script interval reached')
-- Call your function here that shall run every 5 minutes
bathroomHum()
end
if (m % 10 == 0) then
-- print('The 10 minute script interval reached')
-- Call your function here that shall run every 10 minutes
end
if (m % 30 == 0) then
-- print('The 30 minute script interval reached')
-- Call your function here that shall run every 30 minutes
end
if (m % 60 == 0) then
-- print('The 60 minute script interval reached')
-- Put your script code here that shall run every 60 minutes
warnComputerTimeExceeded()
end
return commandArray
Re: Execute a script only once an hour
Posted: Wednesday 07 December 2016 22:38
by bertbigb
Hi,
I have a question about these kind of scripts and I hope someone can help me
if (m % 10 == 0) then
-- print('The 10 minute script interval reached')
-- Call your function here that shall run every 10 minutes
end
This situation is true for 1 complete minute what can mean it runs your function several times
How to run a script just 1 time every 10 seconds for instance?
I hope someone can answer that question and many thanks in advance.
Re: Execute a script only once an hour
Posted: Thursday 08 December 2016 0:03
by Nautilus
bertbigb wrote:Hi,
I have a question about these kind of scripts and I hope someone can help me
if (m % 10 == 0) then
-- print('The 10 minute script interval reached')
-- Call your function here that shall run every 10 minutes
end
This situation is true for 1 complete minute what can mean it runs your function several times
How to run a script just 1 time every 10 seconds for instance?
I hope someone can answer that question and many thanks in advance.
In Domoticz the time scripts run once every minute and these type of conditions are used with them to specify a longer interval between the runs. So there is no danger of them being run for more than once during each minute.
There might have been some discussion here on the forum on how to have a shorter than one minute interval, you can check if the search brings up anything. But in general you'd have to write e.g. a bash script that you can trigger with a Domoticz time script (or with crontab if on Linux platform) once every minute. In the bash script you'd then have a for loop which would run for six times with a 10 second sleep at the end of each round...
Re: Execute a script only once an hour
Posted: Thursday 08 December 2016 4:10
by Egregius
You can trigger scripts easy by updating a (dummy) device, like a solar sensor.
At least that's how I trigger my script to run every minute, exactly at the minute:
Code: Select all
#!/bin/bash
while :
do
SECOND=$(date +%S)
if [ $SECOND -eq 0 ]
then
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8084/json.htm?type=command¶m=udevice&idx=6&nvalue=0&svalue=0"`
#Other stuff removed here...
fi
sleep 0.8
done
To run every then seconds that could be something like:
Code: Select all
#!/bin/bash
for i in {1..6}:
do
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8084/json.htm?type=command¶m=udevice&idx=6&nvalue=0&svalue=0"`
sleep 9.95
done
But, why every 10 seconds? This will cause all your device scripts to be executed every 10 seconds.
Re: Execute a script only once an hour
Posted: Thursday 08 December 2016 10:34
by bertbigb
Hi first of all thanks for the answers. I will try the bash script.
Furthermore
But, why every 10 seconds? This will cause all your device scripts to be executed every 10 seconds.
I have the following code:
Code: Select all
time = os.date("*t")
iswhite = "false"
hue = "48" --warmish yellow
idx = otherdevices_idx["Hue color plafond lamp WK"] -- name lamp
brightness = uservariables['brightness']
state = uservariables['state']
commandArray = {}
if otherdevices['WakeUp2LivingRoom'] == 'On' then --device that triggers the action now for testing
if uservariables['state'] == 0 then
state = uservariables['state']
state = state + 1
commandArray['Variable:state'] = tostring(state)
commandArray['Variable:brightness'] = tostring(100) -- see that lamp in On
elseif uservariables['state'] == 1 then
if (time.sec % 10 == 0) then --ticks every 10 seconds
brightness = brightness - 1 --lower the brightness
commandArray['Variable:brightness']= tostring(brightness)
if brightness <= 0 then
state = uservariables['state']
state = state + 1
commandArray['Variable:state'] = tostring(state) --if brightness = 0 then raise the state
end
end
elseif uservariables['state'] ==2 then
commandArray['Hue color plafond lamp WK'] = 'Off'
commandArray['Variable:state'] = tostring(0)
commandArray['Variable:brightness'] = tostring(100)
commandArray['WakeUp2LivingRoom'] = 'Off'
end
url = 'http://127.0.0.1:8084/json.htm?type=command¶m=setcolbrightnessvalue&idx=' .. idx .. '&hue=' .. hue .. '&brightness=' .. brightness ..'&iswhite=' .. iswhite
commandArray['OpenURL']= url
print('url send ' .. url..' state = ' .. state.. ' brightness = '..brightness .. ' difference = '..difference)
end
return commandArray
With this code I want a lamp to dim fluent. I want to make a wakeup and go to bed light.
And yes, it will trigger the device script every 10 second, but with all the sensors I have (temperature, PIR, door ans window sensors) these scripts get fired almost every second anyhow at the moment. And I don't know how to prevent this since I need very often the devicechanged command in my scripts.
So if you have suggestions I'm really open to that.
Thanks everyone for your good help
Re: Execute a script only once an hour
Posted: Thursday 08 December 2016 14:01
by Egregius
My suggestion: Pass2php

Re: Execute a script only once an hour
Posted: Friday 09 December 2016 10:58
by bertbigb
My suggestion: Pass2php

I have had a look at it on your webside and found a whole lot of code. Can you help me a little bit where to find more explanation and what to do to port it to my own environment?
I like Domoticz very much but this timing problem and everytime evaluating of the software when only one sensors changes makes me want to have a closer look at this php thing.
Re: Execute a script only once an hour
Posted: Friday 09 December 2016 11:06
by Egregius