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] - Introduction to Virtual Devices


AutoFrank

Recommended Posts

Hi,

I know that when I started working with the HC2 I struggled with some of the simpler stuff. In some case it was down to a lack of understanding of the concepts rather than the code. 

I decided to put together this Virtual Devices 101 for new people starting out. I put some of this together to answer a few posts but decided to bring it into one post here to make it easier for newer users to find,

I am starting from the basics and if anybody has ideas on how to make it better please let me know

 

Some Virtual device basics

LABELS - enable the user to display a title and a value. A label with no value entered can act as a divider

BUTTONS - enable a user to execute an action, button can be manually pressed or through code. A VD can press one of its own buttons

MAIN LOOP -  Code here runs continuously in a loop - you can add fibaro:sleep code to slow down how fast the main loop code executes. Variables that are defined in the MAIN LOOP cannot be reused in the buttons. Seperate declarations are required

DEBUG - option on Main loop or Button to what is being echo'd back as it executes

 

The easiest way to understand virtual devices is to take apart a very simple one and understand the various parts

I've attached a very simple VD. This will run on any HC2 after you have created the following two variables (Panels, Variables)

Please login or register to see this attachment.

Use the Add button but NOT the save button that is part of the right side vertical menu (as this may corrupt some of your other variable values)

 

Go to  "Add New Device" and import the VD (Pump Control) attached to this post. 

Please login or register to see this attachment.

 

It's a very simple VD that interacts with the two variables you just created. (I don't expect the VD to be any use in real life but hopefully acts as a good learning tool.

Press the appropriate set Status buttons to adjust the status displayed and also updates the Last change time to current time that indicates when somebody last changed the status. Send email send the current status and when this status was set

 

I'll step through each part

  1. Current Time - label that display the current time from a local variable
  2. Pump Status - label that takes the value of a global variable and display appropriate text (1 = Runnnig, 0 = Stopped)
  3. Last Change - label that displays the value of a global variable. This global variable is whenever the status is adjusted by the following two buttons (#4, #5)
  4. Set Status to Running - Button that adjust a global variable to 1 value of 1. This also updates the global variable that holds the Last Change time to the current time. 
  5. Set Status to Stopped - Button that adjust a global variable to 1 value of 1. This also updates the global variable that holds the Last Change time to the current time
  6. Divider - Label with now label value entered
  7. email Pump Status - This reads some global variables and using the Sendemail command sends the email to whatever emaila ccount is associated with HC2 admin role.
  8. MAIN LOOP - this keeps the global and local variable values up to date so they can be displayed in the labels

 

Pick your way through the VD code, I think its straightforward enough.

I did add some debug lines so you'll see more info in the debug screens

 

Press Button action

if you wanted to press one of the on the VD from another scene you need to know two things to use the following command

Please login or register to see this code.

The id of the virtual device (986 above) and the button position (10 above)

The id can be derived from the browser url when you have either the General or Advanced tab open 

 

Please login or register to see this attachment.

 

n my screenshot above it 1870 but when you import it, the id will be different

 

The second is the button position

You need to count all elements in the VD

 

Please login or register to see this attachment.

 

so above buttons 4, 5 and 7 are valid PressButton actions

These can be activated from within this VD, another VD or a scene using the following commands

Please login or register to see this code.

fibaro:log command

The fibaro.log command in the VD presents the information on the VD when looked at in the full list of devices

 

Please login or register to see this image.

/monthly_2017_02/Capture6.PNG.214f32e3eca58596ab111782253ae626.PNG" />

 

Virtual device - Advanced Config Tab

In the advanced Tab of a VD you can define an IP address and Port

If these are defined then the data can be used throughout the VD as follows.

 

Place this code at the top of the main loop, button or label

Please login or register to see this code.

This takes what was entered as part of the advanced tab and assigns them as local variables (ipaddress and port)

This are generally used where a VD is used to execute a http request

 

Some other commands

Please login or register to see this code.

Suggestions to improve welcome

-f

 

 

EDIT : Feb 11

I added one more VD as an attachment from a suggestion from @Jamie mccrostie

It's called Rack Temp Mngt

 

It reads and displays the temperature from two probes I have, one is above each of my equipment racks . (The temp probes are 1-wire and use a Fibaro UBS to connect to HC2)

I have two fans arrays to the side of the racks and these are switched from the two sides of a Fibaro dual relay

This status is displayed as Fan status

 

II then have two ON/OFF buttons to switch the Fans on or off if I see the temp running high.

(I plan on automating this with temp threshold and notifications but it's on my long to-do list)

 

It's a simple scene but I thought it was a good scene to help understand how things can setup

It does use a HomeTable so the devices are all jT.equipment_rack.XXXX - just replace these with a relay id or equivalent local variable.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Please login or register to see this attachment.

Please login or register to see this attachment.

Edited by AutoFrank
  • Like 7
Link to comment
Share on other sites

6 hours ago, AutoFrank said:

 

Hi,

I know that when I started working with the HC2 I struggled with some of the simpler stuff. In some case it was down to a lack of understanding of the concepts rather than the code. 

I decided to put together this Virtual Devices 101 for new people starting out. I put some of this together to answer a few posts but decided to bring it into one post here to make it easier for newer users to find,

I am starting from the basics and if anybody has ideas on how to make it better please let me know

 

 

 

Thanks Auto Frank for this. You have certainly put some effort in here.

Shame on Fibaro, how many hours of productivity lost on missing tutorials in Fibaro use of LUA.

But lets dwell on the positive, this is great.

Link to comment
Share on other sites

Many thanks for this.  I've only just started to delve into Home Automation and already I'm hooked!

 

Loving the learning curve, though!

Link to comment
Share on other sites

  • Topic Author
  • 16 hours ago, MonkeyJug said:

    Many thanks for this.  I've only just started to delve into Home Automation and already I'm hooked!

     

    Loving the learning curve, though!

     

    @MonkeyJug

    great to hear.. keep plugging away :-)

     

    Link to comment
    Share on other sites

  • Topic Author
  • Added another sample VD to the first post.

    It's one I use in my own setup to monitor temperature in my equipment racks as well as fan status

    Link to comment
    Share on other sites

    8 hours ago, AutoFrank said:

    Added another sample VD to the first post.

    It's one I use in my own setup to monitor temperature in my equipment racks as well as fan status

    Pump VD

    Could never get email to work debug ERROR.... line getGlobal("pumpSt..."]:8 :-/

    Jamie

    Link to comment
    Share on other sites

  • Topic Author
  • 12 hours ago, Jamie mccrostie said:

    Pump VD

    Could never get email to work debug ERROR.... line getGlobal("pumpSt..."]:8 :-/

    Jamie

     

    @Jamie mccrostie

    that's a bit odd

     

    Lets simplify it to just text

    the 2 refers to user id and is generally the superuser that you lo into your HC2 with 

    Go to Configuration / Access Control

    Click the edit (pencil) for superuser (don't change anything )  - look in to the browser address  and you should see ......n/configuration/users.html?id=2

    If there is a 2 at the end then this part is okay

     

    The rest is just text - (see below)

    the first text  is the email subject, (alert !) below

    the second text is the email body, (Garden Sensor triggered)

    Please login or register to see this code.

    if it's still not working .. just drop the above into a new scene and start the scene.. the email should send.

     

    give it a whirl and let me know

     

    -f

     

    Link to comment
    Share on other sites

    5 hours ago, AutoFrank said:

     

     

    Is something to do with "change time" part of sample VD at the end of the email body

    Downloaded new copy, woulnt work. Deleted end parts after  "Pump is now RUNNING" and now works

    So what is wrong with the bit...... GOT IT  changetime typo instead of changeTime

    You like placing the "red herrings" Frank

    Message to self, look for debug over whole script downloaded not just you own changes.

    Thankyou you are slowly teaching a mind change here

    Jamie

    Link to comment
    Share on other sites

    5 hours ago, AutoFrank said:

    f

     

    If you wanted to send the emails from the main loop would this work? You said something about more code needed to stop them from being sent repeatedly .

    Apart from being able to call up a pump status email remotely by pressing button or pressing button by scene why else email button in VD?

    Thanks

    Link to comment
    Share on other sites

  • Topic Author
  • 27 minutes ago, Jamie mccrostie said:

     

    If you wanted to send the emails from the main loop would this work? You said something about more code needed to stop them from being sent repeatedly .

    Apart from being able to call up a pump status email remotely by pressing button or pressing button by scene why else email button in VD?

    Thanks

     

    @jimicr

     

    of course all the mistakes are deliberate :oops:

     

    Re the main loop - anything here (incl emailsend) will be executed again every time the main loop repeats, so in my sample , thats why I added it to the button and not the main loop

     

    Re the Why ?

    One use for the send email could for a certain status or status change. For example if you wanted the pump to run all the time then you might send email (or notification) when it stops running.

     

    In this case it could be in the main loop but with in the right if-then-else conditions and this way it's only triggered when you want it to be

     

    make sense ?

     

     

    Link to comment
    Share on other sites

    Just now, AutoFrank said:

     

    @jimicr

     

    of course all the mistakes are deliberate :oops:

     

    Re the main loop - anything here (incl emailsend) will be executed again every time the main loop repeats, so in my sample , thats why I added it to the button and not the main loop

     

    Re the Why ?

    One use for the send email could for a certain status or status change. For example if you wanted the pump to run all the time then you might send email (or notification) when it stops running.

     

    In this case it could be in the main loop but with in the right if-then-else conditions and this way it's only triggered when you want it to be

     

    make sense ?

     

     

    Why are you always mixing me up with others??? HAHAHAHA :shock:

    Link to comment
    Share on other sites

  • Topic Author
  • 37 minutes ago, Jamie mccrostie said:

     

    If you wanted to send the emails from the main loop would this work? You said something about more code needed to stop them from being sent repeatedly .

    Apart from being able to call up a pump status email remotely by pressing button or pressing button by scene why else email button in VD?

    Thanks

     

    @Jamie mccrostie

     

    of course all the mistakes are deliberate :oops:

     

    Re the main loop - anything here (incl emailsend) will be executed again every time the main loop repeats, so in my sample , thats why I added it to the button and not the main loop

     

    Re the Why ?

    One use for the send email could for a certain status or status change. For example if you wanted the pump to run all the time then you might send email (or notification) when it stops running.

     

    In this case it could be in the main loop but with in the right if-then-else conditions and this way it's only triggered when you want it to be

     

    make sense ?

     

     

    Link to comment
    Share on other sites

  • Topic Author
  • 4 minutes ago, jimicr said:

    Why are you always mixing me up with others??? HAHAHAHA :shock:

     

    @jimicr

     

    now I'm very :oops::oops::oops::oops::oops:

     

    could I blame it on the half glass of wine I had with dinner or that both names being with 'j' ?

    note to self..... cop on !!

     

     

    Link to comment
    Share on other sites

    • T.Konopka featured this topic
    • 1 year later...

    The topic has been moved from "

    Please login or register to see this link.

    " to "

    Please login or register to see this link.

    ".

     

    Temat został przeniesiony z "

    Please login or register to see this link.

    " do "

    Please login or register to see this link.

    ".

    Link to comment
    Share on other sites

    • 6 months later...

    Hi Frank,

    Great tutorial! Just a quick question if I may - how often does a VD execute? 

    Well, I found the answer by a question of yours :)

     

    Link to comment
    Share on other sites

    • 3 months later...

    How to resolve that after releasing the "set status to Running" button on the VD, the relay is switched on after  1 hour and after pressing the "Set Status To Stopped" button, the relay switches off after 3 hours?

    Link to comment
    Share on other sites

    • 5 months later...

    @AutoFrank

    I have your VD two buttons and time info when changed, thanks for the beatifull design!

    VD switch relays Fibaro  on/off no problem and the indicating is correct

    If VD on/off>>>GV test on/of correct

    2nd relais on/off does : fibaro:call(484, "pressButton", "4")>>>makes relais switch but VD info stays the same

    What I am doing wrong?

    Please advice

    Link to comment
    Share on other sites

    • 8 months later...

    email pump status scrip Warming, Variable case sensitive!!!

    Thanks.

     

    "Pump is now STOPPED and status was last changed at "..changeTime.."."

     

    Error an "T" stat "t" an original file.

    Edited by Pablo1326
    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...