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

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Creating triggers from luup code
« Reply #60 on: September 20, 2017, 07:00:14 pm »
status, status1, and power are set once initially and never updated again.  Is that what you intended?  Your if/then/else checks them later so I suspect you want them updated?

Great point !

Thanks for that. I?ve been slicing and dicing the code around so much now to try to get each function to work. So I?ve had those calls in various places  etc. .

I?ll look to make them part of their respective functions again and see how that changes things 

Offline Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #61 on: December 07, 2017, 10:19:45 am »
I've been following this thread and trying to implement variable_watch.  This is code is placed in my startup lua:

luup.variable_watch("device_power_state_change", "urn:upnp-org:serviceId:SwitchPower1","Status", 2)

function device_power_state_change(dev_id,service, variable, old_val, new_val)
   -- called when a device is switched on or off - from a variable_watch
   if new_val == "1" then
      device_switched_on_or_off(dev_id,"true")         
   else
      device_switched_on_or_off(dev_id,"false")
   end

  http = require("socket.http")
  ltn12 = require("ltn12")
  path = "http://192.168.1.69:180/12j0kaq1?param_1=one&param_2=two&param_3=three"
  payload = [[ {"key":"My Key","name":"My Name","description":"The description","state":1} ]]
  response_body = { }

  res, code, response_headers, status = http.request
  {
    url = path,
    method = "POST",
    headers =
    {
      ["Authorization"] = "Maybe you need an Authorization header?",
      ["Content-Type"] = "application/json",
      ["Content-Length"] = payload:len()
    },
    source = ltn12.source.string(payload),
    sink = ltn12.sink.table(response_body)
  }
  luup.task('Response: = ' .. table.concat(response_body) .. ' code = ' .. code .. '   status = ' .. status,1,'Sample POST request with JSON data',-1)

end

I'm not receiving the http POST on the server.  Fairly new at Lua, so hope someone can provide some guidance.

Thanks

Offline akbooer

  • Master Member
  • *******
  • Posts: 5277
  • Karma: +229/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #62 on: December 07, 2017, 11:50:59 am »
The order in which things are defined matters in Lua.  One thing which stands out immediately, is that you call luup.variable_watch before you define the device_power_state_change function.  You need to reverse that state of affairs.

Is there anything that shows up in the log by way of an error?  A failure in the startup Lua should be logged.  If the variable watch call fails, then the rest of the code (including the HTTP request) won't be executed.
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 Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #63 on: December 07, 2017, 01:45:52 pm »
Thanks @akbooer, I've reversed the order and all is working now.  Eventually, I'll probably move the function to a lua file.  So, the function is now sending data to my remote server, which will act upon it; however it appears as though Vera is awaiting a response of some sort.  If I'm in the UI and turn a light on, my server shows it gets the information and light comes on, but in the UI device I get: Waiting for node to reply after 0 retries...    If I then try to turn the light off, there's about a 15-20 sec delay before the command executes.  What type of response (if any) should my server be sending back to Vera?  I'm very new to the http server stuff, so forgive what might be a simple question.

Thanks

Offline akbooer

  • Master Member
  • *******
  • Posts: 5277
  • Karma: +229/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #64 on: December 07, 2017, 04:42:39 pm »
Quote
...however it appears as though Vera is awaiting a response of some sort.

There may need to be some response, perhaps depending on the status return code?   Just "OK" might do.  You wrote the remote server yourself?

I imagine that you are seeing an HTTP handshake timeout, although the default for that in the LuaSocket library is, IIRC, 5 seconds.
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 Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #65 on: December 07, 2017, 05:50:28 pm »
The http server is a simple javascript server written by a friend.  It's receiving the POST just find.  I've just got to figure out the proper way to send the response back to Vera....am I correct in thinking I'll use a POST back to Vera to send my response?  I'm probably totally wrong, though.

This is a big task for me as I'm new to Javascript (not to mention http server stuff).   :)  But, I'm enjoying the process and all the help I get from folks. 

Thanks again for letting me bother you.
Bob 

Offline akbooer

  • Master Member
  • *******
  • Posts: 5277
  • Karma: +229/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #66 on: December 07, 2017, 06:23:30 pm »
No, the response needs to be in the appropriate format of headers and possible body, sent as one or more messages using the original TCP connection established by the POST request.
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 Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #67 on: December 07, 2017, 06:31:40 pm »
You were correct a couple of posts ago....all I needed to do was add:  http.TIMEOUT=10, which gave the server enough time to respond properly.  Now, I just have to parse out the POST on the server side.  One step at a time.  Thanks man.

Offline Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #68 on: December 09, 2017, 06:17:59 am »
I"m trying to add a variable to a statement as below, which sends JSON data to a server:

 local a = "Hello"

 payload = [[ {"id":" .. a .. ", "name":"My Name", "description":"The description", "state":1} ]]

all I'm getting on the return is:  ..a..  Original statement was

 payload = [[ {"id":" Device ID", "name":"My Name", "description":"The description", "state":1} ]]

Just getting started in Lua (and have been reading Programming in LUA), but am i bit confused as to how to get this to work.  Thanks.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5277
  • Karma: +229/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #69 on: December 09, 2017, 06:53:50 am »
Just getting started in Lua (and have been reading Programming in LUA), but am i bit confused as to how to get this to work.  Thanks.

Karma to you for doing more than many others do in trying to get a basic grasp of Lua.  It's small, but subtle, so may take a while.

You are nearly there.  Your original statement:
Code: [Select]
payload = [[ {"id":" Device ID", "name":"My Name", "description":"The description", "state":1} ]]

creates a single string, which itself contains double quotation marks, by using the special double-bracket syntax.

What you need now is a way to make a single string, but containing a variable.  There are many, many ways to do this.  To make this as as similar to your attempt you could write

Code: [Select]
local a = "Hello"

payload = [[ {"id":"]] .. a .. [[", "name":"My Name", "description":"The description", "state":1} ]]

However, what you're doing is to script JSON code directly.  For more complicated cases you would be far better off constructing the response as an actual Lua table and then using a JSON encoder.  Ask again if you need to know more.
« Last Edit: December 09, 2017, 07:52:06 am 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 Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #70 on: December 09, 2017, 07:48:32 am »
Aaaahhh.....makes perfect sense now.  I didn't know about the [[ syntax, but seeing what you did makes perfect sense.  The end goal is to get the device_id and pass it (and other info on a device to my server).

This thread is full of great snippets to do exactly what I need.  My next chore is to take @dunked snippet that watches all switches and incorporate that into my function.  Thanks for your willingness to help.

Cheers 

Offline akbooer

  • Master Member
  • *******
  • Posts: 5277
  • Karma: +229/-67
  • "Less is more"
Re: Creating triggers from luup code
« Reply #71 on: December 09, 2017, 08:45:48 am »
Since you'll be wanting to add more parameters, I'd recommend, once again, the JSON encoder route.

Your existing example would look something like this:

Code: [Select]
local json = require "dkjson"

local a = "Hello"
local b = "My Name"
local c = "The description"
local d = 1

local payload = {
    id = a,
    name = b,
    description = c,
    state = d,
}

local json_payload = json.encode (payload))

which would, again, give you the string:

Code: [Select]
{"id":"Hello","state":1,"name":"My Name","description":"The description"}

But then, you see, it's MUCH easier to change values, and to read the code.
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 Styxman

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #72 on: December 09, 2017, 08:54:42 am »
That is awesome....and yes, much much easier to read.  I will utilize it soonest.  Thanks, man for all the help.