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


[TUTORIAL] Introduction to Lua tables (2nd edition)


AutoFrank

Recommended Posts

3 hours ago, jgab said:


Table.group.option4=1000

Thanks @jgab that was embarrassingly easy! I'd been messing about with table.insert for ages. Thanks this is going to make that first QA better!

 

I've also been trying to insert a new line in this nested table

 

Table={

al_1 = {cw=1, ww=10},

rgbw_1 = {r=38, g=13, b=1, w= 1}, 

sl_1={sl=10},}

 

To make it like this....

Table={

al_1 = {cw=1, ww=10},

rgbw_1 = {r=38, g=13, b=1, w= 1}, 

sl_1={sl=10},

newLine={new=*1},}

 

I'm pretty sure I need to use table.insert but I can't figure out the syntax. Again its probably something simple but I can't figure it out!

 

god Jul

 

Link to comment
Share on other sites

On 12/24/2022 at 9:53 AM, Rosavision said:

Thanks @jgab that was embarrassingly easy! I'd been messing about with table.insert for ages. Thanks this is going to make that first QA better!

 

I've also been trying to insert a new line in this nested table

 

Table={

al_1 = {cw=1, ww=10},

rgbw_1 = {r=38, g=13, b=1, w= 1}, 

sl_1={sl=10},}

 

To make it like this....

Table={

al_1 = {cw=1, ww=10},

rgbw_1 = {r=38, g=13, b=1, w= 1}, 

sl_1={sl=10},

newLine={new=*1},}

 

I'm pretty sure I need to use table.insert but I can't figure out the syntax. Again its probably something simple but I can't figure it out!

 

god Jul

 

 

Lua tables can be either arrays (with consecutive values) ex

Please login or register to see this code.

or of type key/value

Please login or register to see this code.

 

table.insert (t, pos, value) is only used when inserting a value in a Lua array. It inserts a value at a specific position moving all values, at that position and above, one position up.

Ex.

Please login or register to see this code.

will create 

Please login or register to see this code.

It's very common to use table.insert to add a value at the beginning of an array and move all other values a step up.

To add an element at the end of and array you can use

Please login or register to see this code.

 

If you have a table table with key/values, like in your example, table.insert does not work/apply.

Please login or register to see this code.

 

 

Edited by jgab
  • Like 1
Link to comment
Share on other sites

Hi @jgab Many thanks for the solution and the explanation, this really helped me. 

 

My first QA sliders to set CCT light brightness and ambience is now working :)

 

tack för din hjälp och ditt stöd

 

@irekk this might be helpful to you as well

Link to comment
Share on other sites

  • 1 month later...

can you also remove a id or number from a table

example.

 

if a door is open table.insert(doors,doorId) gives me table like [209]

this wil ad another door if this also open gives me table like [209,288] etc

but when the door is closed it has to be remove from the table.

How?

 

thx again

 

Found it

 

if fibaro.getRoomID(door)==rID then

   for k,v in ipairs(doorTable)do

    table.remove(doorTable,k)

  end

end

 

 

Edited by Kage
Link to comment
Share on other sites

It will remove all items in doorTable....?

 

In general, it can be good to extend the table functions with some "missing" functions

Please login or register to see this code.

 

Link to comment
Share on other sites

On 2/4/2023 at 7:31 AM, jgab said:

It will remove all items in doorTable....?

I hope Not!😁

 

No whem the roomID(door) is identical to roomID it wil remove from the table where k stands for the number in the table eq position to be remove. 

 

On 2/4/2023 at 7:31 AM, jgab said:

Please login or register to see this code.

i trying to understand you. if i'm correct this part looks for some data in table if not it returns obj eq table or........

 

On 2/4/2023 at 7:31 AM, jgab said:

Please login or register to see this code.

this part looks if it is a both the same and than if one of them is a table. if equal en not table then false. if one or both are table then if pos 1 of e2 is not nil or not equal to .......and then you lost me..........(v1,e2[k1])......

 

v1 is always diffrend then k1 or e2[k1] where k=key and v=value!?

 

 

Link to comment
Share on other sites

10 hours ago, Kage said:

I hope Not!😁

 

No whem the roomID(door) is identical to roomID it wil remove from the table where k stands for the number in the table eq position to be remove. 

Nope, this code

Please login or register to see this code.

will remove all items in doorTable as it loops through all items with pairs and removes them, one by one... You should have the test inside the loop if you only want to remove one.

 

 

10 hours ago, Kage said:

 

i trying to understand you. if i'm correct this part looks for some data in table if not it returns obj eq table or........

 

this part looks if it is a both the same and than if one of them is a table. if equal en not table then false. if one or both are table then if pos 1 of e2 is not nil or not equal to .......and then you lost me..........(v1,e2[k1])......

 

v1 is always diffrend then k1 or e2[k1] where k=key and v=value!?

 

 

The functions are what they are named.

The first copy any Lua table. The second checks if two Lua objects are the same, including Lua tables.

I think the equal function is the most efficient one can do in Lua - someone that has a better one?

Link to comment
Share on other sites

1 hour ago, jgab said:

Please login or register to see this code.

there is a missing part, this only yes it removes all. it also has to check if door is open

 

more like this

idR={234,456}
doorId=api.get("/devices?baseType=com.fibaro.doorWindowSensor&visible=true")

function doorValue(id)
    for i=1, #id do
        idD=tonumber(id[i])
        if _f.getValue(idD,'value') then return true end
    end
    return false
end
function deviceIdFilter(id)
    for _,i in ipairs(idR)do
        if id.roomID==tonumber(i)then return true end
    end
    return false
end
for i=1,#idR do
    rID=tonumber(idR[i])
    if deviceIdFilter(doorId) then
        if not doorValue(doorId) and light and next(doorTable)~=0 then
            for k,v in ipairs(doorTable)do
                if v==rID then table.remove(doorTable,k)end
            end
        end
    end
end

 

i know if there are more then one door in the same room it will deleted both. i think 🙄 all my rooms have one door one window one ceiling

 

i will differently try your codes if only i am a amateur :)

 

Edited by Kage
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

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