We have moved at community.getvera.com

Author Topic: Calling LUA function outside  (Read 1122 times)

Offline DesT

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +5/-1
Calling LUA function outside
« on: August 02, 2017, 09:48:09 am »
AK,

As I do more and more stuff inside my openLuup installation to manage everything.   Do you think of any way I will be able to call a LUA function I made from another machine ?
Vera Edge-UI7/Plus-UI7 (2), DSC Partition (5 ) + Zones Sensors(31), Nest thermostat (1) & Protect Fire/CO (3), GE Sw (8 ), GE Dimr (14), FGMS-001 (2), ZW100 (2), RZCS4 (1), AL-DSC11 (1), Aeon HEM 2nd Edition (1), Aeon SSE (5), YRD220-ZW (1), SONOS (6), MyQ Chamberlain (1)
PINE64/openLuup/ALTUI/Rules

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #1 on: August 02, 2017, 09:55:12 am »
Unsure exactly what you mean, here.  Of course, it's trivial to set up an HTTP handler which will do anything you want within the environment of openLuup.  But perhaps that's not quite what you meant?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline DesT

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +5/-1
Re: Calling LUA function outside
« Reply #2 on: August 03, 2017, 10:35:00 am »
I have a function like Tools.logHUD that I use to send notification to a display in the hallway...  and I would like to use it outside openLuup with a curl call instead of rewriting everything on all other machine/OS I need to use it.

So I'm looking a way to call them from outside openLuup using the same parameter I'm using inside any scene!
Vera Edge-UI7/Plus-UI7 (2), DSC Partition (5 ) + Zones Sensors(31), Nest thermostat (1) & Protect Fire/CO (3), GE Sw (8 ), GE Dimr (14), FGMS-001 (2), ZW100 (2), RZCS4 (1), AL-DSC11 (1), Aeon HEM 2nd Edition (1), Aeon SSE (5), YRD220-ZW (1), SONOS (6), MyQ Chamberlain (1)
PINE64/openLuup/ALTUI/Rules

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #3 on: August 03, 2017, 11:01:40 am »
So yes, easy, you can use curl to call a Luup HTTP handler with whatever parameters you like.

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

Offline DesT

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +5/-1
Re: Calling LUA function outside
« Reply #4 on: August 03, 2017, 11:29:47 am »
AK,

Not sure how to play with that...  what I really need to do if I want to call my function Tools.logHUD('Test message') from the http ?
Vera Edge-UI7/Plus-UI7 (2), DSC Partition (5 ) + Zones Sensors(31), Nest thermostat (1) & Protect Fire/CO (3), GE Sw (8 ), GE Dimr (14), FGMS-001 (2), ZW100 (2), RZCS4 (1), AL-DSC11 (1), Aeon HEM 2nd Edition (1), Aeon SSE (5), YRD220-ZW (1), SONOS (6), MyQ Chamberlain (1)
PINE64/openLuup/ALTUI/Rules

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #5 on: August 03, 2017, 12:48:05 pm »
Not sure how to play with that...  what I really need to do if I want to call my function Tools.logHUD('Test message') from the http ?

Well, put this in the Lua Startup (or just to try it, run in Lua Test):

Code: [Select]
function HTTP_user_call (_,p)
    local result = "no such function"
    if p["function"] then
        local fct
        a,b = p["function"]: match "(%w+)%.?(%w*)"
        fct = _G[a]
        if fct and b~= '' and type(fct) == "table" then fct = fct[b] end
        if type(fct) == "function" then
            result = fct (p.parameter)
        end
    end
    return tostring(result)
end

luup.register_handler ("HTTP_user_call", "call")

Then, from a browser (or curl, if you must):
Code: [Select]
http://openLuupIP:3480/data_request?id=lr_call&function=Tools.logHUD&parameter=Test message

...would call your function with the given parameter and return a response (if any.)

It works for any one- or two- part function name, so you can test with:

Code: [Select]
http://openLuupIP:3480/data_request?id=lr_call&function=os.date&parameter=%c

which, for me just now, gave:

Code: [Select]
Thu Aug  3 17:46:59 2017
« Last Edit: August 03, 2017, 12:57:13 pm by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline DesT

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +5/-1
Re: Calling LUA function outside
« Reply #6 on: August 03, 2017, 12:52:11 pm »
Trying to run it in the Lua test and I got a nil return.

After trying your os.date exemple, got
error in callback [lr_call] : [string "ALTUI - LuaRunHandler"]:3: attempt to index global 'json' (a nil value)
Vera Edge-UI7/Plus-UI7 (2), DSC Partition (5 ) + Zones Sensors(31), Nest thermostat (1) & Protect Fire/CO (3), GE Sw (8 ), GE Dimr (14), FGMS-001 (2), ZW100 (2), RZCS4 (1), AL-DSC11 (1), Aeon HEM 2nd Edition (1), Aeon SSE (5), YRD220-ZW (1), SONOS (6), MyQ Chamberlain (1)
PINE64/openLuup/ALTUI/Rules

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #7 on: August 03, 2017, 01:02:35 pm »
Yes, you got to it before me...
  • I just updated the openLuup Tips & Tricks thread to explain why it doesn't work for you, and what you could do about it, or...
  • I also edited the code below so that it does now work anyway.
« Last Edit: August 03, 2017, 02:47:55 pm by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline DesT

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +5/-1
Re: Calling LUA function outside
« Reply #8 on: August 03, 2017, 01:43:56 pm »
Working perfectly.... thanks a lot bro ;)
Vera Edge-UI7/Plus-UI7 (2), DSC Partition (5 ) + Zones Sensors(31), Nest thermostat (1) & Protect Fire/CO (3), GE Sw (8 ), GE Dimr (14), FGMS-001 (2), ZW100 (2), RZCS4 (1), AL-DSC11 (1), Aeon HEM 2nd Edition (1), Aeon SSE (5), YRD220-ZW (1), SONOS (6), MyQ Chamberlain (1)
PINE64/openLuup/ALTUI/Rules

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #9 on: September 01, 2017, 05:16:51 am »
Excellent.  Thanks for the positive feedback.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: Calling LUA function outside
« Reply #10 on: September 01, 2017, 07:07:06 pm »
On Vera you can also call the RunLUA action.    I am not sure if OpenLUUP has implemented this.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Calling LUA function outside
« Reply #11 on: September 02, 2017, 02:42:15 am »
On Vera you can also call the RunLUA action.    I am not sure if OpenLUUP has implemented this.

Oh yes, that's the way that the Lua Test window works.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.