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 | if id ne xx or xx then do


Question

Posted

Hi

 

I'm trying to excude 2 id's from a bit of lua code.

could someone take a look at this and tell me if it's in the right direction?

 

local lightsOn = {}
local lightsf = api.get("/devices?interface=light")
for _, light in ipairs(lightsf) do
    if (light.visible == true and light.properties.state == truethen
--153,158
    if (light.id ~= 153 or light.id ~= 158 ) then -- this?
 
        table.insert(lightsOn, light.id)
    end
end
 
if (next(lightsOn) ~= nilthen
    fibaro.call(lightsOn, "turnOff")
end

2 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Posted

    so 2 things worked.. 

     

    1. using the right ID

    2. using AND as I knew I should.. but didn't

     

    this works and if you are curious as to what I want to do.. it's for the alarm. So when alarm is armed.. turn off all lights.. but not garden/outdoor lights. This is assuming it's supposed to be turned on of course.. I may add some time barrier to this.

     

    local lightsOn = {}
    local lightsf = api.get("/devices?interface=light")
    for _, light in ipairs(lightsf) do
        if (light.visible == true and light.properties.state == truethen
    --153,158
        if (light.id ~= 154 and light.id ~= 159 ) then -- this?
     
            table.insert(lightsOn, light.id)
            end
        end
    end
     
    if (next(lightsOn) ~= nilthen
        fibaro.call(lightsOn, "turnOff")
    end
    • 0
    Posted

    Some Lua coding tips.

    You can combine many selectors in /devices?...

    t[#t+1]=x is twice as fast as table.insert(t,x)

    Testing for exclusion for 2 devices is ok with 'and' - testing for more use a map

    #table gets the length for an indexed array. next() is the idiom for testing key-value tables emptiness or not. #t is also about twice as fast as next(t)

     

    Please login or register to see this code.

    You could also look at fibaro.callGroupAction or fibaro.getDevicesID

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