Hi All - I have the following code which I use as a trigger linked to the front door sensor. It triggers a scene called ‘after work’ where a certain selection of lights come on.
The code runs like this - when the front door is opened and it is ‘night’ - the scene is run and also, irrespective of the time of day, Prowl is updated.
What I would like to do is amend the code so the scene is only executed if it is between 20 mins before sunset and 10pm -… and will only run once per day.
Install a virtual switch, setup scene #1 that activates the virtual switch at (sunset - 20 min),
setup scene #2 that deactivates the virtual switch at 10pm.
Setup scene #3 (triggered by your event) that checks if the virtual switch is on.
If the virtual switch is on, run your code and set the virtual switch to off.
You can add an event to your existing scene to trigger at 20 minutes before sunset.
In the LUUP for the scene you can set an ENABLE flag (Just a variable pick any name). Also return FALSE so the commands for the SCENE do not run and niether will the SCENE level LUUP code.
In the same scene you can have another event the runs at 10:00 PM. In the event level LUUP for that event you can disable the the ENABLE flag. Also return false.
In that same scene you also have an event for your door switch.
In the event level code for it you will return TRUE if the ENABLE flag is set and false otherwise.
You will also set the ENABLE flag to false so it does not trigger again.
The key here is that all events share the same variable space. So if you set a variable to a value in one event you can see it in another event.
Then add commands and/or code to the SCENE level LUUP.
Ahh right. That makes sense. Did some more digging and found the service id in the .json file for Vswitch.
urn:upnp-org:serviceId:VSwitch1
Now that is sorted - I just need a line of code to set the Vswitch to “off” as per the following
local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", 24)
if (lul_tmp == "1") then
return true
*** NEED LINE HERE THAT SETS THE VSWITCH TO 'OFF'***
else
return false
end
And like I said earlier … you could do this all without a virtual switch.
Just use a variable in your LUUP code. The only thing you loose is that it is not persistent when the power goes off … But it’s simpler. Does it really matter if the light comes on a second time because Vera rebooted ?
Before Sunset Event Luup Code
ENABLE = true
return false
In your Light Control Scene add an event for your door sensor:
In the luup for the Door Event:
DOOR_EVENT = 1
return true
Add two timers for this Scene as well.
[ol][li]One for 20 minutes before sunset[/li]
[li]One for 10:00[/li][/ol]
in the Scene level luup code section:
if (DOOR_EVENT == 1) then
DOOR_EVENT = 0
if (ENABLED == 1) then
ENABLED = 0
return true
end
else
local Hr = tonumber(os.date("%H"))
if (Hr == 22) then
ENABLED = 0
else
ENABLED = 1
end
end
return false
Note: If the Scene level luup code returns true … the Commands for the scene are executed.
Note: If Vera reboots after sunset, but before your scene is triggered the scene will not trigger … that is because the ENABLED flag is not persistent. If that’s important … with a little more code you could deal with this … A hint … save the state on some previously unused variable name on some device, like your sensor or light. That’s what the virtual device is giving you … but the Virtual device shows up on the UI.
Of course you could send all of this extra stuff to a room called the basement … and never look at what is stored there
I like my approach because I only have 1 scene for a function, not 3 scenes and 1 virtual device. If you have lots of devices and things to control, the distributed functionality is more difficult to deal with. (My approach is based on the design principal of locality). But I also violated a design principal of coupling by passing the DOOR_EVENT flag between the two pieces of code … tradeoffs… tradeoffs … tradeoffs …
Is there a way of fooling Vera into thinking it’s the first time the door has been opened again? - as I would like to test the scene again - but don’t want to have to wait until tomorrow!
i.e. - I manually do something with ‘enabled’? - (if that is the right thing to do…?!)