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

Offline akbooer

  • Master Member
  • *******
  • Posts: 5343
  • Karma: +231/-68
  • "Less is more"
Re: Creating triggers from luup code
« Reply #75 on: December 17, 2017, 06:10:55 am »
Ah!  Just in time... I was about to take a look.
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: 32
  • Karma: +1/-2
Re: Creating triggers from luup code
« Reply #76 on: December 17, 2017, 06:29:46 pm »
Not so fast @ak!!!  :)  So, the watcher is working great, except for one small detail....if I turn a light on directly at the switch, the watcher doesn't send the request to my server.  It works fine if I tell Vera to turn on the light though.  Does turning on a light via the switch act differently where Vera is concerned? Here's one of the watchers:

Code: [Select]
function device_dimmer_level_change(dev_id, service, variable, old_val, new_val)
--gets dim level
local lightlevel = luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", dev_id)
local status

-- set light to currect status
if lightlevel > "0" then
status = "1"
else
status = "0"
end

-- create JSON array
local json = require "dkjson"
local a = dev_id
local b = lightlevel
local c = status
local d = "dimmer"

local payload = {
device_id = a,
light_level = b,
state = c,
dev_name = d
}

local json_payload = json.encode (payload)

http = require("socket.http")
http.TIMEOUT=5
ltn12 = require("ltn12")
path = "http://192.168.1.69:180/request?param_1=one"
payload = json_payload 
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

luup.variable_watch("device_dimmer_level_change", "urn:upnp-org:serviceId:Dimming1","LoadLevelStatus", dev_id)


Hope you can help.  Thanks.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5343
  • Karma: +231/-68
  • "Less is more"
Re: Creating triggers from luup code
« Reply #77 on: December 18, 2017, 03:22:49 am »
This will depend on the technology of the switch.  If it reports instant status, then Vera will respond quickly, if not, then the status update will not happen until Vera polls the switch, which could be some time later.
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.