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

creating variables using lua or thru API


Question

Posted

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

9 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Posted
    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 ...

    • 0
    Posted

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

    8 minutes ago, AutoFrank said:

     

    bumping this ...

     

    • 0
    Posted

    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
    • 0
  • Inquirer
  • Posted
    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 ? 

     

     

    • 0
    Posted

    Check here

     

     

    • 0
  • Inquirer
  • Posted
    1 hour ago, sonnyboy said:

    Check here

     

     

     Thanks @sonnyboy

    • 0
    Posted (edited)

    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
    • 0
    Posted

    Many Thanks, a very useful post

    • 0
    Posted

    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

    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...