How can I get a scheduled scene to run dependent on a status of switch

I have a scene which runs on a schedule trigger. Before running at the scheduled time I would like to check the status of a switch. if that switch is on then I don’t want the schedule to run. How can I achieve this.

I cant have a schedule and switch as a trigger as then the scene would run if either condition is true. I tried adding some code in the LUA section of the scene, but by then the scene has already run.

Many thanks

If your Lua code returns false, then the scene will NOT run, so there must have been some error in the code.

Conditional Scene Execution: Some Examples

I used this and it works:

local dID = 32 -- Device ID of your VirtualSwitch 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 understand all lines apart from the last one. How does that work?

It doesn’t. At least, not the way that I suspect it was intended

The problem with

return ((status == "1") == allow) 

is that if the switch status is “0” and allow has, in fact, been set to false, then this statement returns true, and the scene is run.

What you probably want is

return (status == "1") and allow 

It doesn’t. At least, not the way that I suspect it was intended

The problem with

return ((status == "1") == allow) 

is that if the switch status is “0” and allow has, in fact, been set to false, then this statement returns true, and the scene is run.

What you probably want is

return (status == "1") and allow 

Thank you Akbooer. Your prediction was correct. I tested that scenario of status ) and allow = false and I did get a false positive. I implemented your code and it works perfectly. Thank you very much.

The logic can get confusing in LUA … especially if you have more than one variable you are testing.

You might consider using PLEG for your AND logic and much more.