Trigger to execute scene between sunset and 10pm and only once

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.

Here’s the code:

luup.inet.wget("http://www.prowlapp.com/publicapi/add?apikey=d1383499d63d09cf745aa0&application=Vera&event=Front+Door&description=Opened&priority=-1") return luup.is_night()

If anyone can help - that would be amazing

Thanks

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.

Cheers guys - will give the ideas a go and report back!

OK - I’m stuck with how to say:

(Pseudo Code)
If 20 mins b4 sunset
then
activate switch
else
do nothing

Any help much appreciated!

(BTW - I have installed DAD)

Cheers.

UI4:
Scene → Timers → Day of week based → Time: Before sunset → h:m:s …
Advanced → Pick a Device → Virtual switch → Add → Please select → SetTarget → …

Thanks ap51e

I’m struggling with the condition lua

I have got the virtual switch set up - and when it is on I want the scene ‘after work’ to run and when it is off, i don’t. Here’s my code

[code]local lul_tmp = luup.variable_get(“urn:schemas-upnp-org:device:VSwitch:1”, “Status”, 24)

if (lul_tmp == “1”) then
return false
end
[/code]

At the moment - the scene is running no matter whether the switch is on or off. If you could point out what I am doing wrong - that would be amazing

Thanks

The URN doesn’t look right.

Cheers.

I’m getting it from the ‘device type’ field here: (see attached screen shot)

Is that wrong?

Thanks again

[tt]luup.variable_get[/tt] must be fed with the service ID; you are using the device ID.

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

Sorry for shonkyness of the code!

Cheers

Untested: Advanced → Pick a Device → Virtual switch → Add → Please select → SetTarget → …

or

[tt]luup.call_action( , ‘SetTarget’, { newTargetValue = “0” }, )[/tt]

Superb - thanks

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

10:00 PM Event Luup Code
ENABLE = false
return false

Door Trigger Event Luup Code
tmp = ENABLE
ENABLE = false
return tmp

Add commands to your scene to turn on as many lights as you wish.

Everything is contained in a single Scene!

Thanks Richard

Yes - I would love to have a go at doing it all in LuuP

This is the code in my scene at the mo (using the status value in the virtual switch):

local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", 24) if (lul_tmp == "1") then luup.call_action("urn:upnp-org:serviceId:VSwitch1", 'SetTarget', { newTargetValue = "0" }, 24 ) return true else return false end

I’m pretty new to all this - would you be able to help me out with the

Before Sunset Event Luup Code &10:00 PM Event Luup Code &Door Trigger Event bits of the Luup Code

Cheers!

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 :slight_smile:

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 …

Thanks very much Richard

Will get onto this tonight

Thanks again Richard

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…?!)

Cheers

PS - could I stick something in the ‘start-up’ Lua to sort the ‘enabled’ thing?

If you use a Virtual Switch for the ENABLE flag then you can control the ENABLE flag from the
UI. It will show up as a switch.