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


  • 0

LUA scripting


Question

Posted

I have a HC2 with V 4.018 Beta.

I also have a Heavy duty switch that i would like to switch on/off.

There is an issue with Beta and the controller.

If i use the URLhttp://192.168.1.30/api/callAction?deviceID=120&name=turnOn or same for turnoff. I'm able to switch the device on or off.

I have looked at 100 examples and can not get this right in any manner.

Assistance would be greatly appreciated if someone could assist with some LUA coding.

11 answers to this question

Recommended Posts

  • 0
Posted

Show us what you got till now; then it might show where the problem is in your code.

  • 0
  • Inquirer
  • Posted

    Hi sve.

    Thank you for the reply. The one method i tried was:

    HC2 = Net.FHttp('192.168.1.1',8080)

    HC2:setBasicAuthentication('admin','admin')

    HC2Reponse = HC2:POST('/api/callAction?deviceID=120&name=turnOff')

    It gave me an error of [ERROR] 21:37:20: line 1: attempt to index global 'Net' (a nil value)

    I also tried something simple like:

    --[[

    %% properties

    %% globals

    --]]

    fibaro:call(120, "turnOff")

    this option didn't give an error, but didn't actually do anything.

    I also started to grasp at straws and tried using :

    POST

    Please login or register to see this link.

    With out any luck. I tried to look for a tutorial on Lua but focused at Fibaro but most of it was more focused at Lua as general programming.

    Regards

    Trevor

    • 0
    Posted

    Net is not available in scene which means that it will never work.

    Second does HC2 not allow authentication like you mention.

    The LUA code should work... which type of switch are you using? Does it support the turnOff command?

    • 0
    Posted

    for http request on beta you should use

    Please login or register to see this code.

    and now the correct way for the API is: /api/devices/xxx/action/turnOff

    and finally in scene you're right "fibaro:call(120, "turnOff")" seems not work on the V4.018 beta, wait V4.019beta

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    • 0
  • Inquirer
  • Posted

    Dear KrikRoff

    Thank you very much for that.... Clearly i have a lot to learn. So i made i minor change:

    local url = "http://192.168.1.30/api/callAction?deviceID=120&name=turnOn"

    local httpClient = net.HTTPClient( { 1000 } )

    httpClient:request( url , {

    options = {

    method = 'POST',

    headers = {

    ["Content-Type"] = "application/x-www-form-urlencoded"

    }

    },

    success = function(result)

    local status = tonumber(result.status)

    local response = result.data

    local log = ""

    if (status ~= 200) then

    log = "Error while sending request. Status code: " .. tostring(status) .. ", Body: " .. tostring(response)

    elseif (status == 200) then

    log = "success"

    end

    print(log)

    end,

    error = function (error)

    print('error: '.. error)

    end

    })

    But i'm getting an error:

    [DEBUG] 19:20:06: Error while sending request. Status code: 401, Body:

    I have looked up the error and it appears to refer to access rights. So i changed the url to

    Please login or register to see this link.

    Still received the same error. I also tried to change the url to read as the example you send me.. /api/.... (but knew i was taking a chance)

    Any ideas?

    Thanking you in advance.

    • 0
    Posted

    401 - error = authorization problem

    you got 2 solutions add to url password or use it locally

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    Hi.

    Thank you.

    So i tried option 1. It still gave me 401 error message. I made triple sure of the username and password.

    Option 2 then gave me a 405 error implying that method not allowed.

    really baffled.

    Appreciate the help.

    • 0
    Posted

    try

    Please login or register to see this code.

    GET Method is the best option for turnOn / turnOff

    • Like 1
    • 0
  • Inquirer
  • Posted

    Im really not winning with this. hehe.

    So i made the changes.

    local url = "http://127.0.0.1:11111/api/callAction?deviceID=122&name=turnOff"

    local httpClient = net.HTTPClient( { 1000 } )

    httpClient:request( url , {

    options = {

    method = 'GET'

    },

    success = function(result)

    local status = tonumber(result.status)

    local response = result.data

    local log = ""

    if (status == 200 or status == 202) then

    log = "success"

    else

    log = "Error while sending request. Status code: " .. tostring(status) .. ", Body: " .. tostring(response)

    end

    print(log)

    end,

    error = function (error)

    print('error: '.. error)

    end

    })

    It says successful, but still doesn't actually change the status of on/off of the device. It is a aeotec heavy duty smart switch. It says successful, but nothing happens.

    • 0
    Posted

    And if you change local url = "http://127.0.0.1:11111/api/callAction?deviceID=122&name=turnOff" tunrOff parameter with turnOn ?

    • 0
  • Inquirer
  • Posted

    Neither turnoff or turnoff are working.

    So something strange.

    It works once, then doesn't work again. Ie. The first time tried it by switching it on and then off it worked. When i tried it again, it didn't. Without changing anything between the 2 sessions.

    [ Added: 2014-11-25, 20:20 ]

    Hi guys.

    Not actually sure what sorted the problem out.

    But un-installed the device and reloaded it. All working fine now.

    Thank you to all who contrabuted.

    Thank you.

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...