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

Understanding Virtual Devices (Making Holiday Scene)


Question

Posted (edited)

Hi,

 

I'm trying to understand how virtual devices work. I understand that I can make buttons and when they are pressed you can send a specific code. I have 2 questions for which I cannot find the answer

 

1) I want an on/off button (just like lights have) but when I add 2 buttons they do not show up under devices. Only when I click on the virtual device they appear. How do I make them look the same like lighting devices?

 

2) Is it also possible to let the button adjust a local variable in the main loop or can it only adjust global variables?

Edited by eureka

12 answers to this question

Recommended Posts

  • 0
Posted
6 hours ago, eureka said:

) I want an on/off button (just like lights have) but when I add 2 buttons they do not show up under devices. Only when I click on the virtual device they appear. How do I make them look the same like lighting devices?

you can't use VD as usual (hardware) devices. but you can use buttons, labels state in your scenes.

 

6 hours ago, eureka said:

Is it also possible to let the button adjust a local variable in the main loop or can it only adjust global variables?

you can't use global vars in VD.

 

here is simple VD device for light control

 

main loop

Please login or register to see this code.

 

as you can see I am query device state and show current icon

 

  • 0
  • Inquirer
  • Posted

    Ok, maybe it's good to give a bit of background in what I'm trying to achieve: 

     

    I want to make a Virtual Device in which I can toggle a Holiday Mode on and off. Therefore I would like to have an on/off button with my VD. I think it's strange that the buttons don't show up on the main screen and only show when you go into the VD. What is the purpose of those buttons if you can't click them directly?

     

    I found that I can use Global Variables for the buttons. So I put the following Code in at LUA buttons

     

    Please login or register to see this code.

    where 1 will be '0' for the Off button. This works fine now. However, my code in the general lua area is still having issues which I do not understand.

    I'm getting the following error: 'Attempt to index global 'net' (a Nil value)

     

     

    the error is given on:

     

    Please login or register to see this code.

     

    In the general Lua area I have the following code.

     

    Please login or register to see this code.

     

    • 0
    Posted (edited)

    Sir. do you know that http methods different for VD and scenes? did you see my post? did you see my example? did you see how I call raspberry pi http php file?

    Edited by 10der
    • 0
    Posted (edited)

    Why don't you just use @Sankotronic VSL VD for this. It does exactly this

     

    And yes, your not the only one that has complained about the fact, getting to VD on the app or through the web UI is a pain in the ****.  I have requested a million times that they add a better way to get to VD's...

    Edited by matt1981
    edit
    • 0
  • Inquirer
  • Posted (edited)
    On 25-2-2018 at 9:05 PM, 10der said:

    Sir. do you know that http methods different for VD and scenes? did you see my post? did you see my example? did you see how I call raspberry pi http php file?

     

    Yes, saw it, I just don't understand the way it works. Is there maybe a tutorial on how to use the API in VD? Or what does what in the code you mentioned?

     

    I tried the Get method for a bit to see whether I can pull data from the API and got the following code:

     

    Please login or register to see this code.

    However, I get an error on fibaro:debug(jsonStr("status")):

     

    Please login or register to see this code.

     

    @matt1981 not using the VSL because it is to extensive for what I want to achieve. Want to keep it simple.

     

    Edited by eureka
    • 0
    Posted

    >>>However, I get an error on fibaro:debug(jsonStr("status")):

     

    cuz YOUR response, not a JSON :) or YOUR json response doesn't have a status field. 

     

     

    • 0
  • Inquirer
  • Posted

    I don't get it.

     

    I went to HOMECENTER-IP-ADDRESS/docs/#!/panels/getHeatings. This is where you can find data on the API and the links. It also states the respons is in json (see screenshot).

     

    Please login or register to see this attachment.

    • 0
    Posted

    Please login or register to see this code.

     

    Please login or register to see this image.

    • 0
  • Inquirer
  • Posted (edited)

    @10der I tried the following and it works partly. i put it in the main loop. First time the script runs, it works. So if HolidayMode is off it will put it on. However, in the second loop it gives an error and won't turn holiday mode off. 

     

    Error is as follows:

     

    Please login or register to see this code.

     

    Do you need to close the connection at the end of the code or something?

     

    Below I put the full code:

     

     

    Please login or register to see this code.

     

    Edited by eureka
    • 0
    Posted

     

     

    This is unexpected behaviour, but actually it is quite normal.

     

    Don't do this:

     

    Please login or register to see this code.

    Because... "json" is a predefined table, and it holds a field called 'encode'.

     

    So the first time you run this, you do a decode of jsonTable. But after that assignment, json is no longer that table, because you assigned another table and field "encode" no longer exists.

     

    Lua allows you to do that, it is part of the language, and you do not get any warning.

     

    You might think "fibaro" and "json" are special namespaces and are protected... but this is not the case. They are simply... tables...

     

    Try this instead:

     

    Please login or register to see this code.

    This can happen in scenes as well. It is a "Lua" thing.

    • 0
  • Inquirer
  • Posted

    awesome! I've got it working! Thanks @petergebruers and @10der for the help.

     

    For everyone interest. I made the following script, it's not fully optimal yet but it works perfectly. The script gives you the opportunity to set multiple rooms to holiday mode. Furthermore it makes a schedule for your lighting so that even if you are away, lights will go on/off as if you were at home.

     

    This script requires one global variable called: "HolidayMode"

     

    Make a virtual device with 2 buttons: Code for the buttons:

     

    ON Button:

    Please login or register to see this code.

     

    OFF Button:

    Please login or register to see this code.

     

    Please login or register to see this code.

     

    • 0
    Posted

    Sir. I am still recommend change 

     

    to 

    Please login or register to see this code.

    and 

    Please login or register to see this code.

    or 

    Please login or register to see this code.

    on simple

    Please login or register to see this 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...