Execute a script only once an hour
Moderator: leecollings
-
- Posts: 16
- Joined: Monday 25 January 2016 23:07
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
Execute a script only once an hour
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?
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
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
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
- nayr
- Posts: 354
- Joined: Tuesday 11 November 2014 18:42
- Target OS: Linux
- Domoticz version: github
- Location: Denver, CO - USA
- Contact:
Re: Execute a script only once an hour
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
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
-
- Posts: 329
- Joined: Tuesday 16 July 2013 22:54
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.8807
- Location: North East England
- Contact:
Re: Execute a script only once an hour
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:
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
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
-
- Posts: 625
- Joined: Thursday 02 October 2014 6:36
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2022.2
- Location: Geleen
- Contact:
Re: Execute a script only once an hour
Or create a shell script, update Domoricz via json and fire the script via crontab
-
- Posts: 16
- Joined: Monday 25 January 2016 23:07
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
Re: Execute a script only once an hour
Thnx for all the replies!!!
I will rename my LUA file and test it.
I will rename my LUA file and test it.
Raspberry Pi 2 with latest beta version
1x Aeon Labs USB Z-Stick S2
5x Fibaro Modules
1x Aeon Labs USB Z-Stick S2
5x Fibaro Modules
-
- Posts: 7
- Joined: Monday 29 August 2016 13:29
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.4834
- Location: Argentina
- Contact:
Re: Execute a script only once an hour
Thanks simnrg!
how i must to do, if i need wait 20seconds?
Thanks!!
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
- Egregius
- Posts: 2582
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Execute a script only once an hour
Use cron as a sheduler, much more options than lua.
This will do your stuff at exactly the 20th second.
Code: Select all
#!/bin/bash
while :
do
SECOND=$(date +%S)
if [ $SECOND -eq 20 ]
then
#Do your stuff
fi
sleep 0.9
-
- Posts: 7
- Joined: Monday 29 August 2016 13:29
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.4834
- Location: Argentina
- Contact:
Re: Execute a script only once an hour
Thanks man, but Im new in lua...
I post in script board my problem with all the code that i must modify.
I post in script board my problem with all the code that i must modify.
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: Execute a script only once an hour
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
}
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
- Posts: 485
- Joined: Thursday 17 September 2015 10:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
Re: Execute a script only once an hour
It can be done in a lot of ways. Below is how I did it on my system:
script_time_misc.lua
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
-
- Posts: 147
- Joined: Thursday 13 August 2015 13:36
- Target OS: NAS (Synology & others)
- Domoticz version: beta
- Location: Netherlands
- Contact:
Re: Execute a script only once an hour
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.
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.
Best regards Bert
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
-
- Posts: 722
- Joined: Friday 02 October 2015 12:12
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Finland
- Contact:
Re: Execute a script only once an hour
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.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.
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...
- Egregius
- Posts: 2582
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Execute a script only once an hour
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:
To run every then seconds that could be something like:
But, why every 10 seconds? This will cause all your device scripts to be executed every 10 seconds.
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
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
-
- Posts: 147
- Joined: Thursday 13 August 2015 13:36
- Target OS: NAS (Synology & others)
- Domoticz version: beta
- Location: Netherlands
- Contact:
Re: Execute a script only once an hour
Hi first of all thanks for the answers. I will try the bash script.
Furthermore
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
Furthermore
I have the following code:But, why every 10 seconds? This will cause all your device scripts to be executed every 10 seconds.
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
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
Best regards Bert
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
- Egregius
- Posts: 2582
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Execute a script only once an hour
My suggestion: Pass2php
-
- Posts: 147
- Joined: Thursday 13 August 2015 13:36
- Target OS: NAS (Synology & others)
- Domoticz version: beta
- Location: Netherlands
- Contact:
Re: Execute a script only once an hour
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?My suggestion: Pass2php
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.
Best regards Bert
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Who is online
Users browsing this forum: No registered users and 1 guest