Jump to content
Guides for the Forum Read more... ×
Poradniki na Forum Read more... ×
  • 0
AutoFrank

creating variables using lua or thru API

Question

HI,

Is it possible to create a variable (specifically a predefined variable)

 

As part of creating a VD I want to create the variable on the fly

 

I looked at the developer documentation but couldn't find anything

Is this possible ?

 

Thanks

-f

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • On 12/22/2016 at 9:20 PM, AutoFrank said:

    HI,

    Is it possible to create a variable (specifically a predefined variable)

     

    As part of creating a VD I want to create the variable on the fly

     

    I looked at the developer documentation but couldn't find anything

    Is this possible ?

     

    Thanks

    -f

     

    bumping this ...

    Share this post


    Link to post
    Share on other sites
    • 0

    There are VD's that do this. @jompa68 Alarmclock does this

    8 minutes ago, AutoFrank said:

     

    bumping this ...

     

    Share this post


    Link to post
    Share on other sites
    • 0

    VD:

    function globalVar(var, value)
      if fibaro:getGlobalValue(var) == nil then
        local HC2 = Net.FHttp("127.0.0.1",11111);
        local response ,status, err = HC2:POST('/api/globalVariables','{"name":"'..var..'","value":"'..value..'"}');
        if (tonumber(status) == 200 and tonumber(err)==0) then
          log("Global variable "..var.." created");
        end
      end
    end

     

    Scene:

    function globalVar(var, value)
      local http = net.HTTPClient()
      http:request("http://127.0.0.1:11111/api/globalVariables", {
          options = {
            method = 'POST',
            headers = {},
            data = '{"name":"'..var..'","value":"'..value..'"}',
            timeout = 2000
          },
          success = function(status)
            fibaro:debug(status.status)
            if status.status ~= 200 and status.status ~= 201 then
              print("failed");
            end
            print(status.data);
          end,
          error = function(err)
            print('[ERROR] ' .. err)
          end
        })
    end

    • Like 1

    Share this post


    Link to post
    Share on other sites
    • 0
  • Inquirer
  • 6 minutes ago, boerremk said:

    VD:

    function globalVar(var, value)
      if fibaro:getGlobalValue(var) == nil then
        local HC2 = Net.FHttp("127.0.0.1",11111);
        local response ,status, err = HC2:POST('/api/globalVariables','{"name":"'..var..'","value":"'..value..'"}');
        if (tonumber(status) == 200 and tonumber(err)==0) then
          log("Global variable "..var.." created");
        end
      end
    end

     

    Scene:

    function globalVar(var, value)
      local http = net.HTTPClient()
      http:request("http://127.0.0.1:11111/api/globalVariables", {
          options = {
            method = 'POST',
            headers = {},
            data = '{"name":"'..var..'","value":"'..value..'"}',
            timeout = 2000
          },
          success = function(status)
            fibaro:debug(status.status)
            if status.status ~= 200 and status.status ~= 201 then
              print("failed");
            end
            print(status.data);
          end,
          error = function(err)
            print('[ERROR] ' .. err)
          end
        })
    end

    Thanks @boerremk

     

    Can I also use this to create a predefined variable ? 

     

     

    Share this post


    Link to post
    Share on other sites
    • 0

    Thanks to @petergebruers I finally learned how to make both types of global variables. Here is solution for VD and SCENE. First is to declare global variable. This is how to declare numeric variable (upper part of the variable panel):

    Please login or register to see this code.

    If you want to add predefined global variable (lover part of the variable panel) but give user ability to change values to language preferred then you do this code:

    Please login or register to see this code.

    At last if you want to make global variable with table then you declare global variable this way:

    Please login or register to see this code.

    You use this code then to check if global variable exists and if not to add it:

    Please login or register to see this code.

    Then you call above function with this code in VD main loop:

    Please login or register to see this code.

    or with this code in scene:

    Please login or register to see this code.

     

    Edited by Sankotronic
    Code can be used in VD and Scene
    • Like 2

    Share this post


    Link to post
    Share on other sites
    • 0

    Hello, i don't get it!

     

    i use this part " At last if you want to make global variable with table then you declare global variable this way: " in combination with this part " You use this code then to check if global variable exists and if not to add it: " in a scene but it dosn't work.

    i get this exeption "[DEBUG] 13:51:38: [1;31m2018-09-14 13:51:38.761675 [ fatal] Unknown exception: /opt/fibaro/scenes/189.lua:8: '}' expected (to close '{' at line 4) near 'isEnum' "

     

    Can somebody help me? i want to create "predefined variables" about a scene - e.g. varName=Vacation; value1=Yes; value2=No

     

     

    Thanks

    Cannon

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    ×