Luup.is_night() fails

I call this lua extension in several of my scenes. Recently it started failing consistently. In the log I see:

LuaInterface::CallFunction_Scene Scene 46 failed attempt to call a nil value

Has anyone else seen this?

You have an error in your scene Lua somewhere. Use LuaView or another syntax-checking plugin to help you find it.

My scene code only contains two lines:

os.execute(“date >>/mnt/sda2/motion/Wildlife_motion.log”)
return luup.is_night()

If I try the first line in Apps → Develop Apps → Test Luup code (Lua) it works fine. The second line is exactly as it has been for probably a year or more but now it fails.

If any scene has a syntax error, all scenes fail to compile and cause a nil reference error.

I’ve seen this kind of thing before where a scene’s lua code gets corrupted somehow when it hasn’t been edited at all. I believe this is a long existing bug somewhere. I’ve looked at the code in all my scenes and so far I haven’t found anything that looks wrong.

Yes, I’ve seen that as well. Reload luup and then look through the startup messages. Errors in compiling scene and startup Lua are logged at that point (that’s when it’s done, of course). You’ll often find a more helpful message there, and if scene Lua has been corrupted, that too is usually obvious because the un-base64/decode fails with its own special error message.

Yes, I had already done that but the info in the log is not much help:

01 08/17/20 18:26:32.251 LuaInterface::LoadCode: [string “function scene_39()…”]:37: unexpected symbol near ‘�’ <0x7761d320>
01 08/17/20 18:26:32.255 JobHandler_LuaUPnP::Run cannot start lua with code:
function scene_39()
os.execute(“date >> /mnt/sda2/motion/frontPorch_motion.log”)
return luup.is_night()

These log entries would seem to point to scene 39. However, before this they pointed to scene 35 which I then deleted. I suppose it’s possible that these scenes are all corrupted in exactly the same way since they are all very similar. They are very brief - so I’ll try just re-entering each of them.

Unfortunately, the error message from Lua that Luup logs prints the first few dozen characters of the script being loaded, not the actual location of the error, so because Luup compiles all scene and startup Lua as a series of functions in a single large script, it will always show you the name of the first function in the generated string/script. But as you discovered, that is not the function with the error. To make matters worse, the line number is the line in the generated script, which you cannot see. It takes some real sleuthing to figure this out, which is why I recommended (and wrote) LuaView.

In any case, you have a non-supported character in your Lua code somewhere, that is clear from the message. Most often, it’s a “pretty” quote instead of a “real” quote.

Unfortunately, I can’t trust what you’ve posted in your reply as true to the code, because you haven’t formatted it properly and we know the forum corrupts quotes when you don’t. To format code in line, you use a single backtick ` (on US keyboards, its under the tilde ~) so the code looks like this. If you have a block of code like you are posting, it’s better to put three backticks on a line by themselves ``` then the code and then three more backticks on their own line.

-- That looks like this
luup.dowhatever( parameters )
return luup.is_night()

Only when using these formatting codes can anything you post be relied upon to judge the correctness of your code.

Actually when the compile of all the lua in scenes fails it is all dumped to the log and I think the line number refers to the line in the log when counting down from where the dump starts.

That’s correct.