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

  • Topic Author
  • 1 minute ago, codowd said:

     

    I can confirm that the code in the first post will not work if the name of any device or room contains an apostrophe (') even though it encloses the apostrophe in quotes (").  You can see this if you just type the following line into the HC2 LUA editor:

     

    local jT = '{"Boy's Room"}'

     

    The syntax colour changes after the second apostrophe, indicating the quote finishes there, understandably.

     

    Anyone know how to overcome this in LUA?

     

    @codowd

    Thanks for confirming.

     

    About 9 months ago I went through my HC2 and worked to simplify all the names I used for scenes names, vd  names, device names as well  as any variables or constants I use within the code.  I got rid of all non-alphanumeric characters so at this stage I just use A...Z and 0...9. I also use camelcase (outsideLight) or an underscore (kitchen_Motion) to make some things easier to read. Since I did this I have eliminated all these types of issues. It was a bit of work but well worth it for me.

     

    Over the last week or so I also went through my scenes and vd ( ~ 60 in total) and removed all excess code (debug lines I used to troubleshoot issues and a event logger I added and generally cleaned up and simplified the code and have found that the trigger events happen much faster and the system is using far less resources.

     

     

     

    Link to comment
    Share on other sites

    @codowd i think you quoted the wrong person in your last post, since my question was not related to your issue (it was a general question)

    Link to comment
    Share on other sites

    @riemers

    Sorry I have edited my post.

     

    @AutoFrank

    Thanks, good to know - but that's not really an acceptable option for me. For other users (read WAF) it pays to have the names of rooms and devices in friendly format.  Plus I have no control over what my daughter calls her iPhone (also a Fibaro HC2 device).

     

    @All

    I have edited my previous post to include the code changes that *should* work.  However my iDevices still come through with the apostrophes intact... the "gsub" call doesn't seem to work. The rogue character is "’", in an iOs device name, which is an alternative apostrophe character of some kind.

     

    Strikes me, there is no control over the names of iOs devices from HC2, and all sorts of illegal characters could possible be added.  Anyway, for me an additional nested gsub now has the code working.

    Edited by codowd
    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, codowd said:

    @riemers

    Sorry I have edited my post.

     

    @AutoFrank

    Thanks, good to know - but that's not really an acceptable option for me. For other users (read WAF) it pays to have the names of rooms and devices in friendly format.  Plus I have no control over what my daughter calls her iPhone (also a Fibaro HC2 device).

     

    @All

    I have edited my previous post to include the code changes that *should* work.  However my iDevices still come through with the apostrophes intact... the "gsub" call doesn't seem to work.

     

    Hi @codowd

     

    Another option may be to use @ReneNL code to generate an initial table and then sanitize it to what will work. This way you don't need to change the room , etc names from what you want. You could then put this table into a scene and keep a copy in your source code control. The scene could %%autostart on a reboot. (btw, I don't reload the table on every reboot, I check 5 or 6 items in the table and this give me a sense if the table is corrupt and if it's okay I don't reload). I also don't use the Save button in the variables panel and this also helps keep the variables and their contents intact. I also find that having a copy in Notepad++ is that I can reference it when I am creating new scenes or vd is very convenient

     --This has been my approach and has worked well for me

     

    As I said earlier in the post ReneNL code is fantastic for generating the table for the very first pass but I'm not sure it the best approach to keep the table aligned for reboots. I think you could find it very onerous in trying to manage all this through smart naming/character substitution of everything in your HC2. In my case I also don't need all the data that is generated as many of my scenes and vd are triggered automatically by conditions and not manually (by either myself or other code segments)  and don't need to be in my HomeTable. The hometable provides a nice layer of abstraction and even though my primary goal for my system is close to full automation there are something I don't want automated or are not worth the effort.

     

    I know I'm not providing a solution and I'm not suggesting that you don't take this approach but it might be taking a moment and seeing if this will really reap the benefits you want.

    I can take a look this evening and see if I can solve the substitution issue but be warned :-) this is not my specialty...

     

    -f

     

     

     

     

     

    Link to comment
    Share on other sites

    Thanks for your reply. You are right in what you say.  I think the scene would need a function called (say) "TrimIllegals" which cotains the gsub calls and this is getting to be too much work.

     

    BUT what I wanted to use the code for was to make easier the task of (say) switching off all the lights downstairs, or linking motion sensors with lights in a given room.  The next step was obviously to loop through each room and make the associations, if that is possible.

     

    Otherwise I can see I will simply have to code every little thing in LUA - that is not why I bought a HC2!!  I am happy to LUA startup code but want to block scene the majority of my activated scenes.  If I want to code everything I'll use OpenHAB!  Much cheaper...

     

    Link to comment
    Share on other sites

  • Topic Author
  • 41 minutes ago, codowd said:

    BUT what I wanted to use the code for was to make easier the task of (say) switching off all the lights downstairs, or linking motion sensors with lights in a given room.  The next step was obviously to loop through each room and make the associations, if that is possible.

     

    Post 28 on this topic has some discussion on this

    Personally I created a VD that has multiple lighting sequences (All Lights Up off, All Lamps Up off, All Lights Down off, All Lamps Down Off, All Garage Lights off, All Outside Lights off, All Security Lights On,) I have a similar vd for music ( All Sonos Up Off, All Sonos down off) ), TTS announcements, AV equipment and some others

    Once I had my hometable sorted it took me about 5 mins to create each vd

     

    -- I then just use pressbutton on the appropriate vd  within the relevent scene/VD to execute the required sequence such as my good morning, good night, leaving home, kids to bed, etc. sequence

     

     

    Link to comment
    Share on other sites

  • Topic Author
  • On 1/18/2017 at 2:57 PM, riemers said:

    bw Going bit offtopic, but might i suggest you use docker for that. Some nas systems include docker too (synology) from the gui. Using docker is easy and simple to give to someone else too, work on Pi3 too. For node-sonos-api there is a docker image 

    Please login or register to see this link.

     saves you the hassle of installing all dependency's, assuming you have some knowledge with linux.

     

    Hi @riemers

    I hope you dont mind but I added your docker information above to the frst post of the sonos api thread at 

    Please login or register to see this link.

    Link to comment
    Share on other sites

    @codowd, next to the suggestion of AutoFrank two posts up, you also might want to check on my post 53, see below. Here I also added a changed script where the HomeTable structure is section-->devicetype-->deviceName-->deviceID. This allows you to e.g. select all lights in a section in one go (deviceIDs = jT.FirstFloor.dimmer).

    Best, Rene.

    Link to comment
    Share on other sites

    On ‎1‎/‎24‎/‎2017 at 10:34 AM, codowd said:

    @riemers

    Sorry I have edited my post.

     

    @AutoFrank

    Thanks, good to know - but that's not really an acceptable option for me. For other users (read WAF) it pays to have the names of rooms and devices in friendly format.  Plus I have no control over what my daughter calls her iPhone (also a Fibaro HC2 device).

     

    @All

    I have edited my previous post to include the code changes that *should* work.  However my iDevices still come through with the apostrophes intact... the "gsub" call doesn't seem to work. The rogue character is "’", in an iOs device name, which is an alternative apostrophe character of some kind.

     

    Strikes me, there is no control over the names of iOs devices from HC2, and all sorts of illegal characters could possible be added.  Anyway, for me an additional nested gsub now has the code working.

     @codowd, One other suggestion is to automatically change/remove all non-alphanumeric characters from the devicenames and roomnames in the HomeTable. That can easily be done by changing the line

     

    Please login or register to see this code.

    to

    Please login or register to see this code.

    Same for the line further down in the script for the rooms. Change

    Please login or register to see this code.

    to

    Please login or register to see this code.

    Best, Rene.

    Link to comment
    Share on other sites

    17 hours ago, ReneNL said:

     @codowd, One other suggestion is to automatically change/remove all non-alphanumeric characters from the devicenames and roomnames in the HomeTable. That can easily be done by changing the line

     

    Please login or register to see this code.

    to

    Please login or register to see this code.

    Same for the line further down in the script for the rooms. Change

    Please login or register to see this code.

    to

    Please login or register to see this code.

    Best, Rene.

    Thanks, that is exactly what is required!  It gives me valid json and I can read the jHomeTbale variables successfully (i.e log(iOS.MyiPad2) = 288 for example).

     

    Edited by codowd
    Link to comment
    Share on other sites

    Cool, good to hear that this worked for you! 

     

    @AutoFrank, we could add this line to the script, maybe out-comment it so people can select as needed.

     

    Rene.

    Link to comment
    Share on other sites

  • Topic Author
  • 21 minutes ago, ReneNL said:

    Cool, good to hear that this worked for you! 

     

    @AutoFrank, we could add this line to the script, maybe out-comment it so people can select as needed.

     

    Rene.

     

    All & @ReneNL

    script in first post updated include these two options to remove all non-alphanumeric characters from all devicename and roomname

    un-comment as required at lines 36 and 57 respectively.

     

    -f

     

     

     

    Link to comment
    Share on other sites

    Please login or register to see this code.

    Can anyone see what is wrong with the setglobal statement in this code?

     

    The json is correct and the log statement produces the right value (288).. But the global is still not created...

    Link to comment
    Share on other sites

    22 minutes ago, AutoFrank said:

     

    All & @ReneNL

    script in first post updated include these two options to remove all non-alphanumeric characters from all devicename and roomname

    un-comment as required at lines 36 and 57 respectively.

     

    -f

     

     

     

    I don't think the %W option deletes any additional characters except of course the problem ones, so it can probably be used instead of the %s+ option.

    Link to comment
    Share on other sites

  • Topic Author
  • 10 minutes ago, codowd said:

    Please login or register to see this code.

    Can anyone see what is wrong with the setglobal statement in this code?

     

    The json is correct and the log statement produces the right value (288).. But the global is still not created...

     

    HI @codowd

     

    There would appear to be a few small things mixed up

    Better to save first and then read back later

    Try this - I haven't tested it but it should work

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    49 minutes ago, AutoFrank said:

     

    HI @codowd

     

    There would appear to be a few small things mixed up

    Better to save first and then read back later

    Try this - I haven't tested it but it should work

     

    Please login or register to see this code.

     

     

    Yes I know I probably am being a klutz with this JSON stuff, but I don't understand how your code above works.  I assumed that what is stored in the global is a string representation of the data structure, which needs to be decoded (back to a data structure) on read and encoded back to a string on write.

     

    That seems to be the way the original code (in the first post) works.  jsonHome (and jT) are the data structures. jHomeTable is the string that's read and written. 

     

    But in the above code jsonHome refers to a string, not a JSON structure. But if a string then the log statement log(jsonHome.iOS.iPad2) wouldn't work (yet it does). Is LUA creating a data structure on the fly?

     

    Note: I cannot successfully setglobal using either my code or yours, so that is the reason I am playing around with all the options :/.

    Edited by codowd
    Link to comment
    Share on other sites

  • Topic Author
  • 21 minutes ago, codowd said:

     

    Yes I know I probably am being a klutz with this JSON stuff, but I don't understand how your code above works.  I assumed that what is stored in the global is a string representation of the data structure, which needs to be decoded (back to a data structure) on read and encoded back to a string on write.

     

    That seems to be the way the original code (in the first post) works.  jsonHome (and jT) are the data structures. jHomeTable is the string that's read and written. 

     

    But in the above code jsonHome refers to a string, not a JSON structure. But if a string then the log statement log(jsonHome.iOS.iPad2) wouldn't work (yet it does). Is LUA creating a data structure on the fly?

     

    Note: I cannot successfully setglobal using either my code or yours, so that is the reason I am playing around with all the options :/.

     

    Hi @codowd

     

    No worries and I'm still reasonable new to working with json as well. :-) and still get a lot wrong...

    You're correct in when you said that your code was similar to what I sent back. I just changed it to what I use as it's a bit easier for me to understand.

    The main issue I saw was that you weren't encoding the data before you saved it and at line 2 you were trying to decode something that was not encoded.

     

    I'll step through it with my understanding and apologies if this is not what you asked or if i have simplified it too much

     

    Please login or register to see this code.

    Hope this help a bit

    -f

     

     

     

     

     

     

     

     

     

     

     

     

    Link to comment
    Share on other sites

    Here's another version that automatically adds users and scenes too. All room/device/scene/etc names are converted to lowercase (except the mobile device name so it can be copy/pasted from the Access Control settings page on HC2).

    ID's are available using the following format:

    HC2["zwave devices"]["room name"]["device name"]

    HC2["virtual devices"]["room name"]["device name"]

    HC2["mobile devices"]["device name"]

    HC2["scenes"]["room name"]["scene name"]

    HC2["users"]["user name"]

    Please login or register to see this code.

     

    Please login or register to see this attachment.

    Edited by seiq
    Pretty database icon for scene ;)
    • Like 2
    Link to comment
    Share on other sites

    On 1/19/2017 at 9:24 PM, Momos said:

    Rene, I think this is exactly what i was looking for. Thank you.  Will try it and confirm in about 10 days when i get back home.

     

     

    Hi Again Rene,

     

    Just wanted to share the feedback. I tweaked it a little bit and now it does all:) adds devices per rooms and per sections/devices type like i wanted. I added also a check at the end to verify that it works properly.

     

    Thank you again :)

     

    Please login or register to see this code.

     

    and the debug obtained through adding the same devices but in 2 different ways in subtables - one subtable with all the devices of one kind, and another two subtables with all the devices of the same kind but per SECTION :) 

     

    Please login or register to see this attachment.

    Edited by Momos
    Link to comment
    Share on other sites

    Guest FredrikKarlsson

    Another small note which relate to international characters in device and scene names. 

    For sure, you can remove them all, but the names will then be a bit hard to remember. I prefer a solution like this for example:

    Please login or register to see this code.

     

    Of course the 'replacementTab' needs to be populated with what is appropriate for the language. The above example is what I use for Swedish.

    I prefer using "Rorelsetvattstugan" rather than "Rrelsetvttstugan" which I will never get right.

     

     

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