Conditional Scene Execution

The code that I use to either execute or cancel a scene from running is no longer working.

I recently added a new App, the MyQ garage app, and had some trouble with getting it set up properly, I also recently had some trouble getting Google Home to sync my devices. I was able to resolve both problems and have no reason to believe that either are related to my current problem.

The code in question, and several variations, have been running just fine for years now and I am lost as to why all of a sudden it no longer works. The idea is that a scene is scheduled to run at say 6am, my code simply checks the status of a device and either allows or does not allow the scene to run

local dID = 19               -- Device ID of your VirtualSwitch (Guest Mode)
local allow = true       -- true runs scene if switch is on, false blocks it
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
return ((status == "1") == allow)

I have even tried deleting the code and simply using:
return false
and also i tried
return true

in all cases the scene just executes as if there were no code. When I debug it in ZeroBrane I get an error:
unexpected symbol near ‘return’
I have verified that there are no extra characters or symbols. As i said before i have deleted the code and typed out return false. still the scene executes.

I am looking for any help in either fixing this or where to start looking for the problem.

This may well be the case, but should, nevertheless, review it, as per this thread:

It’s not the source of your problems, and your existing code may, indeed, be what you intend, but it’s worth a read, anyway.

To the matter in hand, you mention looking for extra characters, but perhaps you have one too few… is there a newline at the end? There probably should be.

@akbooer Thanks for posting that article. I have always been a little confused by this code snippet but it has worked for me so I just moved on. I will digest this new information and rewrite the code to be clearer for myself and just all around better.

As you said, that is not the source of the problem as proved by changing the entire code to :
return false

I did not have a newline at the end, so I edited my code by going to the last character and pressing enter, giving me a single empty line at the end of my code. This unfortunately did nothing but without understanding the why I will do this in my code from now on.

@rigpapa I understand your desire to “stay out of it” but advice coming from people like you and @akbooer is how we novices manage to get things cobbled together. Just want you to know how much your opinions and comments are valued.

2 Likes

When i debug in ZeroBrane i get the error "Unexpected symbol near ‘return’. When i run the scene on Vera the lights turn on. The expected behavior is that a code which returns false would stop the scene from executing. This was working as expected until very recently.

Well, I certainly commend you on reading @rigpapa’s post and taking action!

Alas, I still have no solution for your problem, but since it used to work, and doesn’t now, I can only suggest a call to the help line… you never know! Did you upgrade the system, or something, meanwhile?

Just as a footnote to your coding change (which is a great improvement) I would just raise a very personal point of view, since you have, unwittingly, touched a raw nerve of mine…

Your code is broadly of the generic form:

if logical_expression then
    x = true
else
    x = false
end

in which the if statement is, in fact, completely redundant. More concise, entirely accurate, and totally different from the ‘allow meme’ is simply

x = logical_expression

or, in your specific case

return status == "1"

Again, sorry to be of little help.

AK

@akbooer Actually that was helpful; I learned that return must be a boolean variable.

local dID = 19               -- Device ID of your VirtualSwitch (Guest Mode)
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
return status == "1"

So return is used to pass a result back to the calling code, the system. That something is a boolean, meaning it can only be true or false. == is the logical test. So in my case we will be returning the result of the logical test status == "1" which will either be true when Device #19 is on or it will be false when it is off. In theory if my code returns false to the system code, then the scene will terminate execution, if it returns true then the scene will continue to execute in the order determined by the Vera System Code.

Assuming i got it right, then i am sure this is all very obvious to many readers but i hope it helps someone just starting out. Also to people just stating out: I recommend you look into ZeroBrane, it has helped my understanding considerably.

I have just noticed that I can not load the page:
http://192.168.1.5/port_3480/data_request?id=lr_EventWatcher&report=scenes
“Handler failed”

This strikes me as related. I have reached out to Customer Support but not sure how long it will take for them to respond these days.

I found some code, which I believe was written by @akbooer, which when run in AltUI can give a list of possible errors found in Scene LUUP codes.

local json = require "dkjson"
for i,s in pairs (luup.scenes) do
print (i, s.description)
local _,j = luup.inet.wget ("127.0.0.1:3480/data_request?id=scene&action=list&scene=" .. i)
local sc = json.decode (j)
if sc and sc.lua then 
    local ok, err = loadstring (sc.lua, "scene_code_test")
    if ok then print ('',"ok") else print ('', err) end
end
end

I used that and found a number of places where I had code like:
local dID=19
instead of:
local dID = 19
I find it hard to believe that these were causing any real problems but i “corrected” them. I also found that this code flagged any scene with an empty line at the end.
[string "scene_code_test"]:1: unexpected symbol near '<eof>'
removing the empty line seemed to make that go away, (this runs counter to the advice above about adding a blank line).

In doing all of this I found that i had a scene listed and visible in AltUI which did not show in the regular Vera UI. I deleted that scene. I noticed the next morning that my Conditional Scene code(s) were now working as expected (at least i think they are) but i still can not access
http://192.168.1.5/port_3480/data_request?id=lr_EventWatcher&report=scenes
instead i get the Handler failed error.

No word from the Customer Service yet.

Yes, indeed, here…

However, since then, there are also other tools such as @rigpapa’s LuaView

The Handler failed error, looks to be a failed plugin startup issue? Is there nothing in the logs which helps to diagnose this?

Best Home Automation shopping experience. Shop at getvera!

© 2020 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules