Trouble with Lua - "Test Luup code" and Lua in a scene behave differently (??)

Hi all

Here’s a strange one…

I am trying to control a Global Cache IP2IR device. I have written the Lua code to send an IR command to turn my Pioneer Amp on.

I pasted the code into the Apps/DevelopApps/Test Lua Code box and BINGO it works perfectly… the Pioneer Amp turns on.
But when I paste this same code into a scene Lua code (no other code present), it does not work.

any ideas anyone…? (thanks)

This code:

local socket = require(“socket”)
tcp = assert(socket.connect(“192.168.1.181”, 4998))
tcp:send(“sendir,1:3,1,39000,5,1,360,180,22,67,22,22,22,67,22,22,22,22,22,67,22,22,22,67,22,22,22,67,22,22,22,67,22,67,22,22,22,67,22,22,22,22,22,22,22,67,22,67,22,67,22,22,22,22,22,67,22,67,22,67,22,22,22,22,22,22,22,67,22,67,22,22,22,989,360,180,22,67,22,22,22,67,22,22,22,22,22,67,22,22,22,67,22,22,22,67,22,22,22,67,22,67,22,22,22,67,22,22,22,67,22,22,22,67,22,67,22,22,22,22,22,67,22,67,22,22,22,67,22,22,22,22,22,67,22,67,22,22,22,22,22,989\r”)
luup.sleep(100)
tcp:close()

Do you have this code elsewhere?

The variable tcp should be declared local, else it will be shared amongst scenes and Test and Startup Lua code.

This may well not be your problem, but it does need fixing anyway.

Are you sure you need the luup.sleep()? … another potentially damaging Luup primitive.

Also note that there have been others with similar problems previously…

Lua code working in test window but not in vera scene

…although it may not be that particular issue either (earlier firmware.)

thank you for the quick reply.

This code in another scene works fine (its controlling an NEC projector)

local hex = {
“02”, “00”, “00”, “00”, “00”, “02”
}
local binary = “”;
for i, v in ipairs(hex) do
binary = binary … string.char(tonumber(v, 16))
end
local socket = require(“socket”)
tcp = assert(socket.connect(“192.168.1.70”, 7142))
tcp:send(binary)
luup.sleep(100)
tcp:close()

thanks for the hints over the local variable and the delay… I will amend.

I have the latest firmware (yesterday actually)… but I will take a look at the other thread, thanks

Another example including luup.sleep() …

Confused why “Test Luup Code (Lua)” works beautifully, but not as a Luup

…but not quite your problem either?

Do try searching the forum… most things have happened previously!

Oh, wait just a cotton-picking-minute… my NEC Projector scene is not working anymore (can’t remember when it last did, but it DID).

that implies that something has changed in the firmware that means TCP commands coded as LUUP might not work in scenes even if they work in the test…

Ouch.

I?'ll look at those other threads… thanks

Update: (and not a good one)…

to recap… sending a string out of a TCP socket via TestLuaCode works fine. Sending the same string using the same code out of a Luup code within a scene does not work.

So, I just did a PacketSender sniff to compare the two…

TestLuaCode sends the string as expected to PacketSender sniffer
The same code from within a scene SENDS NOTHING AT ALL.

Next step the logs… oh those horrible logs… :frowning:

I take most of the code related to sending a Global Cache command and put it in a function that I load as part of Edit Startup Lua.
Then my individual scene calls are a lot smaller…

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

And then the scene is just:

SendiTachCommand("192.168.0.115,4998,sendir,1:2,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") -- Turn On Panasonic TV

Thanks. I like that idea

I have another update. I have several scenes that should only run at night. The first line of my scene LUA code is ?return luup.isnight()?. This used to work perfectly.

Now, all my night time scenes are also running during the day.

I therefore suspect that it?s not the fact that I had TCP in my scene LUA but it might be ANY scene LUA code that is not running.

In other words I am suspecting that LUA code has stopped working in scenes for some reason.

Anyone else?
Thanks.

SOLVED !!! :slight_smile: :slight_smile: :slight_smile:

Ok here’s the scoop… I have about 50 scenes. ONE of them had a syntax error in it (a missing “-” in a comment), and it transpires that having a syntax error in ANY Lua code in a scene will stop ALL of the scenes from running Lua code.

ha ha! :slight_smile:

Interesting, I have same problem with simple code:

luup.call_action("urn:micasaverde-com:serviceId:HaDevice1", "ToggleState", {}, 32)

It works from test luup code window, doesn’t work from scenes.
Checked all scenes and disabled any lua coding (or put “–” to make it “comment” temporarily), but it didn’t help. Still not work from scene.
Any ideas?

Something in the Startup Lua perhaps … ?

That was the issue, I forgot that I have some code in Startup Lua.
Now everything works, as well as some other scene for which I completely lost hope (i thought that I can’t track proper variable to get for this scene, while it seems the real reason was erratic code in startup lua)
Thanks!

Strange that it was not reported as “error in lua for scenes” message on the blue bar on top.
Edit: Not really strange after looking into notifications settings. It was disabled in this unit for some unknown reason :frowning: