Skip to content

Code/Snippet library

Share and discuss your lua creations here

39 Topics 230 Posts
  • Scene number?

    4
    0 Votes
    4 Posts
    833 Views
    akbooerA
    Why use any individual scene code at all? Per the reference docs you cited, if you define a scene_prolog global (presumably with the exact same code as your existing executeSceneNumber) that should work directly.
  • Embarrassing mind blank

    6
    0 Votes
    6 Posts
    559 Views
    CatmanV2C
    @therealdb thanks so much! C
  • Lua - multi-part/form submission (Paperless-ngx API)

    3
    0 Votes
    3 Posts
    1k Views
    parkercP
    Managed to get there in the end, posting here to help others.. local http = require("socket.http") local ltn12 = require("ltn12") local mime = require("mime") local lfs = require("lfs") local username = "username," local password = "password" http.TIMEOUT = 5 local function upload_file ( url, filename, title, correspondent ) local fileHandle = io.open( filename,"rb") local fileContent = fileHandle:read( "*a" ) fileHandle:close() local boundary = 'abcd' local cdfd = 'Content-Disposition: form-data; ' local header_b = cdfd.. 'name="document"; filename="' ..filename.. '"\r\nContent-Type: application/pdf' local header_c = cdfd.. 'name="title"\r\n\r\n'..title local header_d = cdfd.. 'name="correspondent"\r\n\r\n'..correspondent local MP_b = '--'..boundary..'\r\n'..header_b..'\r\n\r\n'..fileContent..'\r\n' local MP_c = '--'..boundary..'\r\n'..header_c..'\r\n' local MP_d = '--'..boundary..'\r\n'..header_d..'\r\n' local MPCombined = MP_b..MP_c..MP_d..'--'..boundary..'--\r\n' local response_body = { } local _, code = http.request { url = url , method = "POST", headers = { ["Authorization"] = "Basic " .. (mime.b64(username ..":" .. password)), ["Content-Length"] = MPCombined:len(), ['Content-Type'] = 'multipart/form-data; boundary=' .. boundary }, source = ltn12.source.string(MPCombined) , sink = ltn12.sink.table(response_body), } return code, table.concat(response_body) end -- local rc,content = upload_file ('http://httpbin.org/post', '/mnt/nas/10.pdf' ) local rc,content = upload_file ('http://192.168.1.134:8777/api/documents/post_document/', '/mnt/nas/10.pdf', 'Companies House', '12') print(rc,content)
  • This topic is deleted!

    1
    0 Votes
    1 Posts
    5 Views
    No one has replied
  • Lua : Downloading a file over https

    10
    0 Votes
    10 Posts
    2k Views
    parkercP
    Ok, not had much luck with the variations I tried, One thing I did notice was that the luasec version on VeraPlus is v0.4 and the openssl version is v1.0.2 # openssl version OpenSSL 1.0.2l 25 May 2017 Checking the luasec guidance here, https://github.com/brunoos/luasec/wiki/LuaSec-1.0.x , that VeraPlus openSSL version supports luasec v 1.0.x ? Without being able to install/use Luarocks, any idea how to upgrade from luasec 0.4 to 1.0.x?
  • This topic is deleted!

    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • This topic is deleted!

    3
    0 Votes
    3 Posts
    5 Views
    No one has replied
  • Luup : Fitbit API

    10
    0 Votes
    10 Posts
    962 Views
    therealdbT
    You’ll probably need to get it by hand. Ie, write a small page with some JavaScript hosted somewhere that will get the code and that you’ll have to copy and paste manually.
  • Luup : Error Handling

    12
    0 Votes
    12 Posts
    1k Views
    parkercP
    Never thought I’d utter these words; but I’ve been a bit prolific of late building plugins !! (9 in the last month or so ) ok, so they may not be the most elegant in design, but they all seem to work, which is the main thing Working on number 10, I’ve returned to the error handling topic, as I’m curious about the pros/cons of luup.set_failure() vs luup.device_message() luup.set_failure seems so impersonal and rigid (creating multi-variables) compared the device_messages - what is the value of set_failure over say a ‘job error’ red device message ? What are peoples thoughts ?
  • Help with luup.chdev.sync

    14
    1 Votes
    14 Posts
    1k Views
    parkercP
    No, sorry - that was my altered version I had changed it from using two separate functions to just the one. I tried to do as you said, but I couldn’t see what else I could remove without breaking it ?
  • 0 Votes
    12 Posts
    1k Views
    parkercP
    Yes, i think you’re right, i love how coding this sort of thing make me have to think differently I’ve also started to add a multi_state_button too, but classic vera UI, I just can’t get it positioned where I want it too go.. [image: 1638285341953-83123bb1-1401-4b29-b034-9b921793bda2.jpeg] I really want the on/off toggle button to be in line with the “Output 1” wording, and the right chevron “>” Worse case I revert to a standard button, current .json is pasted below., if anyone can find out how to lift it up a level/row.. { "default_icon": "../../../icons/tv_100.png", "state_icons":[], "x": "2", "y": "3", "inScene": "1", "ToggleButton": 1, "Tabs": [ { "Label": { "lang_tag": "tabname_control", "text": "Control" }, "Position": "0", "TabType": "flash", "top_navigation_tab": 1, "ControlGroup": [ { "id": "1", "scenegroup": "1", "isSingle": "1" }, { "id": "2", "scenegroup": "1", "isSingle": "1" } ], "SceneGroup": [ { "id": "1", "top": "2", "left": "0", "x": "2", "y": "1" } ], "Control": [ { "ControlGroup": "1", "ControlType": "button", "top": "0", "left": "0", "Label": { "lang_tag": "Input1", "text": "Input1" }, "Display": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Variable": "Pressed1", "Value": "1", "Top": 60, "Left": 50, "Width": 75, "Height": 20 }, "Command": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Action": "SYCall", "Parameters": [ { "Name": "action", "Value": "I1" } ] } }, { "ControlGroup": "1", "ControlType": "button", "top": "0", "left": "1", "Label": { "lang_tag": "Input2", "text": "Input2" }, "Display": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Variable": "Pressed2", "Value": "1", "Top": 60, "Left": 150, "Width": 75, "Height": 20 }, "Command": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Action": "SYCall", "Parameters": [ { "Name": "action", "Value": "I2" } ] } }, { "ControlGroup": "2", "ControlType": "button", "top": "0", "left": "1", "Label": { "lang_tag": "Input3", "text": "Input3" }, "Display": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Variable": "Pressed3", "Value": "1", "Top": 60, "Left": 250, "Width": 75, "Height": 20 }, "Command": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Action": "SYCall", "Parameters": [ { "Name": "action", "Value": "I3" } ] } }, { "ControlGroup": "2", "ControlType": "button", "top": "0", "left": "1", "Label": { "lang_tag": "Input4", "text": "Input4" }, "Display": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Variable": "Pressed4", "Value": "1", "Top": 60, "Left": 350, "Width": 75, "Height": 20 }, "Command": { "Service": "urn:nodecentral-net:serviceId:SYMatrix1", "Action": "SYCall", "Parameters": [ { "Name": "action", "Value": "I4" } ] } }, { "ControlGroup": "2", "ControlType": "multi_state_button", "top": "0", "left": "1", "Display": { "Top": 0, "Left": 450 }, "states":[ { "Label": { "lang_tag": "ui7_cmd_on", "text": "On" }, "ControlGroup": "2", "Display": { "Service": "urn:upnp-org:serviceId:SwitchPower1", "Variable": "Status", "Value": "1" }, "Command": { "Service": "urn:upnp-org:serviceId:SwitchPower1", "Action": "SetTarget", "Parameters": [ { "Name": "newTargetValue", "Value": "1" } ] }, "ControlCode": "power_on" }, { "Label": { "lang_tag": "ui7_cmd_off", "text": "Off" }, "ControlGroup": "2", "Display": { "Service": "urn:upnp-org:serviceId:SwitchPower1", "Variable": "Status", "Value": "0" }, "Command": { "Service": "urn:upnp-org:serviceId:SwitchPower1", "Action": "SetTarget", "Parameters": [ { "Name": "newTargetValue", "Value": "0" } ] }, "ControlCode": "power_off" } ] } ] }, { "Label": { "lang_tag": "settings", "text": "Settings" }, "Position": "1", "TabType": "javascript", "ScriptName": "shared.js", "Function": "simple_device" }, { "Label": { "lang_tag": "advanced", "text": "Advanced" }, "Position": "2", "TabType": "javascript", "ScriptName": "shared.js", "Function": "advanced_device" }, { "Label": { "lang_tag": "notifications", "text": "Notifications" }, "Position": "3", "TabType": "javascript", "ScriptName": "shared.js", "Function": "device_notifications" } ], "sceneList": { "group_1": { "cmd_1": { "label": "ON", "serviceId": "urn:upnp-org:serviceId:SwitchPower1", "action": "SetTarget", "arguments": { "newTargetValue": "1" }, "display": { "service": "urn:upnp-org:serviceId:SwitchPower1", "variable": "Status", "value": "1" } }, "cmd_2": { "label": "OFF", "serviceId": "urn:upnp-org:serviceId:SwitchPower1", "action": "SetTarget", "arguments": { "newTargetValue": "0" }, "display": { "service": "urn:upnp-org:serviceId:SwitchPower1", "variable": "Status", "value": "0" } } } }, "eventList2": [ { "id": 1, "label": { "lang_tag": "ui7_a_device_is_turned_on_off", "text": "A device is turned on or off" }, "serviceId": "urn:upnp-org:serviceId:SwitchPower1", "argumentList": [ { "id": 1, "dataType": "boolean", "defaultValue": "1", "allowedValueList": [ { "Off": "0", "HumanFriendlyText": { "lang_tag": "ui7_hft_device_turned_off", "text": "Whenever the _DEVICE_NAME_ is turned off" } }, { "On": "1", "HumanFriendlyText": { "lang_tag": "ui7_hft_device_turned_on", "text": "Whenever the _DEVICE_NAME_ is turned on" } } ], "name": "Status", "comparisson": "=", "prefix": { "lang_tag": "ui7_which_mode", "text": "Which mode" }, "suffix": {} } ] } ], "DeviceType": "urn:nodecentral-net:device:SYMatrix:1", "device_type": "urn:nodecentral-net:device:SYMatrix:1" } I managed to get it looking roughly right on the Control Tab, see below.. but this positioning is not reflected on the main UI (see image above) [image: 1638286249114-23387124-ef0b-45d0-bd15-68dd3d9f772f.jpeg]
  • 0 Votes
    6 Posts
    822 Views
    wmarcolinW
    Okay, it will be a long road of trial and error without mastering the language yet.
  • 0 Votes
    14 Posts
    3k Views
    toggledbitsT
    @parkerc said in Lua : http(s) request, works on some URLs, but not all ? But all work via the browser..: Admittedly it would be really nice if (one day) Vera could have an all encompassing http/web module Sadly, I don't think anything new will be coming to Vera from here forward...
  • Add-on - Aviosys IPPower 9258

    17
    0 Votes
    17 Posts
    1k Views
    parkercP
    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
  • Lua : storage.set , storage.get

    3
    0 Votes
    3 Posts
    782 Views
    parkercP
    Thanks @toggledbits , I really do like the simplicity of that storage.set and storage.get .and the potential for a persistent store. I did find some discussions on the Vera site, https://community.ezlo.com/t/where-to-store-persistent-data-from-lua/184647/2. plus I did do a quick surf and found a few examples, sadly while the first one looked promising depends on SQLlite, which I’m not sure can run on Vera. https://github.com/interfaceware/iguana-tools/blob/master/shared/store.lua This seems to relate to what you said about using files. https://www.lua.org/manual/2.1/subsection3_7_3.html
  • 3 Votes
    14 Posts
    1k Views
    CatmanV2C
    @librasun said in Get your Alexa replies to the device you've asked them to with VeraAlexa plug-in: I always know the day the cookie expires, even before MSR sends me the warning, because a very specific nighttime Rule (to turn off the screen of my Echo Spot) will fail to run, so the Echo Spot doesn't go "boop" at bedtime. Either my 'Shutting down the power / Sonic deadline is up' (1) won't be said, or the morning temperature report tends to tell me. C (1) Virtual coconut(s) for anyone that can get the movie links
  • Luup : Hue Energy (watts) Calculator

    17
    0 Votes
    17 Posts
    1k Views
    parkercP
    Thank you so much @akbooer , that made it work ! But what’s weird, is that I had tried various different ways too, with the following the closest to yours . local hueenergy = require("xxhuewatts") -- lua filename/module huepower = hueenergy.getHueLight -- call lua filename/module and required function luup.register_handler ("huepower", "huewatts") -- register http handler When it comes to the ‘syntax’ , why did it like “ GetHueLight” and not “ huepower” Does Lua (Vera/OpenLuup?) require me to use capital letters at certain times ?
  • UDP Datagrams

    4
    0 Votes
    4 Posts
    488 Views
    therealdbT
    So, I've left it running all the night and it's pushing things like a pro to my MQTT broker. Bonus points: I've finally migrated my dashboard to WebSockets, instead of long-polling. I can't finish to press a button that the UI is updated. Brilliant! I've finally rewritten my own VeraBridge in C#, so no polling and less CPU usage on my Vera I'll probably port more things to my C# app, at this point, since I now have sub-second updates of variables I modified other parts where I was calling http endpoints from my Vera, to push UDP datagrams. I still must find some time to fully test openLuup's MQTT broker, but @akbooer you already gave me a lot of inspiration, as always. Thanks!
  • Luup : Whole house energy usage.

    3
    0 Votes
    3 Posts
    376 Views
    parkercP
    Agreed, sadly I only found out about it after I’d create my version but it was still a good learning curve, adds a total to it, and allows me to present it in the format that works for me at the time. I wonder how many other hidden bits like this are out there..
  • Luup : Foscam API

    5
    2 Votes
    5 Posts
    739 Views
    parkercP
    @rafale77 - (FYI) I’ve managed to pick up an Amcrest camera , so it’s going to be interesting to see how they compare to my Foscam .

Recent Topics