Page 1 of 1
Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 12:10
by pcc9
Hi,
Here is my problem:
I try to send simple command to switches through Lua scripting in order to "dupplicate" a command when it is triggered One. Short explanation:
I have blinds interfaced with a Chacon 54754 switch and a RF433 remote control. I have created a blind switch "Fixe Haut" that send same order that the remote control. It works well but... My blinds requieres an impulse whereas the Chacon module maintain the order for 2 minutes. The solution is to send again the same command to make the Chacon switch to release the order. This mean I need to push the button twice for the command appropriatelly.
First solution:
I created a scenario in witch in included my Domoticz switch twice with a delay of 1 seconds and it works ... except when I use the remote control.
Second Solution:
I tried to create a Lua Script to trigger the event coming from the Remote Control or the Domoticz switch "Fixe Haut".
here is the code:
Code: Select all
commandArray = {}
if (devicechanged['Fixe haut'] == 'Open') then
print (" ---- Envoi commande Open sur Fixe Haut" )
oss = "curl 'http://192.168.0.39:8080/json.htm?type=command¶m=udevice&idx=119&nvalue=1&svalue=Open'&"
os.execute(oss)
-- commandArray={['Fixe haut']='Open AFTER 3'}
elseif (devicechanged['Fixe haut'] == 'Close') then
print (" ---- Envoi commande Closed sur Fixe Haut" )
oss = "curl 'http://192.168.0.39:8080/json.htm?type=command¶m=udevice&idx=119&nvalue=0&svalue=Closed'&"
os.execute(oss)
-- commandArray={['Fixe haut']='Closed AFTER 3'}
end
return commandArray
I tried 2 versions to send the command : CommandArray or os.execute.
The script is executing, and the switch change is triggered ( I can see the comment in the log ), but nothing happen.
I tried to execute the http command directly in my Chrome Brower and I have a "OK" as a result in the brower ... but no activation of the switch, and when I look in Domoticz, the state of the Fixe Haut switch has changed.
So it seems the order has not been send to the switch through the RFCOM RF433 module.
My domoticz is running of Raspberry Pi.
What did I do wrong?
Thanks for your help,
regards,
Pierre
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 13:31
by nayr
remove the '&" at the and and just have '"
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 13:54
by pcc9
Hi,
thanks !
Now I have one error : Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_device_FixeHaut.lua has been running for more than 10 seconds
what could be the reason? Is the system entering in a loop?
regards,
Pierre
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 14:02
by nayr
Your curl request is taking too long to complete, you could try adding this switch to the command:
Code: Select all
-m, --max-time SECONDS Maximum time allowed for the transfer
but really, your doing it wrong... first of all there is a built in OpenURL Command.. ie:
commandArray['OpenURL']='192.168.0.39:8080/json.htm?type=command¶m=udevice&idx=119&nvalue=1&svalue=Open'
but even that is horribly wrong because all you really need to do is:
commandArray['Switch']='Open'
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 14:52
by pcc9
THanks for your help.
So I simplified the code to take your remarks.
Here is the code:
commandArray = {}
if (devicechanged['Fixe haut'] == 'Open') then
print (" ####### ---- Envoi commande Open sur Fixe Haut" )
-- oss = "curl '
http://192.168.0.39:8080/json.htm?type= ... value=Open'"
-- os.execute(oss)
commandArray['Fixe haut']='Open AFTER 1'
elseif (devicechanged['Fixe haut'] == 'Close') then
print (" ####### ---- Envoi commande Closed sur Fixe Haut" )
--oss = "curl '
http://192.168.0.39:8080/json.htm?type= ... lue=Closed'"
--os.execute(oss)
commandArray['Fixe haut']='Closed AFTER 1'
end
return commandArray
The code is executing but nothing happen ( I can see my commnets in the logs ). The second command is not send. Do I need to plan with the AFTER number in second where there would be a minimum number of seconds to wait ?
regards,
Pierre
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 14:57
by nayr
I am not sure what your script is trying to do...
If Fixe Haut == Open; set Fixe Haut = Open AFTER 1? your logic is not logical.. its already open and your trying to force it open? At best this is a loop that will trigger the script again infinitely.
now If Fixe Haut == Open; set Fixe Haut = Closed AFTER 1 makes sense..
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 15:04
by pcc9
Chacon switch works as such:
when push On = Open switch for 2 minutes, if you push On again before the 2 minutes it will stop sending the On signal.
As I need to send an Impulse, I need to send On, 1 or 2 seconds after the "On" command has been triggered.
To summarize, I need to send the command a second time when I trigger the command.
When ON is triggered, I need to send ON again
When OFF is triggered I need to send OFF again.
It works when put this sequence in a scenario where I create a scene and include twice the switch , but I can't make it work in lua.
It also works when I click two times on the switch icon in Domoticz.
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 15:14
by nayr
well this is going to execute indefinitely, not just once.. because when you send the Open AFTER 1 it will trigger the script to execute again because you just change its state.
your going to need alot more logic and LUA dont like waiting, it dont even have a wait command
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 15:23
by nayr
try a bash script like this, bash wont mind waiting.. I wont use LUA for any scripts that dont execute instantly.. you can totally hang the event system with such scripts... for example I have scripts that go through a list of PTZ presets for a few mins after an event, has to be forked out to something external.. lua is not capable of doing it.
Code: Select all
#!/bin/bash
sleep 1
if [ ! -f "/tmp/haut-block" ] && [$1 == "Open"]; then
touch /tmp/haut-block
curl 'http://192.168.0.39:8080/json.htm?type=command¶m=udevice&idx=119&nvalue=1&svalue=$1'
else if [ -f "/tmp/haut-block" ] && [$1 == "Closed"];
rm /tmp/haut-block
curl 'http://192.168.0.39:8080/json.htm?type=command¶m=udevice&idx=119&nvalue=1&svalue=$1'
fi
call it FixeHaut.sh and call it with
script:///path/to/FixeHaut.sh Open, it has enough logic to prevent looping and it'll sleep for a second before calling the request.
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 15:28
by pcc9
I was maybe a bit naive, but I thought that the first condition would not be true if there was no "state" change :
I thought : if (devicechanged['Fixe haut'] == 'Open') then was triggered only if there was a change meaning coming from Closed to Open.
If this is not the case then we would need to remember the last status befre the change : when I receive On and I was already On then...
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 15:35
by nayr
devciechanged scripts are triggered for any device change, even if the state is the same as before.. so your going to need to track the state, there is no way to send in a device state without triggering any events watching for it.. you can store the last known state in a uservariable.
your also not going to be able to wait for a second or two with a devicechanged script, those scripts will execute instantly and should.. any attempts to make it sleep/wait are likely to hang your event system and will result in bad behavior.. I dont know how accurate the AFTER 1 is going to be, I would not be surprised if it came in a few seconds later than you expected.. Ive seen reports of people not getting tight enough loops for devices needing specific timing and I think I encountered it my self.
can you break your entire device out into a script? externally you can issue the ON command directly to it twice with the timing required and only have a single event execute within domoticz? I had to do something like this for my security lighting, I can disable the built in motion sensor by turning it off for a second and then back on, I was never able to get lua to do the timing right.. but calling a bash script that shut it off, waited a second, then turned it back on worked perfectly fine.. I think the event system can add a variable delay thats hard to account for.. especially as the number of scripts watching for events grows over time.
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 16:47
by pcc9
Ok clear.
I will try to go then with your bash script, the only issue may come from the remote device I also use on the same blind. Cause then the script won't be executed on the ON coming from the remote device but only on the Domiticz switch ( even if they are on the same RF 433 code )
I was also thinking about a UserVariable by blind that could get the current status of the blind and then change the logic:
if devicechange blind == Open and CurrentStatus == Closed then
CurrentStatus = Open
CommandArray [blind] = Open
end
if devicechange blind == Closed and CurrentStatus == Open then
CurrentStatus = Closed
CommandArray [blind] = Closed
end
Of Course I will code with the correct syntax. Do you think it could work?
thanks a lot,
regards,
Pierre
Re: Lua script doesn't send commands to switch
Posted: Friday 05 February 2016 18:26
by oliviers
May I suggest another solution :
1. Completely dissociate your remote from the Chacon switch
2. Create a (real) switch in Domoticz for your remote
3. Now create two scripts: The first one, which you will assign to "action on", will execute quickly (as to avoid disturbing Domoticz), and will just launch a second script, in background, with all you curl commands and all the delays you wish:
Script1 will look like:
Code: Select all
#!/bin/bash
/home/pi/domotics/scripts/script2.sh & <== Please note the "&" which will ensure that 'script2' is launched in background, thus allowing 'script1' to proceed and complete without waiting
Script2 will look like:
Code: Select all
#!/bin/bash
curl .....
sleep xxxx
curl ....
(I used this to solve a similar problem.... Turn off/on my homecinema, which requires 5 commands and delays in between)
Hope this helps,
Oliviers
Re: Lua script doesn't send commands to switch
Posted: Saturday 06 February 2016 14:59
by jmleglise
2 comments :
1/ testing if the device that changes is 'Fixe haut' and if it changes to the value of 'Open' shall be written :
Code: Select all
if (devicechanged['Fixe haut'] == 'On' and otherdevices['Fixe haut'] == 'Open') then
2/ be carefull, to open or to close, sometime you must send On or Off command. So are you really sure of the command you must send ?
For the blind switch I use in Domoticz (Type RFY, subtype RFY) :
I must send On and Off. And I found , that the command are inverted !!! On stand for closing. and Off is for opening.
Plus, the states are Open, Closed, stopped.
So try this :
script_device_duplicate.lua
Code: Select all
commandArray = {}
if (devicechanged['Fixe haut'] == 'On' then
if otherdevices['Fixe haut'] == 'Open' then
print (" ---- Envoi commande Open sur Fixe Haut" )
commandArray={['Fixe haut']='Off AFTER 3'}
elseif otherdevices['Fixe haut'] == 'Closed' then
print (" ---- Envoi commande Closed sur Fixe Haut" )
commandArray={['Fixe haut']='On AFTER 3'}
end
end
return commandArray