When a scene runs its own scene code: How do you get the number of the scene that is running that scene code?
Or course, I should be used to being blank by now.
Can someone please refresh me how I can have VeraAlexa say the value of a variable? I used to have it, but have deleted it and now failing to find it in the posts 😞
TIA
C
Hi
I’ve been investing a lot of time recently trying to digitise everything I can, and my current adventure is with all my paper documents.
My tool of choice is Paperless-ngx, which so far is an amazing open source solution, and highly recommended if you’re interest in such a thing…
Such a move to Paperless would not be complete without some form of integration into Vera / openLuup, and thankfully it has a nice Rest API (https://paperless-ngx.readthedocs.io/en/latest/api.html) I can use. So far I’ve been able to make use the GET requests, to provide document counts etc., but it’s the mutipart/form data piece where I’m struggling..
The Curl command to upload a document, is as follows
curl -H "Authorization: Basic Y2hyaXM62tgbsgjunotmeY2hyaXNob3N0aW5n" -F "title=Companies House File 10" -F "correspondent=12" -F "document=@/mnt/nas/10.pdf" http://192.168.102.134:8777/api/documents/post_document/But, I’d like to know how to do this with Lua code, more just to help me in my Lua learning curve, and what I thought would be reasonably straight forward thing to do, has turned out to be quite the opposite 🙂 Sending a multipart form data submission via http.request, is not as straight forward as I hoped. If anyone has any time to help, I’d appreciate some guidance on what I’m missing with the following, as based on the feedback, it looks like it not seeing the file (which I think I’ve confirmed by trying to sending both the curl and Lua commands to httbin.org ) …
local http = require("socket.http") local ltn12 = require("ltn12") local mime = require("mime") local lfs = require("lfs") local username = "username" local password = "password" local httpendpoint = 'http://httpbin.org/post' local filepath = "/mnt/nas/10.pdf" local file = io.open(filepath, "rb") local contents = file:read( "*a" ) -- https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data local boundary = "-----BoundaryePkpFF7tjBAqx29L" local send = "--"..boundary.. "\r\nContent-Disposition: form-data; title='Companies House File'\r\n" .."----"..boundary.. "\r\nContent-Disposition: form-data; document="..filepath.. "\r\nContent-type: application/pdf".."\r\n" ..contents.."\r\n" .."------"..boundary.."--\r\n" -- Execute request (returns response body, response code, response header) local resp = {} local body, code, headers, status = http.request { url = httpendpoint, method = 'POST', headers = { ["Authorization"] = "Basic " .. (mime.b64(username ..":" .. password)), ["Content-Length"] = lfs.attributes(filepath, 'size'), ['Content-Type'] = "multipart/form-data; boundary="..boundary, }, -- source = ltn12.source.file( io.open(filepath,"rb") ), source = ltn12.source.file( io.open(send) ), sink = ltn12.sink.table(resp) } print(body, code, headers, status) print(table.concat(resp)) if headers then for k,v in pairs(headers) do print(k,v) end endHi,
I’m sure this is not a unique question about Vera (& Lua 5.1) and I’ve seen this reference in place,, but I can’t seem to find a fix to make an https download request work..
To give you an example, the code below aims to download 2 different files, the http call works fine, returning a 200 code, but the https one doesn’t, it doesn’t return anything ?
Please could some highlight what I’m missing?
print("--------DOWNLOAD http---------") local http = require("socket.http") local body, code = http.request("http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg") print(code) if not body then error(code) end local f = assert(io.open('mnt/nas/webtest1.jpg', 'wb')) -- open in "binary" mode f:write(body) f:close() print("--------DOWNLOAD https---------") local https = require "ssl.https" --luasec local body, code = https.request("https://u.cubeupload.com/jbcooper/16146313918060.jpg") print(code) if not body then error(code) end local f = assert(io.open('mnt/nas/webtest2.jpg', 'wb')) -- open in "binary" mode f:write(body) f:close()Hi
I’m trying out the Fitbit api, not only to extract some activity data for a potential plugin, but also to learn more about OAuth 2.0. More details can be found here..
Fitbit Development: Getting Started Fitbit Development: Getting StartedYou'll fit in here. Using JavaScript, CSS, and SVG, developers now have a fast, easy way to build apps and clock faces for Fitbit OS.
In addition to having a fitbit account, to get going with any integration, you need to register your app, via here..
LoginWith an app registered, I’ve called mine “VERA” , I’ve written chunks of Lua code to support the various steps, (completed manually) my goal now is to try and put them all together into functions to be called in sequence on my Vera and/or OpenLuup.
Where I’m getting stuck is with the authorisation code step, which Fitbit provides appended to the domain name I registered with them when registered my app ..
Here’s an example of a redirect URL that Fitbit will redirect the user too, with their associated authorisation code, which I need in the process later on to obtain the required access token. (More specific details are here - https://dev.fitbit.com/build/reference/web-api/developer-guide/authorization/ )
https://myapp.com/callback?code=d62d6f5bdc13df79d9a5f#_=_And it’s the d62d6f5bdc13df79d9a5f part of that new page/URL which I need..
How best do i capture/process the URL call Fitbit issues?
I assume this is where a luup.register_handler would come in, and I could register my Vera handle (using https ?, as that’s required by Fitbit) as the redirect domain/url e.g.
https://veraip:3480/data_request?id=lr_fitbit&So Fitbit would redirect to..
https://veraip:3480/data_request?id=lr_fitbit&/?code=d62d6f5bdc13df79d9a5f#_=_But that doesn’t seem to work..
Any suggestions on how to do this would be appreciated..
function FitbitRequestHandler(lul_request, lul_parameters, lul_outputformat) for k,v in pairs(lul_parameters) do luup.log ('fitbit_Handler: parameters are: '..tostring(k)..'='..tostring(v)) end if next(lul_parameters) == nil then luup.log("lul_parameters Table is empty") end local html = "</html><head>" .. "</head>" .. "<body>" .. "PRINTING" .. "\n" .. " lul_request: " .. tostring(lul_request) .. "\n" .. " lul_parameters: " .. tostring(lul_parameters) .. "\n" .. " lul_outputformat: " .. tostring(lul_outputformat) .. "\n" .. "</body></html>" return html, "text/html" end luup.register_handler("FitbitRequestHandler", "fitbit")Thanks is advance…
Hi,
Please could people share how they are presenting and handling errors in their Vera / OpenLuup plugins, I’ve tried to look through various Implementation files, but very few seem to have anything, other than perhaps a luup.task (which for some reason doesn’t work for me) ?
Below is the startup and check/validation functions for a plugin I’m working on..
function checkIPPowerSetUp(lul_device) log("Checking if parent device is configured correctly...") ipAddress = luup.devices[lul_device].ip -- check if parent device has an ip address assigned if ipAddress == nil or ipAddress == "" then -- if not stop and present error message luup.task('ERROR: IP Address is missing',2,'IPPower',-1) debug("ERROR: IP Address is missing " ..ipAddress.. " unable to progress") return false else -- if one is provided, present success message luup.task('IP Address for IPPower 9258 present, setup continues',4,'IPPower',-1) debug("IPPower StartUp activated - Device #" .. lul_device .. " now creating children") createChildIPPowerOutlets(lul_device) luup.call_delay("IPPowerPoller", 5) return true end end function IPPowerStartup(lul_device) -- set attributes for parent device luup.attr_set( "name", "IPPower 9258", lul_device) luup.attr_set( "category_num", "3", lul_device) luup.attr_set( "subcategory_num", "1", lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "Icon", 1, lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "PluginVersion", PV, lul_device) checkIPPowerSetUp(lul_device) log("Start up, Parent device created...") end </functions> <startup>IPPowerStartup</startup>What’s the best way to present errors to the user, and make iot clear what they need to do ?
I'm struggling to fix the code attached here
https://github.com/dbochicchio/vera/blob/master/OpenSprinkler/L_VeraOpenSprinkler1.lua#L270,L429
Under certain circumstances (thanks @DesT!) I see that it's causing a luup reload infinite cycle, because child devices are being created, then deleted. Specifically, I see this in the logs:
2020-06-01 19:35:30.482 luup.chdev.sync:: [102] Open Sprinkler Test, syncing children 2020-06-01 19:35:30.482 openLuup.chdev:: deleting [413] Daily Garden 2020-06-01 19:35:30.482 openLuup.chdev:: deleting [414] Water Level 2020-06-01 19:35:30.482 openLuup.chdev:: deleting [411] Daily Cedars 2020-06-01 19:35:30.482 openLuup.chdev:: deleting [412] St-Eustache Rules AM 2020-06-01 19:35:30.482 openLuup.luup:: device 102 'Open Sprinkler Test' requesting reloadThe devices are created (look at line 357/410), but when I call luup.chdev.sync, they got deleted. Any hints? Thanks
Hi
I’m putting the finishing touches to a HDMI Matrix plugin, and while I have it working well - I can’t find anything on the Vera/MIOS wikis that tells me how I can show if a UI button is pressed or not (to reflect a input choice that is selected)
Here’s a screenshot of the child outputs I’ve created for the Matrix switcher. (The plugin creates a parent device and then 4 children to show the Matrix Outputs - parent is not shown - and the idea is that’s you select the input number for the respective output)
BB5B6001-294E-4AE9-A095-D2E639F56B9C.jpeg
Here is an extract of the button element in the associated JSON file used for the child devices..
{ "ControlGroup": "1", "ControlType": "button", "top": "0", "left": "0", "Label": { "lang_tag": "Input1", "text": "Input1" }, "Display": { "Top": 60, "Left": 50, "Width": 75, "Height": 20 }, "Command": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Action": "SYCall", "Parameters": [ { "Name": "action", "Value": "I1" } ] } },The associated command calls the parent implementation file SYCall - (The value “I1” relates to a element of the URL that needs to be called to select that associated input)
<action> <serviceId>urn:nodecentral-net:serviceId:SYMatrix1</serviceId> <name>SYCall</name> <run> SYMatrix.SYCall(lul_device, lul_settings.action, lul_settings) </run> </action>And then the Lua file (L_****) has holds the SYCall function
function SYCall (deviceNo, action, settings) ….. endI’ve just started to have a play with iOS Shortcut and one of my test cases is to see if I can do the following
select a a file (.lua or .txt) on my iPad extract the entire script/contents encode the data obtained append the encode content to the end of the RunLua action. call the URL http://myIP/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunLua&Code=Below is a screenshot of the workflow I have built in Shortcuts, which seems pretty straight forward, and it does generate a encoded URL, however the actual request itself does not complete. I get an ‘OK’ response back eventually, but the code is not actioned, and the logs don’t seem to tell me anything..
FAB8DB5F-9003-4624-BCF3-8F536A660D21.jpeg
The code I’m extracting from the Lua file works fine in the test Lua window, so i’m not sure what’s occurring differently, between the two routes, maybe it’s the encryption being used ?..
Is anyone able to help me test this, and see if they can generate a long / multi line RunLua encoded URL (if not via Shortcut, then by some that can help me with the root cause to then address that in Shortcut
I have a couple of calendar URLs I’m trying to call, and both work when i enter the URL into the browser, however when I try to call them via Lua and http(s).request, only one of them works ? Please could someone help me understand why, and what needs to be done differently to make the other one work ?
local socket = require 'socket' local http = require "socket.http" local ssl = require 'ssl' local https = require 'ssl.https' local calendarUria = "https://ics.fixtur.es/v2/ipswich-town.ics" local calendarUrib = "https://calendar.google.com/calendar/ical/en-gb.uk%23holiday%40group.v.calendar.google.com/public/basic.ics" print("Fixtur.es Football (URL A) returns the following?") local responseBodya, responseCodea, responseHeadersa = https.request(calendarUria) print(responseBodya, responseCodea, responseHeadersa) print("Google UK Holidays (URL B) returns the following?") local responseBodyb, responseCodeb, responseHeadersb = https.request(calendarUrib) print(responseBodyb, responseCodeb, responseHeadersb)If you run the code above, only the second Google (URL `B) returns anything..
Hi
Picking up from another thread this is to focus on the creation of the 4 key elements of a new Plugin for my Aviosys IPPower 9258, which are quite old, and I don’t believe are made anymore, but they seem to have a simple http api interface to cut my teeth on with a plugin.
First the device file, which is to support the creation/set up of the device, and here I explain what it is, tell Vera what the implementation file and .json file is, and also leverage the pre-existing Switch and HA capabilities..
Device File - D_IPPower.xml
<?xml version="1.0"?> <root xmlns="urn:schemas-upnp-org:device-1-0"> <specVersion> <major>1</major> <minor>0</minor> </specVersion> <device> <deviceType>urn:nodecentral-net:device:IPPower:1</deviceType> <staticJson>D_IPPower1.json</staticJson> <manufacturer>Aviosys</manufacturer> <modelDescription>IPPowerSwitch</modelDescription> <modelName>IPPower9258</modelName> <handleChildren>1</handleChildren> <implementationList> <implementationFile>I_IPPower1.xml</implementationFile> </implementationList> <serviceList> <service> <serviceType>urn:schemas-upnp-org:service:SwitchPower:1</serviceType> <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId> <SCPDURL>S_SwitchPower1.xml</SCPDURL> </service> <service> <serviceType>urn:schemas-micasaverde-com:service:HaDevice:1</serviceType> <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId> <SCPDURL>S_HaDevice1.xml</SCPDURL> </service> </serviceList> </device> </root>Next it is the Implementation file, which thanks to the feedback on this forum, will focus on a specific Lua file, where all the code will be, the only thing I need to add here are the Actions (what do I want it to be able to do etc.)
Implementation file - I_IPPower.xml
<?xml version="1.0"?> <!-- I_IPPower.xml; Vera/openLuup "IPPower 9258" Plug-in V1.1 Nov 2021 --> <implementation> <settings> <protocol>crlf</protocol> </settings> <files>L_IPPower1.lua</files> <startup>IPPowerStartup</startup> <actionList> <action> <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId> <name>SetTarget</name> <job> local url = "http://" .. ipAddress .. "/set.cmd?user=admin+pass=12345678+cmd=setpower+" .. luup.devices[lul_device].id .. "=" .. lul_settings.newTargetValue luup.log("Sending command " .. url) local status, data = luup.inet.wget(url) if (data) then debug("Data received = " .. data) else log("No Data received !! ") end local value = string.match(data, luup.devices[lul_device].id.."=(%d)") if (value) then luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", value, lul_device) else log("Value returned is empty") end </job> </action> </actionList> </implementation>The other two files D_IPPower.json, and L_IPPower.lua are still being worked on so i will add later …
If anyone notices any improvement
Hi
I’ve seen storage.set and storage.get used in some Lua code posted on the internet, where they need to store/retrieve somethings, e.g.
iv = encdec.base64dec(challenge_Key) storage.set('IV', iv)and then later on within another function I see..
iv = storage.get('IV')It makes sense how this is meant to work, and it seems such a simple/elegant feature - hence I was wondering, is there an equivalent in Vera/OpenLuup? As I’d naturally go to use luup.variable_set and luup.variable_get - but that requires a device to be used etc.
So, I was messing with my Alexa routines and ha bridge, and I want to share something very cool you could do with the ability to get the last Alexa that heard your command. I mentioned it a couple of times on the old place, but Vera's OS is missing the jq package, but openLuup could have it installed.
Basically, just execute -lastalexa with my VeraAlexa plug-in:
local lastAlexa = luup.call_action("urn:bochicchio-com:serviceId:VeraAlexa1", "RunCommand", {Command="-lastalexa"}, 666)Then use it in your scene, to dynamically generate your TTS response with the device you've asked your question seconds before!
local temperature = 25 -- get it from your sensor luup.call_action("urn:bochicchio-com:serviceId:VeraAlexa1", "Say", {Text="Ok, outise temperature is " .. temperature .. " degree", Volume=50, GroupZones= lastAlexa, Repeat = 2}, 666Very cool indeed 🙂
Hi all
I’ve been trying to use the Hue api and some of my Lua knowledge to create an energy calculator based on the brightness and wattage of specific models of hue bulb registered on the hub
I’ve pretty much got there, the only stumbling block I have is how I total up all the individual bulb wattages. Can anyone help ?
It’s likely an easy thing for someone more experienced to see, but as I only dip in and dip out now and then, the correct code/syntax does not always come to mind.
Note : as I’ve tried various routes, I’ve commented some things out, but you should be able to get the idea of what I’m trying to do..
Create device json from Hue Hub Decode JSON and extract key values to a table Calculate approx watts based on bulb model and/or watts function Total up watts of all bulbs that are switch on.Code below...
local hueBridgeIP = '192.168.1.29' local hueBridgeAPI = "80uCoXjnn2LNLhxEJ9PW6nmt-G5KWQ2uP3oONAvcm0j" function getHueLight() local http = require('socket.http') local ltn12 = require('ltn12') local json = require('dkjson') t = {} local url = string.format("http://%s/api/%s/lights", hueBridgeIP, hueBridgeAPI) b, c, h = http.request{url=url, sink = ltn12.sink.table(t), method='GET'} huestring = tostring(table.concat(t)) local hue, pos, err = json.decode(huestring, 1, nil) huelights = {} for k, v in pairs(hue) do local modelno = string.gsub(hue[k]['modelid'], "%s+", "") table.insert(huelights, {k, hue[k]['state']['on'], hue[k]['name'], hue[k]['state']['bri'], modelno, hue[k]['state']['reachable']}) print(k, hue[k]['state']['on'], hue[k]['name'], hue[k]['state']['bri'], modelno, hue[k]['state']['reachable']) end end local fourpointeightwattTable = { [ 1 ] = 0.6 , [ 2 ] = 0.7 , [ 3 ] = 0.8 , [ 4 ] = 0.9 , [ 5 ] = 1.2 , [ 6 ] = 1.4 , [ 7 ] = 1.7 , [ 8 ] = 2.0 , [ 9 ] = 2.3 , [ 10 ] = 2.6 , [ 11 ] = 2.9 , [ 12 ] = 3.1 , [ 13 ] = 3.5 , [ 14 ] = 4.0 , } local sevenwattTable = { [ 1 ] = 1.6 , [ 2 ] = 1.7 , [ 3 ] = 1.8 , [ 4 ] = 1.9 , [ 5 ] = 2.2 , [ 6 ] = 2.4 , [ 7 ] = 2.7 , [ 8 ] = 3.0 , [ 9 ] = 3.5 , [ 10 ] = 3.9 , [ 11 ] = 4.5 , [ 12 ] = 5.1 , [ 13 ] = 5.5 , [ 14 ] = 6.4 , } local ninewattTable = { [ 1 ] = 1.6 , [ 2 ] = 1.7 , [ 3 ] = 1.8 , [ 4 ] = 1.9 , [ 5 ] = 2.2 , [ 6 ] = 2.5 , [ 7 ] = 2.9 , [ 8 ] = 3.5 , [ 9 ] = 3.8 , [ 10 ] = 4.6 , [ 11 ] = 5.5 , [ 12 ] = 6.5 , [ 13 ] = 7.7 , [ 14 ] = 8.5 , } function fourpointeightgetWattage(hue) local index = math.floor(hue / 16.5) if index <= 0 then return 0.2 elseif index >= 15 then return 4.8 else return fourpointeightwattTable[index] end end function sevengetWattage(hue) local index = math.floor(hue / 16.5) if index <= 0 then return 0.4 elseif index >= 15 then return 7.0 else return sevenwattTable[index] end end function ninegetWattage(hue) local index = math.floor(hue / 16.5) if index <= 0 then return 0.4 elseif index >= 15 then return 9.0 else return ninewattTable[index] end end local total = 0 local Value = 0 local huewatts = {} for k,v in pairs(huelights) do if v[2] == true and v[5] == "RS125" and v[6] == true then local watts = fourpointeightgetWattage(v[4]) local Value = tonumber(watts) local total = total + Value print("four point eight watts") print(v[5], v[4], v[2], v[3], watts) table.insert(huewatts, {v[5], v[4], v[2], v[3], watts}) --print(watts) --print(total) elseif v[2] == true and v[5] == "LWA004" and v[6] == true then local watts = sevengetWattage(v[4]) local Value = tonumber(watts) local total = total + watts print("seven watts") print(v[5], v[4], v[2], v[3], watts) table.insert(huewatts, {v[5], v[4], v[2], v[3], watts}) --print(watts) --print(total) elseif v[2] == true and v[6] == true then local watts = ninegetWattage(v[4]) local Value = tonumber(watts) local total = total + Value print("nine watts") print(v[5], v[4], v[2], v[3], watts) table.insert(huewatts, {v[5], v[4], v[2], v[3], watts}) --print(watts) --print(total) end end print(total) print(huewatts) -- confirm if table it there (TBC)I've implemented this:
https://smarthome.community/topic/20/openluup-version-log/18
into my own MQTT Server and that's cool, since I'm now pushing every variable via MQTT, originating from the Vera.
Just a couple of questions for @akbooer
do I need to check that sock is connected, from time to time? is there anything specifically to retry the command?I'm running it in the latest hour, and everything seems OK, but I want to code it for the worst situations. Thanks, always inspiring!
Hi all
Here’s another snippet of code I like to use, to see what’s going on.
If anyone has any variations on this, I’d love to see it.
Add-on - Aviosys IPPower 9258
-
ISSUE : Current status is if I go to Create Device, it will not appear on the UI, however I can see what I think it is via an all devices look up, but it has very few attributes. It does not look like the L_IPPower.lua file is being called ??
Here is the latest L_IPPower1.lua, I can only assume for now it’s not being called, or requirements such as lul_device are not being passed to it.. What’s strange is that there is nothing in the logs, so I can only assume the latter ? I’ve tried the Lua file with and without the package_seeall declaration..
Lua file =
L_IPPower1.lua
module("L_IPPower1", package.seeall) local POLLING_INTERVAL = 30 local OUTLET_START = 61 -- API returns the first socket as P61 local OUTLET_END = 68 -- depending on your device, it can be a 4 or 8 port, so 64 or 68 local DEBUG_MODE = true -- turn on debugging local PV = "1.1" -- plugin version number local ipAddress local childDeviceIndex = {} local function log(text, level) luup.log("IPPower: " .. text, (level or 1)) end local function debug(text) if (DEBUG_MODE == true) then log("debug: " .. text, 50) end end function IPPowerPoller() -- Call IPPower to return state of each outlet local user = "admin" local pass = "12345678" debug("Polling IPPower Device") local url = "http://" .. ipAddress .. "/set.cmd?user=".. user .. "+pass=" .. pass .. "+cmd=getpower" luup.call_timer("IPPowerPoller", 1, POLLING_INTERVAL, "", "") debug("Calling status " .. url) local status, data = luup.inet.wget(url) if (data) then debug("Response received " .. data) -- Example response is "P61=0,P62=1,P63=0,P64=0,P65=1,P66=0,P67=0,P68=0" else log("Empty response returned") end for v = OUTLET_START,OUTLET_END do local value = string.match(data, "P"..v.."=(%d)") if (value) then luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", value, childDeviceIndex["P"..v]) else log("Empty value returned") end end end local function checkIPPowerSetUp(lul_device) log("Checking if parent device is configured correctly...") ipAddress = luup.devices[lul_device].ip -- check if parent device has an ip address assigned if ipAddress == nil or ipAddress == "" then -- if not stop and present error message luup.task('ERROR: IP Address is missing',2,'IPPower',-1) debug("ERROR: IP Address is missing " ..ipAddress.. " unable to progress") --return false luup.set_failure(1,lul_device) else -- if one is provided, present success message luup.task('IP Address for IPPower 9258 present, setup continues',4,'IPPower',-1) debug("IPPower StartUp activated - Device #" .. lul_device .. " now creating children") --luup.call_delay("IPPowerPoller", 5) --return true end end -- Initialize plug-in function IPPowerStartup(lul_device) -- set attributes for parent device luup.attr_set( "name", "IPPower 9258", lul_device) luup.attr_set( "category_num", "3", lul_device) luup.attr_set( "subcategory_num", "1", lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "Icon", 1, lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "PluginVersion", PV, lul_device) log("Start up, Parent device created...") child_devices = luup.chdev.start(lul_device) -- Tells Luup to start enumerating children for this device based on its dev_id for v = OUTLET_START,OUTLET_END do luup.chdev.append(lul_device,child_devices, "P" .. v, " Outlet " .. v, "urn:nodecentral-net:device:IPPower:1", "D_IPPower1.xml", "", "", false) debug("Child device created with id = P" .. v, " and description = Switch" .. v) end luup.chdev.sync(lul_device, child_devices) for k, v in pairs(luup.devices) do if (v.device_num_parent == lul_device) then -- populate child device table childDeviceIndex[v.id]=k end end checkIPPowerSetUp(lul_device) end
-
This is not required as you are not making a library/module component:
module("L_IPPower1", package.seeall)
Your implementation file says:
<startup>IPPowerStartup</startup>
yet your Lua code does not contain that function. So there's nothing to call. For testing, that function, could just write to the log and do nothing else. Don't forget every-time you change something you have to restart the Luup engine. This the slow apporach. Typically you would write and test your code first using some sort of test environment and then link it all into the implementation file.
After making any changes to your xml files with, you should check them for errors with a xml validator.
-
Thanks @a-lurker
Good to know, as I only added
module("L_IPPower1", package.seeall)
at the last minute, as I thought that might be the cause, and could see other like @toggledbits had itAs for the
IPPowerStartup
function, I do have that in there, it’s on line 66 in theL_IPPower1
file, shared above..What am I missing, I must be missing something ?
I’ll try variations, I assume it must be something like this
<startup>L_IPPower1.IPPowerStartup(lul_device)</startup>
instead then ?@a-lurker said in Add-on - Aviosys IPPower 9258:
Typically you would write and test your code first using some sort of test environment and then link it all into the implementation file.
After making any changes to your xml files with, you should check them for errors with a xml validator.
Yep, totally agree and that exactly what I’ve been doing, all the functions seem to check out fine in isolation - I’m just struggling to pull them all together into a stand-alone plugin.
As for the validators, totally agree, I’m a big user of the json one, and all my xml ones - all check out too.
-
I structure my implementation in modules, and load it in my startup section, not using
<files>
. This is an important distinction. You cannot mix and match styles. You have to go one way or the other. -
Ok, thanks @toggledbits , that makes sense, I’d like to get the
<files>
approach working at some point, but let’s try one with modules too.. Which means myI_IPPower.xml
will now be something like this..? With theL_IPPower.lua
having the seeall declaration this time..?<?xml version="1.0"?> <!-- I_IPPower.xml; Vera/openLuup "IPPower 9258" Plug-in V1.1 Nov 2021 --> <implementation> <functions> function initialiseIPPowerPlugin(lul_device) luup.log("IPPower Plugin START-UP!") IPPower = require("L_IPPower1") return IPPower.IPPowerStartup(lul_device) end </functions> <startup>initialiseIPPowerPlugin</startup> <actionList>
-
Oh no., somethings gone wrong, it’s now finding the start up but it’s going into some sort of loop of child device creations - and continual luup.reloads !!??
The function called by the implementation file is still this..
-- Initialize plug-in function IPPowerStartup(lul_device) -- set attributes for parent device luup.attr_set( "name", "IPPower 9258", lul_device) luup.attr_set( "category_num", "3", lul_device) luup.attr_set( "subcategory_num", "1", lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "Icon", 1, lul_device) luup.variable_set("urn:nodecentral-net:serviceId:IPPower1", "PluginVersion", PV, lul_device) log("Start up, Parent device created...") child_devices = luup.chdev.start(lul_device) -- Tells Luup to start enumerating children for this device based on it's dev_id for v = OUTLET_START,OUTLET_END do luup.chdev.append(lul_device,child_devices, "P" .. v, " Outlet " .. v, "urn:nodecentral-net:device:IPPower:1", "D_IPPower1.xml", "", "", false) debug("Child device created with id = P" .. v, " and description = Switch" .. v) end luup.chdev.sync(lul_device, child_devices) for k, v in pairs(luup.devices) do if (v.device_num_parent == lul_device) then -- populate child device table childDeviceIndex[v.id]=k end end checkIPPowerSetUp(lul_device) end
-
From what I can see for the Log file extract, it seems to keep incrementing (deleting and creating) the child devices ?
50 11/14/21 21:06:20.986 luup_log:600: IPPower Plugin START-UP! <0x76db4520> 06 11/14/21 21:06:20.990 Device_Variable::m_szValue_set device: 600 service: urn:nodecentral-net:serviceId:IPPower1 variable: Icon was: 1 now: 1 #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x76db4520> 06 11/14/21 21:06:20.990 Device_Variable::m_szValue_set device: 600 service: urn:nodecentral-net:serviceId:IPPower1 variable: PluginVersion was: 1.1 now: 1.1 #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x76db4520> 01 11/14/21 21:06:20.990 luup_log:600: IPPower: Start up, Parent device created... <0x76db4520> 50 11/14/21 21:06:20.991 luup_log:600: IPPower: debug: Child device created with id = P61 <0x76db4520> 50 11/14/21 21:06:20.991 luup_log:600: IPPower: debug: Child device created with id = P62 <0x76db4520> 50 11/14/21 21:06:20.992 luup_log:600: IPPower: debug: Child device created with id = P63 <0x76db4520> 50 11/14/21 21:06:20.992 luup_log:600: IPPower: debug: Child device created with id = P64 <0x76db4520> 50 11/14/21 21:06:20.992 luup_log:600: IPPower: debug: Child device created with id = P65 <0x76db4520> 50 11/14/21 21:06:20.992 luup_log:600: IPPower: debug: Child device created with id = P66 <0x76db4520> 50 11/14/21 21:06:20.993 luup_log:600: IPPower: debug: Child device created with id = P67 <0x76db4520> 50 11/14/21 21:06:20.993 luup_log:600: IPPower: debug: Child device created with id = P68 <0x76db4520> 09 11/14/21 21:06:20.993 Child_Devices::ProcessChildDevice deleting device 889 id P61 room 0 desc Outlet 61 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.011 UserData::CommitToDatabase data size 86066 86066 <0x76db4520> 02 11/14/21 21:06:21.107 Device_Basic::m_sDescription_set device 897. description: Outlet 61 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 61 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.109 Child_Devices::ProcessChildDevice created device 897 id P61 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.110 Child_Devices::ProcessChildDevice deleting device 890 id P62 room 0 desc Outlet 62 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.234 UserData::CommitToDatabase data size 85500 85500 <0x76db4520> 02 11/14/21 21:06:21.240 Device_Basic::m_sDescription_set device 898. description: Outlet 62 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 62 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.244 Child_Devices::ProcessChildDevice created device 898 id P62 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.244 Child_Devices::ProcessChildDevice deleting device 891 id P63 room 0 desc Outlet 63 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.376 UserData::CommitToDatabase data size 84934 84934 <0x76db4520> 02 11/14/21 21:06:21.381 Device_Basic::m_sDescription_set device 899. description: Outlet 63 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 63 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.386 Child_Devices::ProcessChildDevice created device 899 id P63 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.386 Child_Devices::ProcessChildDevice deleting device 892 id P64 room 0 desc Outlet 64 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.491 UserData::CommitToDatabase data size 84368 84368 <0x76db4520> 02 11/14/21 21:06:21.494 Device_Basic::m_sDescription_set device 900. description: Outlet 64 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 64 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.497 Child_Devices::ProcessChildDevice created device 900 id P64 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.497 Child_Devices::ProcessChildDevice deleting device 893 id P65 room 0 desc Outlet 65 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.568 UserData::CommitToDatabase data size 83802 83802 <0x76db4520> 02 11/14/21 21:06:21.572 Device_Basic::m_sDescription_set device 901. description: Outlet 65 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 65 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.575 Child_Devices::ProcessChildDevice created device 901 id P65 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.575 Child_Devices::ProcessChildDevice deleting device 894 id P66 room 0 desc Outlet 66 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.645 UserData::CommitToDatabase data size 83236 83236 <0x76db4520> 02 11/14/21 21:06:21.649 Device_Basic::m_sDescription_set device 902. description: Outlet 66 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 66 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.652 Child_Devices::ProcessChildDevice created device 902 id P66 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.652 Child_Devices::ProcessChildDevice deleting device 895 id P67 room 0 desc Outlet 67 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.722 UserData::CommitToDatabase data size 82670 82670 <0x76db4520> 02 11/14/21 21:06:21.726 Device_Basic::m_sDescription_set device 903. description: Outlet 67 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 67 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.728 Child_Devices::ProcessChildDevice created device 903 id P67 under 600 topmost parent 600 <0x76db4520> 09 11/14/21 21:06:21.729 Child_Devices::ProcessChildDevice deleting device 896 id P68 room 0 desc Outlet 68 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 under 600 topmost parent 600 because type o:urn:schemas-micasaverde-com:device:GenericIO:1!=n:urn:nodecentral-net:device:IPPower:1 or file o:D_IPPower1.xml!=n:D_IPPower1.xml embedded o:0!=n:0 ok:0/1 <0x76db4520> 02 11/14/21 21:06:21.798 UserData::CommitToDatabase data size 82104 82104 <0x76db4520> 02 11/14/21 21:06:21.802 Device_Basic::m_sDescription_set device 904. description: Outlet 68 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 exist already, new Description: Outlet 68 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <0x76db4520> 09 11/14/21 21:06:21.805 Child_Devices::ProcessChildDevice created device 904 id P68 under 600 topmost parent 600 <0x76db4520> 06 11/14/21 21:06:21.805 Device_Variable::m_szValue_set device: 1 service: urn:micasaverde-com:serviceId:ZWaveNetwork1 variable: LastDongleBackup was: now: #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x76db4520> 01 11/14/21 21:06:21.830 UserData::WriteUserData saved--before move File Size: 22154 save size 22154 <0x76bb4520> 02 11/14/21 21:06:21.831 UserData::TempLogFileSystemFailure start 0 <0x76bb4520> 02 11/14/21 21:06:21.847 UserData::TempLogFileSystemFailure (not failure, only WriteUserData) 0 <0x76bb4520> 02 11/14/21 21:06:21.848 UserData::TempLogFileSystemFailure 699 res:1
-
You have to be super-careful with how you structure your luup.chdev work. If the loop creates or deletes any child, then the
sync()
will cause a restart, so it's very easy to get reload loops. -
@toggledbits said in Add-on - Aviosys IPPower 9258:
You have to be super-careful with how you structure your luup.chdev work. If the loop creates or deletes any child, then the
sync()
will cause a restart, so it's very easy to get reload loops.Agreed, and it’s my first attempt at doing this, and i even remember making a note of the warning on the wiki - http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#Module:_luup.chdev .
When the final luup.chdev.sync is executed the children are checked. Any additions, changes or deletions will result in a Luup engine restart. Any incorrect coding of luup.chdev.append may result in a situation where the engine goes into a loop and continually restarts. As an example, if two children are added with the same id parameter, this will occur. To recover, you need to quickly upload a new file that has the faulty "append" code commented out. In the interim the engine will be incrementing the device ID numbers with no end in sight.
I can’t work out why the chdev is doing that , can you see any issue with my chdev.start and append that would cause the sync to do that ?
local OUTLET_START = 61 -- API returns the first socket as P61 local OUTLET_END = 68 -- depending on your device, it can be a 4 or 8 port, so 64 or 68 child_devices = luup.chdev.start(lul_device) -- Tells Luup to start enumerating children for this device based on it's dev_id , this returns: ptr (binary object) which is used in the cheer.append. for v = OUTLET_START,OUTLET_END do luup.chdev.append(lul_device,child_devices, "P" .. v, " Outlet " .. v, "urn:nodecentral-net:device:IPPower:1", "D_IPPower1.xml", "", "", false) debug("Child device created with id = P" .. v, " and description = Switch" .. v) end luup.chdev.sync(lul_device, child_devices) for k, v in pairs(luup.devices) do if (v.device_num_parent == lul_device) then -- populate child device table childDeviceIndex[v.id]=k end end
-
I’ve discovered that if I change the chdev.append so it creates a different device (see below), i don’t seem to get the loop issue.
I assume this is likely due to the previous set up had the append calling the same implementation file as the parent, which looks to create children - thus triggers a loop ?
for v = OUTLET_START,OUTLET_END do luup.chdev.append(lul_device,child_devices, "P" .. v, " Outlet " .. v, "urn:schemas-upnp-org:device:BinaryLight:1", "D_BinaryLight1.xml", "", "", false) end
-
Since you have
handleChildren
set to 1 in the D_.xml (device) file, the children should not receive an implementation file at all (it should be blank/empty string inappend()
). And it seems right that the children also should not have the same device type as the parent. -
Agreed, many thanks .
Well it looks like I’m almost there now, the only thing left to fix is the the luup.call_timer I have on the main polling function in L_IPPower1.lua file .
Here’s the function..
function IPPowerPoller() -- Call IPPower to return state of each outlet local user = "admin" local pass = "12345678" debug("Started Polling IPPower Device") local url = "http://" .. ipAddress .. "/set.cmd?user=".. user .. "+pass=" .. pass .. "+cmd=getpower" debug("Calling URL " .. url) local status, data = luup.inet.wget(url) if (data) then debug("Response received " .. data) -- Example response is "P61=0,P62=1,P63=0,P64=0,P65=1,P66=0,P67=0,P68=0" else log("Empty response returned") end for v = OUTLET_START,OUTLET_END do local value = string.match(data, "P"..v.."=(%d)") if (value) then luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", value, childDeviceIndex["P"..v]) else log("Empty value returned") end end debug("Finished Polling IPPower Device") luup.call_timer("IPPowerPoller", 1, 30, "", "") end
Which returns this..
01 11/15/21 0:00:58.100 LuaInterface::CallFunction_Timer-5 function IPPowerPoller failed attempt to call a nil value <0x74e48520>
I might just be tired, but I can’t work out the cause here, any ideas?
I’ve made it a local and a global function, called it with and without the module name, all return the same nil value error?
-
@parkerc said in Add-on - Aviosys IPPower 9258:
I might just be tired, but I can’t work out the cause here, any ideas?
I’ve made it a local and a global function, called it with and without the module name, all return the same nil value error?If you're getting the same
nil
error, you're somehow not making a global, or making it with the correct name/using the correct name. In order to help there, I'd need to see the current I_ and L_ files in their entirety.But before you do that, I think you said you were looking at the SiteSensor code as an example, and you invoked the "module" word, so... in the I_SiteSensor file, look at how I create a global called
siteSensorWatch
that is an alias for the module's function. This creates a global, which must be done in the scope of the implementation (I_) file if the L_ file is a module. Then in my L_SiteSensor module, you can see in theluup.variable_watch
statements where I'm using that name -- the function name parameter for that works the same as it does forcall_delay
andcall_timer
. Check that out, and see if you can work out what you didn't get to there. -
@toggledbits said in Add-on - Aviosys IPPower 9258:
…… in the I_SiteSensor file, look at how I create a global called
siteSensorWatch
that is an alias for the module's function. This creates a global, which must be done in the scope of the implementation (I_) file if the L_ file is a module.I had a look in your Implementation file , but could not see anything called
siteSensorWatch
listed?Here is the opening of my a I_IPPower.xml` Implementation file..
<?xml version="1.0"?> <!-- I_IPPower.xml; Vera/openLuup "IPPower 9258" Plug-in V1.1 Nov 2021 --> <implementation> <functions> function initialiseIPPowerPlugin(lul_device) luup.log("IPPower: IPPower Plugin STARTING-UP!") IPPower = require("L_IPPower1") return IPPower.IPPowerStartup(lul_device) end </functions> <startup>initialiseIPPowerPlugin</startup> <actionList> <action>
@toggledbits said in Add-on - Aviosys IPPower 9258:
Then in my L_SiteSensor module, you can see in the
luup.variable_watch
statements where I'm using that name -- the function name parameter for that works the same as it does forcall_delay
andcall_timer
. Check that out, and see if you can work out what you didn't get to there.Right, I’m on it, I couldn’t find any luup.variable_watch calls, but I did find a
luup.call_delay
in yourL_SiteSensor
luup.call_delay("siteSensorRunQuery", delay, string.format("%d:%d", stamp, dev))
Which links to the following assocation in your siteSensor Implementation file
siteSensorRunQuery = SiteSensor.runQuery
So that’s where I’ll focus..
-
Sorry, I'm always looking at the development version, but you've followed the right trail.
runQuery
works the same way. -
Thanks @toggledbits - it’s all working now !!
My last piece is back to my other post around error handling, and how I can present that better to the user, and so for that topic, I;ll revert back to that thread.. - > https://smarthome.community/topic/777/luup-error-handling/9