Page 1 of 1
Lua script Set after 120 if ..?
Posted: Tuesday 18 October 2016 12:08
by henning84
Heii
I usually use blocky to program my functions, but want to use a function which is not possible in blocky. Thinking Lua is more advanced so I suppose it is possible here. But dont not know
I want this.
Dimmer 50 %
Wait 120 sek
dimmer 100% if dimmer not = off
wait 2 sek
Turn off Dimmer if dimmer not equal = Off
Is that possible..?
Re: Lua script Set after 120 if ..?
Posted: Tuesday 18 October 2016 20:09
by dannybloe
Sure... easiest way is using
dzVents.
You can do stuff like:
Code: Select all
domoticz.devices['myDimmer'].dimTo(50).for_min(2)
See
Timing options
Re: Lua script Set after 120 if ..?
Posted: Tuesday 18 October 2016 21:19
by henning84
domoticz.devices['myDimmer'].dimTo(50).for_min(2)
Here you dont have the "If dimmer not = OFF"
I want the script to wait 120 sec and check the state of a switch again, before the action..
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 8:08
by dannybloe
henning84 wrote:domoticz.devices['myDimmer'].dimTo(50).for_min(2)
Here you dont have the "If dimmer not = OFF"
I want the script to wait 120 sec and check the state of a switch again, before the action..
You cannot wait INSIDE a script for a(nother) device to change state. If a device changes state it will trigger a script. When that script is run you don't get state updates. Basically all states are frozen during script execution.
If you want to achieve something like this you have to do something with flags or something like that. Like you set a flag (a dummy switch or a variable or a dzVents persistent variable) and check in the next script cycle if that flag is set. So let's say you have a script that runs every two minutes. At some point you switch on a light in that script, you set a flag and the next time the script runs you check for the light and if the flag is set and then do your thing.
From what you have described I'm not able to figure out exactly what it is you want to do so I cannot help in a more detailed manner than just giving some ideas.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 8:33
by Egregius
Quite simple in my pass2php script:
Code: Select all
//Dimmer 50 %
sl($i['mydimmer'],50);
//Wait 120 sek
sleep(120);
//dimmer 100% if dimmer not = off
if($s['mydimmer']!='Off') sl($i['mydimmer'],100);
//wait 2 sek
sleep(2);
//Turn off Dimmer if dimmer not equal = Off
if($s['mydimmer']!='Off') sl($i['mydimmer'],0);
But I'd like to know the purpose of this action...
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 8:47
by dannybloe
Egregius wrote:Quite simple in my pass2php script:
But I'd like to know the purpose of this action...
Yes, you tell me! I have no idea what you want to achieve with this script. It doesn't make any sense to me.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 15:12
by henning84
Egregius wrote:Quite simple in my pass2php script:
Code: Select all
//Dimmer 50 %
sl($i['mydimmer'],50);
//Wait 120 sek
sleep(120);
//dimmer 100% if dimmer not = off
if($s['mydimmer']!='Off') sl($i['mydimmer'],100);
//wait 2 sek
sleep(2);
//Turn off Dimmer if dimmer not equal = Off
if($s['mydimmer']!='Off') sl($i['mydimmer'],0);
But I'd like to know the purpose of this action...
Perfect - and the sleep will not "freeze" domoticz..? Have read a thread where sleep in Lua will freeze domoticz while it sleep.
I know it dont make sense

But the script is for my light in the kitchen, when i turn off my tv in the evening - it turn on the light in the kitchen (Normaly i dont have any light on when i watch tv, so it is quite dark when the tv turn off.) A kind of "directing light"

But if i turn off the kitchen light when i leave the room, i dont want the script to turn on the light and turn it off 2 sec later.
Hope it make sense - my english is not so good.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 15:21
by dannybloe
henning84 wrote:Egregius wrote:Quite simple in my pass2php script:
Code: Select all
//Dimmer 50 %
sl($i['mydimmer'],50);
//Wait 120 sek
sleep(120);
//dimmer 100% if dimmer not = off
if($s['mydimmer']!='Off') sl($i['mydimmer'],100);
//wait 2 sek
sleep(2);
//Turn off Dimmer if dimmer not equal = Off
if($s['mydimmer']!='Off') sl($i['mydimmer'],0);
But I'd like to know the purpose of this action...
Perfect - and the sleep will not "freeze" domoticz..? Have read a thread where sleep in Lua will freeze domoticz while it sleep.
I know it dont make sense

But the script is for my light in the kitchen, when i turn off my tv in the evening - it turn on the light in the kitchen (Normaly i dont have any light on when i watch tv, so it is quite dark when the tv turn off.) A kind of "directing light"

But if i turn off the kitchen light when i leave the room, i dont want the script to turn on the light and turn it off 2 sec later.
Hope it make sense - my english is not so good.
Eh.. haha... not really (english is fine). This is getting interesting...
Can you tell me exactly the sequence/situation (if you want of course)
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 15:46
by henning84
Like i already told.
When i turn off my tv, it turn on the light in the kitchen.
I leave the road - 120 sec later the script dimme the light 100% (so it 100% next time i turn it on), 2 sec later it turn it off. - But some time i turn off the light my self, then i dont want the script to turn it back on and 2 sec later turn it off.
General it nice to make a IF in a "Set after XX" action..
Like
If phone OFF (ping)
Wait 2 min
If phone still off do "some thing here"
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 15:49
by Egregius
Turn off tv = turn on kitchen light. That I understand.
But why does it have to go to 100% for 2 seconds? Edit: now I get it

Because pass2php is run seperately of domoticz it doesn't freeze anything.
Because the lua part of that script is so small it's really lightning fast and can do anything you can imagine.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 15:53
by henning84
it cut be nice to have some more "advance" opportunities in Blockly script.
How do pass2php work..? seems not really I can find something about it.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:00
by Egregius
Blockly is for simple beginners stuff.
As soon as you want stuff really the way you want it you need to go scripting.
Because I didn't want to learn lua and knew php I came up with my pass2php script.
Afterwirds I did some speed tests and it seems by far the fastest solution to have advanced script.
With both blockly and lua the response times grow with each script you add, with pass2php not because for domoticz there's only one small lua script to handle, all the rest is done externally by the php enterpreter.
Script, code and help for pass2php in
http://www.domoticz.com/forum/viewtopic ... 23&t=12343 please.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:08
by henning84
Thanks - i will have a look at it.
I can not PHP or LUA - so i have to start from the scratch - then blockly it seems like the easiest but also the most restrictions :/
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:17
by Egregius
I find blockly difficult and unclear

Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:21
by henning84
advantage Blockly you are not required to know the code, just drag and drop. Ih PHP/LUA i have to write all the code by you self

Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:25
by henning84
will the pass2php work on windows..? I run Domoticz on a windows 7.
Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 16:29
by Egregius
Ieuw
Probably it will, if you get php working. Maybe with something like wamp server? Never used php on windows before. Linux rules for server applications

Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 17:48
by henning84
i am 100% windows man - sorry

Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 18:59
by Egregius
You do have Raspberry indicated as target system...
And that just because you don't know any better

Re: Lua script Set after 120 if ..?
Posted: Wednesday 19 October 2016 19:36
by henning84
i start on a Raspberry - but move to Windows. I have a server on 24/7 anyway.