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 - Using a Hometable to store device and scene ID's


AutoFrank

Recommended Posts

Guest andyhud
1 hour ago, Sankotronic said:

 

Hi @andyhud ,

 

  1. Scene should be set to "Automatic"
  2. Default setting for instances is OK
  3. Table should be generated only if some changes where made with devices (added, deleted or reconfigured), best time is after reboot.
  4. That is possible to achieve, but why you need that?

 

Thanks for your reply @Sankotronic good to know.

 

RE Point 3 - I guess it wont ever change by itself, but it was more a passing thought if it seemed a better idea.

Re Point 4 - My theory behind it was it could email you a nice format of the output so you can reference while doing LUA coding. I know @AutoFrank has suggested taking a copy manually to keeping separate (which I totally understand) but I just though about automating that process, thats all.

 

On a side note, I tried to manually add my scenes into the HomeTable by adding them manually as per below but in the Debug window it fails.

 

Please login or register to see this code.

I had assumed that if my scenes had spaces in their names but I wrapped it in quotes (") then it would be ok, but I dont think it is?

 

I get below.

 

any thoughts appreciated as ever

 

Thanks

 

Andy

 

 

 

 

 

Please login or register to see this attachment.

Edited by andyhud
Link to comment
Share on other sites

Here is example of HomeTable:

Please login or register to see this code.

 

Here is example how to use above table:

Please login or register to see this code.

If you use quotes in table then you get different results and I don't recommend it because it complicates programming. Also spaces can't be used in table as a part of the value, but you can use underscore for that like in above example.

 

Edited by Sankotronic
Link to comment
Share on other sites

It's possible to use spaces in table keys. It's a bit verbose but some people like the 'HomeTable[<key>]' style instead of the dot notation.

Please login or register to see this code.

 

Link to comment
Share on other sites

Guest andyhud

Thankyou @Sankotronic and @jgab

 

I ended up renaming everything in my HC2 to camelCase with no spaces and now it works ok and the scenese are being recorded in the hometable

 

I saw a post or two about auto populating the hometable with scenes, but I decided to stick to adding the scenes manually at the top. It works better for me

 

Cheers

 

Andy

 

Link to comment
Share on other sites

  • 1 month later...

Hi all

I created below group in the standard hometable lua scene:


LightsDelayedOffAtNight = {
    Tuinpad=208,Tuinlampen=207,MainTuinLampen=205, Tuinlampen_Schakelaa=210, MainTuinSchakelaar=209
    },

 

I noticed that the final table is created in a random order of the above devices. It's not created in the above order, neither alphabeltical or numericable.


This is the output of the json decode after creating the table:

"LightsDelayedOffAtNight": {
    "MainTuinLampen": 205,
    "Tuinpad": 208,
    "Tuinlampen": 207,
    "Tuinlampen_Schakelaa": 210,
    "MainTuinSchakelaar": 209
  }

 

What's the solution in case I want to loop through these devices in a specific order (not based upon ID neither alphabetical)?

Link to comment
Share on other sites

40 minutes ago, wpeeters said:

What's the solution in case I want to loop through these devices in a specific order (not based upon ID neither alphabetical)?

The order of a key/value table is always undefined - think of it as a unordered key/value set

Link to comment
Share on other sites

@jgab, ok I see.
Isn't there a way to add an additional field (integer) in order to sort while or after decoding?

 

Link to comment
Share on other sites

6 minutes ago, wpeeters said:

@jgab, ok I see.
Isn't there a way to add an additional field (integer) in order to sort while or after decoding?

 

 

If you want to treat those devices differently per device, I should not put them in the same group.

 

Maybe you can share some details what you want to do with the devices grouped here?

 

 

Link to comment
Share on other sites

@jgab,

Ok, here comes the background.
For my garden lights i use 3 fibaro relais each connected to a 'real' relais (I don't know how say that in english - it's a relais which is handling the load of the lights while the fibaro relais is only triggering the relais) and 2 switches.
Already since the beginning, I have very strange electrical behavior if I switch the lights off using the switch (all 3 circuits the same time), the lights dare to switch on again. Nobody could give me a solution already.
I think @petergebruers ever mentioned a hint but I don't remember.

Nevertheless, it's already working for years like this. In order to solve this, I added a double check in the lua scene and check for the load. If the wattage is still < 0 after switching off, I switch them off again (after some delay).

Now I want to rewrite the scene and acutally noticed that while switching off the lights, one by one instead of turning off the switch that it is working better.
But only if done in a specific order. That's the reason for my question.

But I think I was able to find a solution that will work for me:

 

--Tuinpad=208,Tuinlampen=207,MainTuinLampen=205, Tuinlampen_Schakelaa=210, MainTuinSchakelaar=209
      LightsDelayedOffAtNight = {
    [1]=208,[2]=207,[3]=205, [4]=210, [5]=209
    },


With ipairs, the result will always be in the same order. If created on the hometable, I can just document which ID is the equivalent for which device and I still have the advantage to maintain those in only 1 scene (hometable).
So, I can live with this workaround.

 

Edit : this is actually how I want to loop through it:

function TurnOffLightsDelayed() 
      info("white", "--- Start looping through devices to turn off with delay in between ...")
    for k,i in ipairs(jT.LightsDelayedOffAtNight) do -- loop through all devices in the section of the HomeTable.
          log("grey", k)     
            if (( tonumber(fibaro:getValue(i, "value")) > 0 )) then -- if the device is on then ....
              log("orange", "The device "..k.." is on, start switching it off.")
            fibaro:call(i, "turnOff"); -- turn the device off
              fibaro:sleep(1500)
              end
      end -- for k,i in pairs(jT.LightsOffAtNight)
end -- function TurnOffLightsDelayed

Edited by wpeeters
Link to comment
Share on other sites

17 minutes ago, wpeeters said:

@jgab,

Ok, here comes the background.
For my garden lights i use 3 fibaro relais each connected to a 'real' relais (I don't know how say that in english - it's a relais which is handling the load of the lights while the fibaro relais is only triggering the relais) and 2 switches.
Already since the beginning, I have very strange electrical behavior if I switch the lights off using the switch (all 3 circuits the same time), the lights dare to switch on again. Nobody could give me a solution already.
I think @petergebruers ever mentioned a hint but I don't remember.

Nevertheless, it's already working for years like this. In order to solve this, I added a double check in the lua scene and check for the load. If the wattage is still < 0 after switching off, I switch them off again (after some delay).

Now I want to rewrite the scene and acutally noticed that while switching off the lights, one by one instead of turning off the switch that it is working better.
But only if done in a specific order. That's the reason for my question.

But I think I was able to find a solution that will work for me:

 

--Tuinpad=208,Tuinlampen=207,MainTuinLampen=205, Tuinlampen_Schakelaa=210, MainTuinSchakelaar=209
      LightsDelayedOffAtNight = {
    [1]=208,[2]=207,[3]=205, [4]=210, [5]=209
    },


With ipairs, the result will always be in the same order. If created on the hometable, I can just document which ID is the equivalent for which device and I still have the advantage to maintain those in only 1 scene (hometable).
So, I can live with this workaround.

Yes, that's probably the easiest.

Btw,   {[1]=208,[2]=207,[3]=205, [4]=210, [5]=209} is the same as {208,207,205,210,209}

 

..or alternatively as below, which is a bit easier to read if you want to change the order

Please login or register to see this code.

 

Edited by jgab
Link to comment
Share on other sites

The issue with your solution is that when the device id changes, the implementation breaks. I understand the idea of the hometable is based to get rid of dealing with device ids.

 

Why is it necessary to have a table to cycle through? Another suggestion is to call out the device names explicitly in the scene in the correct order, like

 

Please login or register to see this code.

In the above example I assumed jT is the home table. In absence of a room name I just used "Room".

Link to comment
Share on other sites

@jgab , ok thanks. I'll see what's best to use.

@jayrock, You never can't get rid of the device id's. I prefer to create those kind of groups of devices in hometable so I can manage them one place.
In case I add one additional device, I only need to change the hometable scene.

 

Link to comment
Share on other sites

  • 2 months later...

Hey All... just a quick question.. is it true that I can't have scene names with spaces in them? Just tried to implement the scene to build HomeTable and get error

 

[DEBUG] 14:08:41: 2019-03-21 14:08:41.530312 [ fatal] Unknown exception: /opt/fibaro/scenes/130.lua:12: unexpected symbol near char(239)

 

What do I need to do to resolve this?? Go and rename all the scenes with _ to replace the Spaces??

Link to comment
Share on other sites

Many posts on HomeTables

 

If you want spaces and foreign language alphabets (UTF-8) you need to use "index" syntax for key name in tables.

Please login or register to see this code.

 

Link to comment
Share on other sites

28 minutes ago, jgab said:

Many posts on HomeTables

 

If you want spaces and foreign language alphabets (UTF-8) you need to use "index" syntax for key name in tables.

Please login or register to see this code.

 

Thanks very much...

Link to comment
Share on other sites

  • 1 month later...

Hi all,

 

I'm really struggling to implement something similar to this for another project. What I'd like to do collect a json result from a URL and store it in a home table.

This is the result as returned...

Please login or register to see this code.

How can I put this in to a home table so I can call out any single part of the result at a later date?

I've tried all sorts but I don't fully understand what I'm doing with json and the home table.

 

This result/table would be updated multiple times a day.

Edited by Desmo
Link to comment
Share on other sites

On 5/1/2019 at 9:42 PM, Desmo said:

Hi all,

 

I'm really struggling to implement something similar to this for another project. What I'd like to do collect a json result from a URL and store it in a home table.

This is the result as returned...

Please login or register to see this code.

How can I put this in to a home table so I can call out any single part of the result at a later date?

I've tried all sorts but I don't fully understand what I'm doing with json and the home table.

 

This result/table would be updated multiple times a day.

 

The concept of a "home table" is usually a table with names for you deviceIDs. In this case it's more about just storing the table in a fibaro global.

Please login or register to see this code.

Then when you need the value (in another scene or at another time) you do

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 4 weeks later...

There is this problem that we need to use device numbers for the header triggers and not the nice names we put in the HomeTable.

Here is a lazy way around it that is not the most efficient. It leverages the fact that we can have a wild-card '*' for deviceID in the header.

Please login or register to see this code.

There could also be ready-made 'valueTriggers' tables per scene stored in the HomeTable.

The disadvantage is that your scene is triggered too often and you do an unnecessary json.decode. 

The advantage is that you don't have to hard code the deviceIDs...

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

From what I have seen this code is great when you can hardcode the device name, but is there a solution when you have device IDs stored in tables and you want replace these id's with a string that represents the new device name that is stored in the home table?

 

Home Table entry for:  heating.tv_room = 123

 

e.g. 

local HT                                     = json.decode(fibaro:getGlobalValue("HomeTable"))

return_string_from_table       = "HT.heating.tv_room"

fibaro:debug(???)  <= how to return the value of 123?

Link to comment
Share on other sites

9 hours ago, amilanov said:

From what I have seen this code is great when you can hardcode the device name, but is there a solution when you have device IDs stored in tables and you want replace these id's with a string that represents the new device name that is stored in the home table?

 

Home Table entry for:  heating.tv_room = 123

 

e.g. 

local HT                                     = json.decode(fibaro:getGlobalValue("HomeTable"))

return_string_from_table       = "HT.heating.tv_room"

fibaro:debug(???)  <= how to return the value of 123?

 

Hmm, I'm not sure what you are after. If you want to return or print 123 you do

fibaro:debug(HT.heating.tv_room)

You typically never end up with the string "HT.heating.tv_room" as the hometable is always converted to a proper Lua table.

e.g. HT = {heating = {tv_room = 123}}

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