Execute a script only once an hour

Post Reply
Noudje
Posts: 18
Joined: Tuesday 26 January 2016 0:07
Target OS: Raspberry Pi
Domoticz version: Beta

Execute a script only once an hour

Post by Noudje » Tuesday 26 January 2016 0:34

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?

malarcy
Posts: 56
Joined: Sunday 15 November 2015 14:36
Target OS: Linux
Domoticz version:
Contact:

Re: Execute a script only once an hour

Post by malarcy » Tuesday 26 January 2016 1:12

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

User avatar
nayr
Posts: 430
Joined: Tuesday 11 November 2014 19:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: Execute a script only once an hour

Post by nayr » Tuesday 26 January 2016 1:34

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

simonrg
Posts: 1071
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Execute a script only once an hour

Post by simonrg » Tuesday 26 January 2016 1:38

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

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

Re: Execute a script only once an hour

Post by jannl » Tuesday 26 January 2016 10:13

Or create a shell script, update Domoricz via json and fire the script via crontab

Noudje
Posts: 18
Joined: Tuesday 26 January 2016 0:07
Target OS: Raspberry Pi
Domoticz version: Beta

Re: Execute a script only once an hour

Post by Noudje » Tuesday 26 January 2016 10:49

Thnx for all the replies!!!

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

javed222pi
Posts: 9
Joined: Monday 29 August 2016 13:29
Target OS: Raspberry Pi
Domoticz version: 3.4834
Location: Argentina
Contact:

Re: Execute a script only once an hour

Post by javed222pi » Monday 19 September 2016 20:43

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

User avatar
Egregius
Posts: 2664
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Execute a script only once an hour

Post by Egregius » Monday 19 September 2016 23:58

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.

javed222pi
Posts: 9
Joined: Monday 29 August 2016 13:29
Target OS: Raspberry Pi
Domoticz version: 3.4834
Location: Argentina
Contact:

Re: Execute a script only once an hour

Post by javed222pi » Tuesday 20 September 2016 2:00

Thanks man, but Im new in lua...
I post in script board my problem with all the code that i must modify.

dannybloe
Posts: 1433
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Execute a script only once an hour

Post by dannybloe » Tuesday 18 October 2016 20:35

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.

BakSeeDaa
Posts: 551
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version:

Re: Execute a script only once an hour

Post by BakSeeDaa » Wednesday 19 October 2016 16:34

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

bertbigb
Posts: 137
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

Post by bertbigb » Wednesday 07 December 2016 23:38

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.
Best regards Bert

Synology DS1515+ - DSM 6.1
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

Nautilus
Posts: 753
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Execute a script only once an hour

Post by Nautilus » Thursday 08 December 2016 1:03

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...

User avatar
Egregius
Posts: 2664
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Execute a script only once an hour

Post by Egregius » Thursday 08 December 2016 5:10

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&param=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&param=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.

bertbigb
Posts: 137
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

Post by bertbigb » Thursday 08 December 2016 11:34

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&param=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
Best regards Bert

Synology DS1515+ - DSM 6.1
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

User avatar
Egregius
Posts: 2664
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Execute a script only once an hour

Post by Egregius » Thursday 08 December 2016 15:01

My suggestion: Pass2php :P

bertbigb
Posts: 137
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

Post by bertbigb » Friday 09 December 2016 11:58

My suggestion: Pass2php :P
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.
Best regards Bert

Synology DS1515+ - DSM 6.1
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


Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest