Author Topic: Creating triggers from luup code  (Read 17970 times)

Offline dunked

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +3/-0
Re: Creating triggers from luup code
« Reply #15 on: September 15, 2013, 02:22:04 pm »
Pretty much as @akbooer says - you can write code to do whatever you need

so if you have a variable_watch such as

luup.variable_watch("thermostat_heat_on", "urn:upnp-org:serviceId:thermostat","ModeStatus ", 112) ---- I made those values up so you'd need to check what they really are!

then when ModeStatus changes the Vera will call the function thermostat_heat_on  which you will need to write and add to the vera, then in that function you can check if the new value is "HeatOn" and do your stuff.

Having said all that if you're not comfortable coding you may not enjoy the experience. I have spotted that when adding triggers to scenes that some devices have a lot of things you can use as triggers - such as temperature changes or device switched on - you may find what you need is already available without having to write any code.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Creating triggers from luup code
« Reply #16 on: September 15, 2013, 02:59:45 pm »
Thanks guys

I have spotted that when adding triggers to scenes that some devices have a lot of things you can use as triggers - such as temperature changes or device switched on - you may find what you need is already available without having to write any code.

You'll be surprised at how limited the trigger options are compared to the number of variables that are listed against  the device. So this is largely the only option I really have (assuming that variable is updated - I'm still testing.

As for the coding, that side intrigues me, so I consider it a challenge. ;)

Your suggestion to create a .lua file for your own code and adding the line to the startup sounds like a really good idea. I already have some lines in the start up, so I could also, move that out to it's own file too I assume just by add another line e.g

require("my_code.lua"). -- my obscure triggers and actions
require("my_danfoss_living_code.lua") -- syncing my temp sensor with the living connect Trv

Then if I wanted to stop anything I could just remove the line, rather than edit the file ?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #17 on: September 15, 2013, 03:56:45 pm »
Your suggestion to create a .lua file for your own code and adding the line to the startup sounds like a really good idea. I already have some lines in the start up, so I could also, move that out to it's own file too I assume just by add another line e.g

require("my_code.lua"). -- my obscure triggers and actions
require("my_danfoss_living_code.lua") -- syncing my temp sensor with the living connect Trv

Then if I wanted to stop anything I could just remove the line, rather than edit the file ?

I found a few challenges when creating modules to include (especially when it came to creating sub-modules), so wrote up my findings...

http://forum.micasaverde.com/index.php/topic,13296.msg112631.html#msg112631
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dunked

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +3/-0
Re: Creating triggers from luup code
« Reply #18 on: September 15, 2013, 04:10:25 pm »
@parkerc,
The require thing with the Lua files is a proven technique and works well, so what you describe would work fine. The snag is that if your lua files are anything like mine they will grow and grow. So removing the require line would take out whatever else in in the file also.

What I tend to do is edit the lua file with the code I'm writing but, when I'm testing rather than keep saving and uploading the file (which gets tedious when you've done it half a dozen times). I just paste it into the "test luup code box" and run it there and only when it's working do I upload the file.

Looks like @akbooer replied just before I finished this, with a much more comprehensive example. But if you're new to this coding lark then I'd stick with what you're planning for now, then as you get more confident then you can move onto the more powerfull stuff!

But I started by doing exactly what you describe, so dive on in and have fun!

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Creating triggers from luup code
« Reply #19 on: September 15, 2013, 04:45:05 pm »
Thanks again.

@akbooer seems to suggest that each separate file I create needs to start with the following line?

Code: [Select]
module("module-name", package.seeall)
Does that mean the first line would be something like this ?

Code: [Select]
module("my_code.lua", package.seeall)

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #20 on: September 15, 2013, 05:16:48 pm »
The module name is independent of the file name, but is what is used to prefix the exported functions (and variables). My understanding is that although the module keyword is deprecated in the latest versions of Lua, it is required (sorry) by Luup because of the compression applied to the files when loaded onto Vera (it has been overloaded in order to decompress these files).
« Last Edit: September 15, 2013, 05:48:26 pm by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dunked

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +3/-0
Re: Creating triggers from luup code
« Reply #21 on: September 16, 2013, 12:24:12 pm »
I have never used modules or the "module" keyword. I can happily call functions and run code in the files that I "require" without any difficulty.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #22 on: September 16, 2013, 12:37:01 pm »
I have never used modules or the "module" keyword. I can happily call functions and run code in the files that I "require" without any difficulty.
Cool. I'll give it a go.  Old habits die hard.  Although I see it specified this way often.
Do you use the Luup files pages to download file to Vera, or some other way?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dunked

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +3/-0
Re: Creating triggers from luup code
« Reply #23 on: September 16, 2013, 12:51:04 pm »
Yep I just use luup files to get them to the vera. It's all been nice and simple - which makes a change!

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Creating triggers from luup code
« Reply #24 on: September 16, 2013, 05:09:20 pm »
Would I be right in saying that once a function is loaded either from an uploaded .lua file or specified in the Luup Start Up (under Apps/Dev) - can it be referred to within a scene?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #25 on: September 16, 2013, 05:43:01 pm »
Yes, that's right.
Almost all my scene Luup is just one-liners which call functions loaded at startup.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #26 on: September 30, 2013, 05:42:57 am »
@dunked
[...]
My problem is simply that when I've tried to use any callback like variable watch or timers, etc., then the function supplied as a named string in the setup call is never found and generates a runtime error when the event occurs.  What am I doing wrong ??

At last, I've got to the root of this callback issue - the problem is well described in an old thread:
http://forum.micasaverde.com/index.php/topic,10258.0.html

..with solutions from @guessed:
The timer callback method must be a public function on your plugin.  In turn, that method can call the one on your module/library, but call_timer cannot see the one in the module.

...and @futzle:
The way I normally do this is to copy the function from the module into the global symbol table explicitly.
Code: [Select]
t =require("test")
CallBackTimer = t.CallBackTimer

Thanks to all on this thread and those mentioned above for putting me right.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dunked

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +3/-0
Re: Creating triggers from luup code
« Reply #27 on: September 30, 2013, 08:57:24 am »
I've avoided the trouble you've been having, and it seems that's because I've never used any modules! I'll try and keep it that way, the simpler the better!

Offline Stealth

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: Creating triggers from luup code
« Reply #28 on: March 18, 2014, 03:46:10 pm »
Hi Guys,

I have been recommended to this thread for help.

I'm new to the Vera Lite and coding. I am looking for a solution so I can have my Vera Lite send node status updates out to my 3rd party home automation controller, as and when a node state is changed. 

The data I need to send is-

1) Node ID

2) Node level On (1-100) or node Off.

I have the code below which will send the two variable values onto my 3rd party HA controller and run a macro to update the internal lighting table (Node 3 Light level 50%) -

local http = require("socket.http")
 http.TIMEOUT = 5
result, status = http.request("http://192.168.0.23:3005/HVTableVar.html","Var 241 : 3 = 3")
result, status = http.request("http://192.168.0.23:3005/HVTableVar.html","Var 242 : 50 = 50")
result, status = http.request("http://192.168.0.23:3005/HVTableMacro.html","Macro 37=Run Macro")


Var 241 is for the light node ID

Var 242 is the light node level status

I would be grateful if someone could talk me through how I could set this up to trigger when a node state changes, then to send the http commands that would parse the relevant node and light level.

I rather not setup a scene for each node with the Luup code as this will need loads of scenes which would be a nightmare. If there is a way to put some code together that would work for new nodes as they are added that would be fantastic.

Please bear in mind I am a complete newbie....  I realise I have a steep learning curve but I need to get this working.

Any help would be appreciated.

Steve
« Last Edit: March 18, 2014, 03:55:02 pm by Stealth »

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #29 on: March 18, 2014, 04:23:22 pm »
How would you envisage specifying which 'nodes' / variables you want this to apply to?  There are different approaches, but the basis is to use luup.variable_watch to watch those variables and to call a function with your code in it, so you do need some algorithmic way of defining what you're interested in.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.