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

Filtering Devices by 'Battery-Operated'?


Question

Posted (edited)

Hi,

 

Using the new methods discovered in this thread -> 

Please login or register to see this link.

, is it possible to filter devices by 'battery-operated'?

 

I am not sure if there is any documentation on the 'filter' command. Will the example below work?

Please login or register to see this code.

 

Edited by chaicka

15 answers to this question

Recommended Posts

  • 1
  • Inquirer
  • Posted

    Figured out the right syntax to achieve this.

     

    Please login or register to see this code.

     

    • 0
    Posted
    On 11/29/2016 at 4:19 PM, chaicka said:

    Figured out the right syntax to achieve this.

     

    Please login or register to see this code.

     

     

    Hi @chaicka,

     

    If you want to filter out only master of the battery operated devices so to avoid getting master and all slaves of the same device then you can filter like this:

    Please login or register to see this code.

    It is perfect filter for code that checks battery status of the devices. 

    • Like 1
    • 0
  • Inquirer
  • Posted
    On 19/09/2017 at 8:25 PM, Sankotronic said:

     

    Hi @chaicka,

     

    If you want to filter out only master of the battery operated devices so to avoid getting master and all slaves of the same device then you can filter like this:

    Please login or register to see this code.

    It is perfect filter for code that checks battery status of the devices. 

     

    Thanks for circling back and share code to streamline the filter. Looks like this code works perfect without unnecessary overhead and best if the hidden master has device name that follows some form of naming convention for easy identification. I happened to practice just that. ;)

    • 0
    Posted

    Hi! Interesting topic.

     

    Can I - errrm - add a few things? I knew you guys want to know the gritty details...

    • I also use parentId = 1 to filter, but technically it is incorrect. Your first controller will have this ID. But you can have multiple controllers and they will have another id. I bet it works for 99.9% of all installations. But someone will find out about this important detail.
    • The call is not very cheep. You may think it simply asks the IDs of devices that meet a certain criterium. That is not how it works. It asks all information, of all devices that meet the filter. Then it throws away the details and anly keeps the IDs. I'd say "premature optimisation is the root of all evil" but if you know you need other information and you are going to loop over many devices anyway, you might just as well pull in all data and filter it in your scene.
    • I discuss this and the somewhat related callGroupAction (recommended !) here: 

      Please login or register to see this link.

    • If you want to write a script that tells you "device X in room Y" you do not want the master. I recommend to find "the first non-hidden slave of a master" instead. Not many people seem to give useful names to the masters, so they won't understand what device "X" is and room "Y" is probably not defined either. But they'll recognise the first slave, if it is not hidden, and the room it is in...
    • 0
    Posted

    Hi @petergebruers,

     

    I agree with you on all points. This above was just an experiment for me. In scenes that I will share use of the above solution is out of the question for the reason you mentioned. But that accounts for callGroupAction while getDevicesId will always give you only list of the devices ID and nothing else. If you need any other information on the filtered devices you still need to use REST API to get it.

     

    At the beginning I was getting list of all devices but that takes time since I have more than 500 of them! But if I use getDevicesId with filter and then get other info using api.get then it works much faster. I need this so that my code can find relevant devices on its own and not requiring users to add them as a list in the code.

     

    BTW bellow code does NOT work on my system HC2 v4.140 in either scene or VD:

    Please login or register to see this code.

    But this code does work in scenes and VD:

    Please login or register to see this code.

    Any thoughts?

    • 0
    Posted

    @Sankotronic callGroupAction and getDevicesId have completely different filter syntax. If your filter table contains a field "filters", like in your first example, it is for callGroupAction only.

    • 0
    Posted (edited)

    Hi @petergebruers and thank you!

     

    Yes I know that syntax must be different but first code in my previous post is copied from example for getDevicesId from 

    Please login or register to see this link.

    . Also this rule:

    Please login or register to see this code.

    implies that you can use more than one value for the given filter thanks to those square brackets, so this code should work:

    Please login or register to see this code.

    but it DOES NOT work. You can use only one value for the given filter and value does not like any brackets so only this works for me:

    Please login or register to see this code.

    So is it only me crazy here or there is something wrong with my Angelina? I don't think so! 

     

    I think that Fibaro constantly forgetting to update documentation just making our lives miserably difficult. I'm sure that you know how tedious and time consuming is "try and error" way of searching for the right solutions!

     

    Hello @T.Konopka, @I.Srodka

     

    Any comments on this revelation?

     

    Question

    At the moment I use this code to get all doors and windows of my house:

    Please login or register to see this code.

    and this is because when I try this way:

    Please login or register to see this code.

    it just doesn't work. So what I'm doing wrong?

     

     

     

    Edited by Sankotronic
    • 0
    Posted (edited)

    @Sankotronic 

     

    Wooooooooooot?

     

    I am 99,9% certain that documentation for getDevicesId is incorrect, that is the only logical explanation. All my filters still work, those filters do not contain the field "filters". If I try the example from the developer sit (and pick a roomId and device type that exist on my network) I get no devices, as expected.

     

    Maybe Fibaro is going to change the API? I am 99,* certain firmware 4.140 is still using the "old" (less flexible) syntax...

     

    BTW the old syntax supports multiple values for "properties" only and "type" is not a property. I think you cannot filter on 2 different types, but I am not sure. I've tried a few alternative syntaxes but they all failed.

     

    So I agree, mergeTables is a good workaround.

     

    Maybe one last issue... Some users might have missed the fact that the data you copy/pasted at the start of your post is not a table. It is raw json. In lua this would be:

     

    local data={filter="type",value={"com.fibaro.multilevelSwitch"}}

     

    So no square brackets. Not that it matters, it that does not work for getDevicesId anyway. Here is that part from your post:

     

    1 hour ago, Sankotronic said:

    Yes I know that syntax must be different but first code in my previous post is copied from example for getDevicesId from 

    Please login or register to see this link.

    . Also this rule:

    Please login or register to see this code.

    implies that you can use more than one value for the given filter thanks to those square brackets, so this code should work:

     

    For sake of completeness, I think the api understands a query like this:

    Please login or register to see this code.

    But I think it means "all devices that satisfy *both* conditions" so it always returns an empty result... What you want is "or" and I think the old syntax could not do that. But I am not 100% sure, I never dug very deep into that syntax...

     

    EDIT: getdevicesId is function that takes a table, and turns the arguments into a url, like the one I posted above.

    Edited by petergebruers
    • 0
    Posted
    On 20.09.2017 at 6:14 PM, petergebruers said:
    • If you want to write a script that tells you "device X in room Y" you do not want the master. I recommend to find "the first non-hidden slave of a master" instead. Not many people seem to give useful names to the masters, so they won't understand what device "X" is and room "Y" is probably not defined either. But they'll recognise the first slave, if it is not hidden, and the room it is in...

     

     

    This is my script to filter battery devices:

    Please login or register to see this code.

     

    • 0
    Posted

    Hi @szmyk,

     

    That is interesting piece of code. Thank you for sharing!

     

    • 0
    Posted

    @szmyk thanks for sharing. We seem to have similar ideas. You use "visible = true" to filter out both the master devices and the hidden devices. Okidoki!

     

    I am going to nitpick, but I happen to know an interesting failure mode of your script. I've toyed a lot with the device API so I know a few arcane issues. I expect less than 0.1% of all users to detect this. Not a lot, but you are both publishing advanced code and lots of users are going to use your code, so I might as well tell you what I know.  Your code assumes that all devices are grouped by "nodeId" to show only the first visible, enabled device. But if someone a) makes a master visible b) the user has done a soft reconfigure on the device the devices are no longer grouped by node ID. An example based on my Z-Wave network:

     

    Please login or register to see this code.

    So 229 is a master, and 742 is a slave

     

    So If I print out the results of your filter by using this code:

    Please login or register to see this code.

    I get:

     

    Please login or register to see this code.

    I cannot judge if this is a problem worth solving. It depends on your application.

     

    I do not see a quick fix: you cannot express "parentId not 1" as a filter and "parentId" does not exist as a property which you can retrieve by calling getValue.

     

    You could sort the table, but I do not recommend this solution because it is probably too resource hungry. Not the sorting itself is expensive, per se, but retrieving the data. I should really test this but I think it less expensive to retrieve the data by calling the api directly by calling:

     

    /api/devices?interface=battery&visible=true&enabled=true

     

    The result will contain any property, parameters but not associations, so you probably do not need to call the api again to retrieve data, everything you need is already available.

     

    Are you... following me...?

    • 0
    Posted

    Thanks @petergebruers for good advices.

     

    My algorithm was developed based on observations of my system. There is no documentation about nodeId but filtering works good with my system.
    Can you write nodeId values of your two devices: 229, 742?

     

    I agree with you that retrieving the data by calling the api directly will be more efficient. I'll try it.

    On the other hand my script runs only once during HC2 startup and then values are stored in local variable. So I don't care about performance.

    • 0
    Posted

    @szmyk I understand!

     

    I had a quick go at the "sort" version. Maybe tomorrow I'll try to make an api version. And I'll explain why I mentioned the "first non hidden slave" without showing you actual code!

     

    The nodeId of both devices is 74.

     

    I have not spent much time/thought on this "sort" solution, so maybe I missed some obvious optimisation. I made it to demonstrate that grouping the Node IDs fixes the problem.

     

    Please login or register to see this code.

     

    • 0
    Posted

    Hi @petergebruers,

     

    Experience on the work! I haven't played much with getDevicesId and callGroupAction and now you saved me some time!  Thank you for explanation and code.

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