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

Simple xml readout


Question

Posted

Hi all, i basicly want some value's from an xml output into a virtual device. I've seen some examples here and there but most seem to be either to big or complex or it tells you to convert xml to json via another webserver (which i dont want)

In the end i get this back from the device:

Please login or register to see this code.

Which in xml terms is:

Please login or register to see this code.

The keyname can be different and/or have more lines like that. So i will only grab the lines i do have and say if "Toestelstatus" = "Klaar" then set other values to empty.

Does anybody have a simple example on how to get those things without getting to fancy?

Thanks,

Erik

Recommended Posts

  • 0
Posted

I don't think there is a simple way to do this directly on the hc2. To be honest, I do think the easiest way - even though you don't want to, would be to set up a webserver and convert to json using a premade script.

If you don't have a webserver, I am going to create a how to for setting up a low power (2-4 W) webserver for less than 50€ using a raspberry pi for my home automation interface. It is very simple to set up (it takes less than half an hour and will double as a NAS if you want) and can do almost everything for you that you could possibly need to interact with the HC2.

Alternatively, you could try your hand at building an XML-parser in Lua. But all that is going to do for you is create the same array as the json file will provide you ready made. There are some good examples out there, but personally (as I'm already running a webserver) I would go for the simpler xml->json->hc2 way.

  • 0
  • Inquirer
  • Posted

    My preference is without a webserver, since i would think it would be possible. But anyways, i do have one on my qnap. If i convert it to a php i get this back:

    {"information":{"key":[{"@attributes":{"name":"Toestel","value":"Wasautomaat"}},{"@attributes":{"name":"Toestelstatus","value":"Klaar"}}]},"actions":{}}

    How would i go about getting that into a virtual? Since the json is a bit weird (attributes inside a key which is inside information?)

    • 0
    Posted

    riemers, you can parse your response with LUA, you should use match:

    Example:

    Please login or register to see this code.

    it should work.

    • 0
  • Inquirer
  • Posted

    Ah yes, something like that is more easier in my eyes.

    Although some values might change and i can have some more key names, its always that same amount, so it doesn't need clever logic (as far as i can tell)

    • 0
    Posted

    Name "toestel" using json in lua:

    Information.key[1][email protected]

    For the toestelstatus you replace 1 with 2.

    The smart thing with using this, is that it is an array so you can retrieve all data without needing to know i.e. The name. It's much easier to integrate than having to search for each individual tags. Check out the official lua ressource for more info and examples on using arrays in lua.

    • 0
  • Inquirer
  • Posted

    So i got this now:

    Please login or register to see this code.

    But i get:

    [ERROR] 13:22:23: line :log (arg 2), expected 'string const &' got 'nil'

    Nil indicates that the GET is not working correctly.. if i try and display the response via log it doesnt error but does not give me something back in my window.

    Ok using the debug function i can see that the "response" is empty.. but if i get it via a normal browser, it does have information in it..

    • 0
    Posted

    riemers, try to use fibaro:debug instead of fibaro:log

    - Expected 'string const &' got 'nil' => toestel is null, match return nothing...

    replace the line local toestel = response:match(''); with local toestel = response:match('') or '';

    • 0
  • Inquirer
  • Posted

    Now i get back: [DEBUG] 13:34:31: Wasautomaat" />

    If i do a second one (so the toestelstatus) that does give me the exact command. Which is "Uit" at the moment.

    Then i should be able to set this value to the virtual device label. The id of this virtual is 173. So i would think it would be something like:

    fibaro:call(173, "setProperty", "ui.Status.value",toestelstatus);

    Where "ui.Status.value" -> Status is the name of the label. But underneath it , i also see a ID for it. Which would sound more logical. But the explanation underneath it does not correctly tell in my eyes.

    So getting closer!

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Update:

    Also tried it like this:

    fibaro:call(thisId, "setProperty", "ui.label_0_0.value",toestelstatus);

    fibaro:call(thisId, "setProperty", "ui.label_1_0.value","Drogen");

    But no values change.

    • 0
    Posted

    riemers, you should replace the ID with fibaro:getSelfId()

    try:

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    That works, its a shame the api is still in polish and not that great..

    What i dont get is, this is what it should match:

    Please login or register to see this code.

    But i notice that when i save it and go back to the lua script (refresh page) i see this again:

    Please login or register to see this code.

    It adds the behind it, which i did not ask for. Is that normal?

    • 0
    Posted
    t adds the behind it, which i did not ask for. Is that normal?
    Whow

    Please login or register to see this image.

    /emoticons/default_icon_eek.gif" alt=":shock:" /> you are right !

    Work with :

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    That doesn't change after i save, so thats good. But now it matches the last occurance that ends with />. I suppose you need a $ to say only match till end of the line or something like that.

    [ Added: 2014-03-19, 19:22 ]

    Played around looked on google, i came up with this:

    Please login or register to see this code.

    Dont know what the [[ and ]] does, but it makes it shorter. And instead of (.+) i use (.-) to only match the first occurance it encounters.

    [ Added: 2014-03-19, 21:10 ]

    End result:

    Please login or register to see this image.

    Also checked if it has "remaining time", if so change the icon to be a bit red (like in the screenshot)

    • 0
    Posted

    Good news

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    in [[ ]] you can pass all you want: ', ", \n, \t etc...

    • 0
  • Inquirer
  • Posted

    And there i thought i had it, it seems that if a device is busy, it creates more keypairs.. i read those out. But once its finished it only leaves 2, device name and status. So those other ones i have actually error on me since it cant find them and stops the lua.

    • 0
    Posted

    Wow, a dishwasher that outputs xml? Where can I get one of those?

    • 0
    Posted
    Wow, a dishwasher that outputs xml? Where can I get one of those?

    All (i think) Miele Futura appliances have Wifi. Also there are some Whirlpool-appliances with Wifi.

    I would be surprised if not all manufacturers have something like this in their product line up nowadays.

    • 0
  • Inquirer
  • Posted

    The older ones are based on powerlines communications. These are modules that needs to be added and can only be read out by a Miele Gateway device (i think around 600,- euro) each module for miele@home costs money too. I have several devices that can talk like this and show as xml on my gateway. Its costly and it doesn't really add that much sadly..

    But that said, its a nice extra little gadget. The future miele will be with zigbee modules though. Not wifi.

    [ Added: 2014-03-29, 19:59 ]

    This is the end result, sorry about the dutch, thats my language.

    Each device shows if its on or off, what program is running and how long it will take. Some devices can be finished while still on. So i just check if the remaining minutes is 0:00h.

    The cooking plate indicates what level of power is used per hub. So if i set this to level 12 on my cooking plate it actually shows it in my list too.

    Might be a bit overkill but it comes down to the simple rule "just because i could"

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    • 0
    Posted

    Could you post your virtual device?

    I'm struggling a bit with catching multiple similar xml entries with match. I can catch the first occurrence using your syntax but how do I get multiple ones?

    • 0
  • Inquirer
  • Posted

    Please login or register to see this code.

    I only think that if it cannot find a correct entry it will fail and stop 'polling' but so far its working for me and just now and then i still need to tweak it a bit (not all devices get the same output)

    But just do multiple lines for matches (in my case)

    • 0
    Posted

    My problem was that the multiple lines are identical (e.g. Server name="" repeated for each server)

    I found a solution.

    See function for getting active plex clients in this thread:

    Please login or register to see this link.

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