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

update global variable which is set to enumValues


Question

Posted

I have a home table which contains a list of speakers
i thank have a variable called sonosPlayer, with a enumValues of the speakers
what I am trying to do is on save of the hometable to remove all the enums which are not in my list of speakers and add the speakers which are in it.

i tired this, but it does not work, can anyone give me some help on how to get ti working?

 

Many thanks for any help provided.
 

--Update Speaker Variable with list of speakers
local gvName = "sonosPlayer"
--local gv = api.get("/globalVariables/" .. gvName)
local speakersList = "" -- Initialize an empty string to store the speakers' IP addresses
local jT = json.decode(fibaro.getGlobalVariable("jHomeTable"))
local counter = 0
-- Loop through the "jT" variable to find the list of speakers' IP addresses
for k, v in pairs(jT) do
    if k == "Speakers" then
        
        for k2, v2 in pairs(v) do
            counter = counter + 1
            -- k2 is the name of the speaker
            -- v2 is the IP address of the speaker
            if counter == 1 then
                speakersList = v2
            else
                speakersList = speakersList .. "," .. v2
            end
        end
    end
end
 
-- Update the "sonosPlayer" global variable with the list of speakers' IP addresses
print("Speaker list "..speakersList)
api.put("/globalVariables/sonosPlayer", { value = speakersList })
 
--output the values of sonosPlayer Variable for debugging
local sonosPlayerVariable = api.get("/globalVariables/" .. gvName)
print(json.encode(sonosPlayerVariable))

2 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Posted

    i have tried this too

    --Update Speaker Variable with list of speakers
    local gvName = "sonosPlayer"
    --local gv = api.get("/globalVariables/" .. gvName)
    local speakersList = "" -- Initialize an empty string to store the speakers' IP addresses
    local jT = json.decode(fibaro.getGlobalVariable("jHomeTable"))
    local counter = 0
    -- Loop through the "jT" variable to find the list of speakers' IP addresses
    for k, v in pairs(jT) do
        if k == "Speakers" then
            for k2, v2 in pairs(v) do
                counter = counter + 1
                -- k2 is the name of the speaker
                -- v2 is the IP address of the speaker
                if counter == 1 then
                    speakersList = v2
                else
                    speakersList = speakersList .. "," .. v2
                end
            end
        end
    end
     
    -- Update the "sonosPlayer" global variable with the list of speakers' IP addresses
    print("Speaker list "..speakersList)
    api.put("/globalVariables/sonosPlayer",
        {
            name="sonosPlayer",
            value=""-- optional
            isEnum=true,
            enumValues=
                {
                    speakersList 
                }
        })
     
    --output the values of sonosPlayer Variable for debugging
    local sonosPlayerVariable = api.get("/globalVariables/" .. gvName)
    print(json.encode(sonosPlayerVariable))
    • 0
    Posted (edited)
    • So you have a Global Variable jHomeTable and a Global Variable SonosPlayer?
    • In the jHomeTable you have a property (list) for your Sonos speakers?
    • You want to read the list in jHomeTable and match it with SonosPlayer? So replace the property (list) with SonosPlayer?

     

    If I understand it correctly please post an example of the SonosPlayer value and jHomeTable value (please strip everything except the SonosPlayer property, to help you I need the structure.

    Edited by Joep

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