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

change global variable


Question

Posted

i have a global variable called sonosPlayer which stores the ip address of the sonos i want to play music to, the ip addresses are stored in the variable as a enum.

i have made a qa with a button which toggles through the different sonos ip addresses.

this is my function;

function QuickApp:ToggleMusicPlayer()

    local gv = api.get("/globalVariables/sonosPlayer")

    local MusicPlayer = fibaro.getGlobalVariable(self.gloVarNameMusicP)

    counter = 0

    for k,v in pairs(gv.enumValues) do counter = counter +1 end

    for k,v in pairs(gv.enumValues) do

        if MusicPlayer == v then

            if k < counter then

                newK = k + 1

                print(newK)

            else

                newK = 1

            end

          --  print (k,v)

        end

    end

    for k,v in pairs(gv.enumValues) do

        if k == newK then

            print(v)

            fibaro.setGlobalVariable(self.gloVarNameMusicP,v)  

        end

    end

    self:MusicPlayer()

end

 

while it works i am cycling 3 times through the table and i wanted to know if there was a more effcient way to change the ip address without having to cycle so many times through the table.

6 answers to this question

Recommended Posts

  • 0
Posted (edited)

Please login or register to see this code.

 

Edited by jgab
  • 0
  • Inquirer
  • Posted
    11 hours ago, jgab said:

    Please login or register to see this code.

     

    for i=1,5 do

     

    are you only allowing for 5 records?

    • 0
    Posted
    2 hours ago, Jay Ess said:

    for i=1,5 do

     

    are you only allowing for 5 records?

    No, it was just some test code and I didn't know how many values you had in your enum.

    The 5 should be more than the number of enum values so you can see that it wraps around correctly.

    You need to complement with code to trigger it and update the global.

    • 0
    Posted (edited)

    What I didn't understand is why you have 2 globals

    sonosPlayer and whatever is stored in self.gloVarNameMusicP.

    Why not just rotate the sonosPlayer enum?

     

    Edited by jgab
    • 0
  • Inquirer
  • Posted (edited)
    1 hour ago, jgab said:

    What I didn't understand is why you have 2 globals

    sonosPlayer and whatever is stored in self.gloVarNameMusicP.

    Why not just rotate the sonosPlayer enum?

     

    self.gloVarNameMusicP = "sonosPlayer"

     

    i am only using one variable, in my code I might be calling it twice, need to check what i have done, but often i will put in extra code so that i can debug and then trim it down or start doing it one way and then change it a different way and leave bits of code ... until i get to clean it up properly.

     

    this is my code to toggle the sonos player, I still want to clean it up so that I can also stored as an enum in the variable all the speakers which i will have in the hometable, but that is waiting for me to re-order my hometable slightly to take in to account the different speakers as its own sub table. BTW for my home table I have manually built it and manually change and adjust them as prefer to building the hometable from code.

     

    I have looked at your code above to simplify it and need to copy it across to make my function neater and simpler, the only problem i had was that i didnt understand what you were doing LOL

     

    function QuickApp:MusicPlayer()

        self.gloVarNameMusicP = "sonosPlayer"

        local MusicPlayer = fibaro.getGlobalVariable(self.gloVarNameMusicP)

        local jT = json.decode((fibaro.getGlobalVariable("jHomeTable")))

        for k,v in pairs(jT) do

            if k == "Other" then

                for k2,v2 in pairs(v) do

                    --print (MusicPlayer)

                    if v2 == MusicPlayer then

                        MusicPlayer = k2

                        --print (k2)

                    end

                end

            end

        end

        self:updateView("lblMusicPlayer", "text", "Sonos Player is Set to "..MusicPlayer)  

    end

    function QuickApp:ToggleMusicPlayer()

        local gv = api.get("/globalVariables/sonosPlayer")

        local MusicPlayer = fibaro.getGlobalVariable(self.gloVarNameMusicP)

        counter = 0

        for k,v in pairs(gv.enumValues) do counter = counter +1 end

        for k,v in pairs(gv.enumValues) do

            if MusicPlayer == v then

                if k < counter then

                    newK = k + 1

                else

                    newK = 1

                end

              --  print (k,v)

            end

        end

        for k,v in pairs(gv.enumValues) do

            if k == newK then

                print(v)

                fibaro.setGlobalVariable(self.gloVarNameMusicP,v)  

            end

        end

        self:MusicPlayer()

    end

    Edited by Jay Ess
    • 0
  • Inquirer
  • Posted
    1 hour ago, jgab said:

    No, it was just some test code and I didn't know how many values you had in your enum.

    The 5 should be more than the number of enum values so you can see that it wraps around correctly.

    You need to complement with code to trigger it and update the global.

    if i dont know how many records might be there can i put x or something like that?

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