Author Topic: Help w/ IR code delays in luup for scene with multiple IR codes for same devices  (Read 159 times)

Offline Nicomonster

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
Hi All, long time Vera user, but newbie when it comes to Lua/Luup/scripting.  I recently discovered how to incorporate luup code into scenes, but I have pretty much used examples found in the forums that matched the devices I was trying to control.  Namely, a Panasonic TV in my kitchen and the Monoprice 6-zone Whole Home Audio receiver.  The luup code for both of these devices are sent through an IP2IR Itach for my Panasonic TV and IPSerial Itach for the Monoprice.  I am now able to have my wife ask Alexa to turn on TV in the kitchen as well as turn on Kitchen Speakers.

I also have the same Panasonic TV in my kids play room connected to an older Pioneer receiver.  Both of these devices are controlled through the same IP2IR itach as the kitchen Panasonic TV.  I have a few different gaming consoles connected into the Pioneer including a PS3 in HDMI 1, Classic Super Nintendo in HDMI 2 and a Mac Mini in HDMI 3.

I am trying to create a scene that will 1) Turn on Panasonic TV, 2) Switch Panasonic TV to HDMI 1 (in case its not as my kids often touch buttons on remotes), 3) Turn on Pioneer Receiver and 4) Turn Pioneer Receiver to HDMI 1.  Then I want my kids to be able to ask Alexa to Turn On Super Nintendo and everything turns on and switches over for them.

I have all 4 necessary IR commands in the luup code that do work individually.  However when I add all 4 codes and test the code on Vera, it will only turn on the TV and the Receiver, but it will not switch the HDMI ports on either the TV or receiver.  When I press the test button again, it then will change both HDMI ports.  I think I need to insert delays between the 4 IR commands as it takes a moment for the TV and the receiver to turn on.  I have tried to research luup.call delay for the past day or so, but I cannot figure out what the commands I am supposed to add for this.

In other control programs I have used (Simple Control and IRule) I can set the delay between commands at whatever interval I want.  I cannot figure it out here though.  Anyone have any tips or pointers for me on how to achieve what I am trying to do?  Here is the code that I am using for simply turning on the Panasonic TV and then trying to switch to HDMI 1.  When I test this, it will only turn on the TV....when I test a 2nd time, it will then switch the input to HDMI 1.....

local CommandtoSend      
      
CommandtoSend= 'sendir,1:1,1,37000,1,1,128,63,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,48,16,48,16,48,16,48,16,16,16,16,16,16,16,48,16,48,16,48,16,48,16,48,16,16,16,48,16,2712,\r'      
      
print ("test:", CommandtoSend)      
 -- Connect and Send Command      
      
local socket = require("socket")      
host = "10.0.1.124" --IP2IR Living Room      
c = assert(socket.connect(host, 4998))      
c:settimeout(5)      
      
local sres, serr = c:send(CommandtoSend)      
print("Send:", sres, serr)      
local data, rerr = c:receive(5)      
print ("Receive:", data, rerr)      

local CommandtoSend      
      
CommandtoSend= 'sendir,1:1,1,37000,1,1,132,67,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,17,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,51,17,17,17,51,17,17,17,17,17,51,17,17,17,51,17,51,17,17,17,17,17,2846,\r'      
print ("test:", CommandtoSend)      
 -- Connect and Send Command      
      
local socket = require("socket")      
host = "10.0.1.124" --IP2IR Living Room      
c = assert(socket.connect(host, 4998))      
c:settimeout(5)      
      
local sres, serr = c:send(CommandtoSend)      
print("Send:", sres, serr)      
local data, rerr = c:receive(5)      
print ("Receive:", data, rerr)      

c:close()



Any help is greatly appreciated and thanks in advance.  My kids will thank you too :)

Offline kigmatzomat

  • Sr. Member
  • ****
  • Posts: 365
  • Karma: +21/-0
No direct ir experience but I believe you want to add a call_delay in your luup

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions

I have an old example from ui5, should work on ui7.

------
function buzzer()
   luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='5',Data='112 4 6 1 2'},1)
end

luup.call_delay( 'buzzer', 1) -- Call the buzzer function after a delay of 1 seconds

Offline wilme2

  • Hero Member
  • *****
  • Posts: 1234
  • Karma: +29/-13
I have all 4 necessary IR commands in the luup code that do work individually.  However when I add all 4 codes and test the code on Vera, it will only turn on the TV and the Receiver, but it will not switch the HDMI ports on either the TV or receiver.  When I press the test button again, it then will change both HDMI ports.  I think I need to insert delays between the 4 IR commands as it takes a moment for the TV and the receiver to turn on.  I have tried to research luup.call delay for the past day or so, but I cannot figure out what the commands I am supposed to add for this.

Yes, you need to wait until the TV and Receiver power on and are ready for the next command.  Here is an example where I turn on a TV and wait 10 seconds to change the input.  You will have to experiment with how much time your device needs - but 10 seconds or more is not uncommon...  call_delay is the best way to go, [you can test with SLEEP(10), but don't use SLEEP in production] but it takes some work - you need a function that takes only one parameter.  Which means your whole iTach command is a single string, and you then have to parse it out.  Example below probably took me two days to figure out.   :o

Function as part of Startup LUA:

Code: [Select]
function SendiTachCommand(ParamString)

local IPAddress,Port, CommandtoSend = string.match(ParamString,"(%d+.%d+.%d+.%d+),(%d+),(.+)")
print (IPAddress)
print (Port)
print (CommandtoSend)

local socket = require("socket")
c = assert(socket.connect(IPAddress, Port))
c:settimeout(5)

local sres, serr = c:send(CommandtoSend)
print("Send:", sres, serr)
local data, rerr = c:receive(5)
print ("Receive:", data, rerr)

  c:close()

end

Now scene to execute.

Code: [Select]
SendiTachCommand("192.168.0.121,4998,sendir,1:1,1,38000,1,1,172,172,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,64,22,21,22,21,22,64,22,64,22,21,22,21,22,64,22,21,22,64,22,64,22,21,22,21,22,64,22,64,22,21,22,1820,\r") -- Turn On Samsung TV in Bedroom

luup.call_delay("SendiTachCommand",10, "192.168.0.121,4998,sendir,1:1,1,38000,1,1,173,173,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,65,21,21,21,21,21,65,21,21,21,65,21,65,21,65,21,21,21,65,21,65,21,21,21,65,21,21,21,21,21,21,21,1832,\r") -- Switch Samsung TV to Input HDMI 1
« Last Edit: December 03, 2018, 02:23:20 pm by wilme2 »
Long Live UI5!  (OK finally made the switch to UI7 in January 2018, and am pretty happy....)

Offline shallowearth

  • Hero Member
  • *****
  • Posts: 985
  • Karma: +47/-9
If you only need to pause like 2-3 seconds for the TV to fire up just use luup.sleep(2000)  it is in milliseconds so 2000 is a two minute sleep.
You don't want to do this for long pauses, then you need to use the function calls with delays like examples here.  But for a quick pause, you can get away with a quick sleep.

You also don't need re-connect the socket between your two commands. 
Here is your code with a 2 second delay till the second command is sent:


local CommandtoSend     
     
CommandtoSend= 'sendir,1:1,1,37000,1,1,128,63,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,48,16,48,16,48,16,48,16,48,16,16,16,16,16,16,16,48,16,48,16,48,16,48,16,48,16,16,16,48,16,2712,\r'     
     
print ("test:", CommandtoSend)     
 -- Connect and Send Command     
     
local socket = require("socket")     
host = "10.0.1.124" --IP2IR Living Room     
c = assert(socket.connect(host, 4998))     
c:settimeout(5)     
     
local sres, serr = c:send(CommandtoSend)     
print("Send:", sres, serr)     
local data, rerr = c:receive(5)     
print ("Receive:", data, rerr)     

luup.sleep(2000)
     
CommandtoSend= 'sendir,1:1,1,37000,1,1,132,67,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,17,17,17,17,51,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,51,17,51,17,17,17,51,17,17,17,17,17,51,17,17,17,51,17,51,17,17,17,17,17,2846,\r'     
print ("test:", CommandtoSend)     
 -- Connect and Send Command     
     
   
sres, serr = c:send(CommandtoSend)     
print("Send:", sres, serr)     
data, rerr = c:receive(5)     
print ("Receive:", data, rerr)     

c:close()