Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Cafun

Member
  • Posts

    312
  • Joined

  • Last visited

About Cafun

Profile information

  • Country
    The Netherlands
  • Gateway/s
    Home Center 2

Recent Profile Visitors

278 profile views

Cafun's Achievements

Smart User

Smart User (3/8)

13

Reputation

1

Community Answers

  1. It's working great, thank you! Are you still developing some things for it? Just curious
  2. Oh damn, I didn't got a notification you replied! I'll try it. Thanks! And where can I find the 1.0.1? It's not on your github? Edit: It works! Thanks, Now I can do some detailed programming for the curtains (normally I let it open and close at sunrise and sunset, but now that's not sufficient anymore). If I can get your updated code, I would be happy to.
  3. I've used the code and virtual device from your github. Here is my scene: --[[ --[[ %% properties %% events %% globals --]] --[[ SLIDE Api Author: Gabor de Wit Github referece: https://github.com/gabordewit/SLIDE Versions 1.0 Initial release ToDo - request realtime status of current slide to be reflected in Virtual device - set holiday modes - start calibration of device --]] --set environment variables that can be used to switch logger on/off. debug = true dryrun = false testIndividualSlide = false slideIdToTest = 1867 slideCommandToTest = "open" function debuglogger(contentToLog) if (debug) then fibaro:debug(tostring(contentToLog)) end end -- set userdata for SLIDE api including URL. userName = "xx@xx" password = "xx" slideApiUrl = "https://api.goslide.io/api" -- set global userdata variable for storing tokens. for dryrun purposes it's possible to take the output and store here. authData = { ["access_token"] = "", ["token_type"] = "Bearer", ["expires_at"] = "2020-03-10 09:48:38", ["expires_in"] = "2592000", ["refresh_token"] = "", ["household_id"] = "15" } -- global variable to set amount of slides numberOfSlides = 2 -- set global householdata variable for storing slide information, including example data model slides = { --]] ["id"], { ["id"] = decodedHouseholdResponse.slides[slideCount].id, ["device_name"] = decodedHouseholdResponse.slides[slideCount].device_name, ["device_id"] = decodedHouseholdResponse.slides[slideCount].device_id, ["zone_id"] = decodedHouseholdResponse.slides[slideCount].zone_id } --]] } --[[ In orde to use the SLIDE api, a user needs to be authenticated with the platform. The data used needs to be provisioned above. --]] function getToken(refreshHousehold) debuglogger("Calling getToken function for access") -- setup variables to include in the call - include json encoding local requestStr = {email = userName, password = password} local requestTringToJSON = json.encode(requestStr) debuglogger("RequestStr:" .. requestTringToJSON) local selfhttp = net.HTTPClient() local endPoint = "/auth/login" local headers = { ["content-type"] = "application/json", ["X-Requested-With"] = "XMLHttpRequest" } local url = slideApiUrl .. endPoint debuglogger(url) -- The actual request selfhttp:request( url, { options = { headers = headers, method = "POST", data = requestTringToJSON }, success = function(resp) -- Check for response coming from API debuglogger("Got a response: " .. resp.data) decodedTokenResponse = json.decode(resp.data) -- Store retrieved JSON response in a map authData.access_token = decodedTokenResponse.access_token authData.token_type = decodedTokenResponse.token_type authData.expires_at = decodedTokenResponse.expires_at authData.expires_in = decodedTokenResponse.expires_in authData.household_id = decodedTokenResponse.household_id -- store json value in global token (if this is not a dryrun) if dryrun == false then fibaro:setGlobal("Slidetoken", json.encode(authData)) else debuglogger("Performing dry-run, not storing tokens") end debuglogger("Token needed for upcoming request: " .. authData.access_token) -- Loop over new values, evaluate if they are stored correctly for tokenKey, value in pairs(authData) do debuglogger("Authdata key: " .. tokenKey .. ", stored value: " .. value) end -- to speed up onboarding in Fibaro we're also getting household information already. -- SLIDES will be provisioned in the virtual device so set that up first if refreshHousehold then debuglogger("Refresh of household triggered") getHouseholdInfo() else debuglogger("Not refreshing household") end end, error = function(err) -- response = json.decode(err) fibaro:debug("error: " .. err) end } ) end -- decoded global value for slidetoken if dryrun then fibaro:debug("Not requesting global variable due to dryrun") decodedGlobal = authData elseif dryrun == false and fibaro:getGlobalValue("Slidetoken") ~= nil then decodedGlobal = json.decode(fibaro:getGlobalValue("Slidetoken")) else fibaro:debug("Warning, no global value set yet for Slidetoken") end --[[ This is one of the most important API's, as this API will allow you to set the technical id's of the slides to use in other API's. The data is store in a global variable. The auth data is used here instead of the global value since this will improve latency and prevent race conditions. --]] function getHouseholdInfo() debuglogger("Calling householdinfo for household paramters") local selfhttp = net.HTTPClient() local endPoint = "/slides/overview" local headers = { ["content-type"] = "application/json", ["X-Requested-With"] = "XMLHttpRequest", ["Authorization"] = "Bearer " .. authData.access_token } local url = slideApiUrl .. endPoint debuglogger(url) selfhttp:request( url, { options = { headers = headers, method = "GET" }, success = function(resp) debuglogger("Response on household call: " .. resp.data) decodedHouseholdResponse = json.decode(resp.data) for slideCount = 1, #decodedHouseholdResponse.slides do debuglogger( "Looping over slides in household, loop sequence: " .. slideCount .. " ID found: " .. decodedHouseholdResponse.slides[slideCount].id ) dataInHousehold = { ["id"] = decodedHouseholdResponse.slides[slideCount].id, ["device_name"] = decodedHouseholdResponse.slides[slideCount].device_name, ["device_id"] = decodedHouseholdResponse.slides[slideCount].device_id, ["zone_id"] = decodedHouseholdResponse.slides[slideCount].zone_id } -- setcounter on slide identifier slideNumberID = "slide" .. slideCount numberOfSlides = slideCount -- merge the slide data to it's own ID slides[slideNumberID] = dataInHousehold end -- store json values in global token, thhis needs to be set before using these api's otherwise -- data is not available. if dryrun == false then -- store slide data in a global variable - needs to be created first fibaro:setGlobal("Slides", json.encode(slides)) -- store number of slides in global variable - needs to be created first fibaro:setGlobal("Slidehousehold", numberOfSlides) elseif dryrun and testIndividualSlide then debuglogger("Testing individual slide in dryrun mode: " .. slideIdToTest) setSlide(slideIdToTest, slideCommandToTest) else debuglogger("Performing dry-run, not storing tokens or setting slides") end -- list the amount of slides in the house and list the individual slides debuglogger("Number of slides in household: " .. numberOfSlides) for slideNumber, value in pairs(slides) do debuglogger("Slide ID to be stored in memory: " .. slideNumber) end end, error = function(err) --response = json.decode(err) fibaro:debug("error: " .. err) end } ) end --[[ In orde to set an individual SLIDE , we can send a request to an individual slide the api already supports the setting of anything other than close and open but this is open for VFID to use. --]] function setSlide(slideId, command) debuglogger("Calling setslide for specific slide") local selfhttp = net.HTTPClient() local endPoint = "/slide/" .. slideId .. "/position" local requestStr = {pos = 1} if command == "open" then requestStr = {pos = 0} elseif command == "close" then requestStr = {pos = 1} else requestStr = {pos = command} end local requestTringToJSON = json.encode(requestStr) debuglogger("Request send to slide is: " .. requestTringToJSON) local headers = { ["content-type"] = "application/json", ["X-Requested-With"] = "XMLHttpRequest", ["Authorization"] = "Bearer " .. decodedGlobal.access_token } local url = slideApiUrl .. endPoint debuglogger(url) selfhttp:request( url, { options = { headers = headers, method = "POST", data = requestTringToJSON }, success = function(resp) debuglogger("Response on set slide call: " .. resp.data) decodedSlideResponse = json.decode(resp.data) end, error = function(err) -- response = json.decode(err) fibaro:debug("error: " .. err) end } ) end --[[ If needed the information of an individual slide can be called (e.g. to see if the slide is open etc. For use in future operations. --]] function getSlideInfo(slideId) debuglogger("Calling slide info for slide parameters") local selfhttp = net.HTTPClient() local endPoint = "/slides/" .. slideId local headers = { ["content-type"] = "application/json", ["X-Requested-With"] = "XMLHttpRequest", ["Authorization"] = "Bearer " .. decodedGlobal.access_token } local url = slideApiUrl .. endPoint debuglogger(url) selfhttp:request( url, { options = { headers = headers, method = "GET" }, success = function(resp) debuglogger("Success on slide call: " .. resp.data) decodedSlideResponse = json.decode(resp.data) end, error = function(err) -- response = json.decode(err) fibaro:debug("error: " .. err) end } ) end --[[ The scene uses a input command mechanism as if it was an API. Using Fibaro's method of allowing virtual devices to call a scene with input parameters, the scene van be triggered from virtual devices. --]] args = fibaro:args() if dryrun then refreshHousehold = true getToken(refreshHousehold) elseif args == nil then print("No commands or dry run request received") elseif args ~= nil and args[1] == "requestNewToken" then request = args[1] refreshHousehold = args[2] print("Request " .. request) getToken(refreshHousehold) elseif args ~= nil and args[1] == "controlSlide" then request = args[1] slideToCommand = args[2] slideCommand = args[3] print("Request " .. request) debuglogger("slideToCommand " .. slideToCommand) debuglogger("slideCommand " .. slideCommand) -- Check for existence of a token to be able to make calls if (decodedGlobal.access_token ~= "" and os.date("%Y-%m-%d %X") < decodedGlobal.expires_at) then setSlide(slideToCommand, slideCommand) print("Token found and not expired, expiry date: " .. authData.expires_at) else print("No valid token found (not present or expired), triggering function to retrieve a new token") print("The action you've intended to execute might have failed, please try again after token refresh") refreshHousehold = true getToken(refreshHousehold) end else print("Unknown error occurred, please check config syntax") end
  4. @gabordewit I have downloaded the scene/vib from your github. When I want to initialize the slides I get the error below. Hope you can help me with this: [DEBUG] 09:37:56: 2020-03-06 09:37:56.774388 [ fatal] Unknown exception: /opt/fibaro/scenes/264.lua:54: '=' expected near ','
  5. @gabordewit Thnx! I'm going to try it!
  6. Nice! Can't wait to integrate it in Fibaro
  7. No one can help me with the JSON code?
  8. Hi muc, thank you for your reply. I would try your suggestion, but it appears I'm stuck at row 1: [ERROR] 09:05:43: line 1: attempt to index global 'net' (a nil value) I'll dig a little deeper. If anyone has suggestions, I'm open to it. Thanks. Edit: I guess it needs to be a scene instead of a virtual device to call the 'net.HTTPClient()' command. But now I get the following error: [DEBUG] 11:05:52: 2019-12-15 11:05:52.306106 [ fatal] Unknown exception: /opt/fibaro/scenes/262.lua:21: attempt to index global 'ltn12' (a nil value) I'm going to lookup some other examples, hopefully I get it working. @Bodyart I hope you've got your slide back already?
  9. Ah cool! Thanks for the response! Hopefully you can soon continue with testing. If you want me to test anything, I will be happy to. In the mean time, I hope someone can help me with the problem.
  10. Hi community! I was hoping someone can help me out with something. So, I got Slide to open/close my curtains. They've got an OpenAPI. At this moment there is no integration Fibaro (but they say it will be). I also do have Home Assistant and with that I can control my curtains. I know it's not ideal, but I would like to use the Home Assistant API to open/close my curtains. So when I press a button on a Zwave remote, it opens/closes my curtains. I don''t have any knowledge about JSON and http requests into Lua. I have the code below, but it doesn't work (I didn't expected to ). At this moment it I get the error: [ERROR] 07:22:13: line 3: '' expected near '" "' But I don't think it's the only thing which isn't right. They payload I defined should be in the body. local http = net.HTTPClient() local siteurl = "http://ip/api/services/cover/open_cover" local payload = "{"entity_id:" "cover.raam_voor"}" local respback = { } local res, code, response_headers, status = http.request { url = siteurl, method = "POST", headers = { ["Content-Type"] = "application/json", ["authorization"] = "Bearer xxxx", ["Content-Length"] = payload:len() }, source = ltn12.source.string(payload), sink = ltn12.sink.table(respback) }
  11. Hi! This scene has worked for years, but since this friday, I get the following error in the debug log: [DEBUG] 21:25:36: Success: Invalid argument Anyone knows what I can do about it? I didn't changed a thing. Thanks in advance.
  12. Hi! You can make a block scene. As a trigger you can use the timers. If you only want Lua scenes you can convert the block scene to Lua after saving:
  13. It's working perfect with Fibaro. The device is recognized by Fibaro, so no special template or manually configure of parameters needed.
×
×
  • Create New...