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


Recommended Posts

Hi,
After I put together the Virtual devices Introduction Tutorial I was asked would I do something similar for Scenes.

I’ll preface this post (like the Virtual devices) that I am no expert but I am willing to document what I understand and it can be added to over time. It would be great if some of the experst could read through, fill in the coupelof gaps and let me know if I havesomething wrong or coudl do with better explanation

 

I also start at a very basic level as some of the newer users people liked that with the virtual device tutorial

I am also only going to Lua scenes as I have never worked with Magic Scenes or Graphics Blocks

Please login or register to see this attachment.

 

Scene Basics
A scene is a mechanism to execute a lua code script for an on-demand basis.

General Tab

  • Name, Room - self explanatory
  • ID – set when the scene is saved, doesn’t change for life of scene. This is the unique identifier for this scene and can be used as part of the startScene command

Please login or register to see this code.

  • Max number of instances – A scene can be called from a number of different sources. This dictates how many instance of the scene can be running concurrently. This can also be set within the scene using lua. I’m not sure if these two settings are in conflict which one takes priority.

Please login or register to see this code.

  • Lili commands – voice control for running/stopping scene using Lili who is the native HC2 personal assistant. 
  • Run Scene
    • Automatic – it can be called from other scenes or run manually
    • Manual – it can be called from other scenes or run manually (unsure what the difference is between manual and automatic)
    • Disabled – scene cannot be run
  • Scene Hidden: Not visible in the Web UI (unless you select HC2 hidden devices)
  • Protect by Pin: Prompted for PIN CODE if scene is set to running
  • Do Not Alarm to Stop: ???
  • Change Icon – select a new icon to display alongside the scene (128x128 png transparent)

 

Please login or register to see this attachment.

 

 Advanced Tab

  • Lua Window
  • The code in the lua window is the defaulst header for any scene and needs to be kept.
  • Debug window (debug output from scene code when it is run)
    • START – Start the script running 
    • STOP – Stop the script running
    • CLEAR – Clear the debug window

 

Please login or register to see this attachment.


Web UI View
1.    Title of scene
2.    Edit
3.    Delete (confirmation req)
4.    Icon
5.    Disable/enable
6.    Run/Stop scene
7.    Status – populated by fibaro:log command     

 

Please login or register to see this image.

/monthly_2017_02/58a5dca14d223_image1.PNG.78743c2f462000ef3155e456f173bf52.PNG" />

 


 Lua Scene Header

The header of a scene always contains some of all of the following. These are used to trigger the scene in a number of different way.

Please login or register to see this code.

%%autostart – including this means that the scene will run when it is saved or when the HC2 restarts.

$$ properties – the user can add a device ID and parameter. Any change to this parameter will trigger/run the scene. The format is ID following by parameter. 

Please login or register to see this code.

In the example above if 1429 was a door sensor and value was the status parameter that changed (0 <-> 1) when the door opened than any status change would cause the scene to run. This has to be the device ID and cannot be a variable that represents the ID


%%events – ??


%%globals – the user can add the name of global variable (exactly and case sensitive). Any change to the value of the global variable will trigger/run the scene

An example would be 

Please login or register to see this code.

A scene can be setup to trigger from properties, events or global variables

The following code allows a different action depending on the trigger if this is what’s required.

Please login or register to see this code.

In the example above the following happens

  • Scene triggered from property (1425 value) - it will execute function1() 
  • Scene triggered from global variable(sleepState) - it will execute function2()
  • Scene triggered from an autostart - it will execute function3()
  • Scene triggered from any other valid method  - it will execute function4() 
  • or else the scene will no start

  

Local Variable declaration

These are variables tha you intend to use within the scene. You can also use global variable but these do not need to be declared prior to usage

Many people put the local variable declaration at the start of the scene followed by the functions

Please login or register to see this code.

 

Code Comments

Comments /explanations in the code are very useful as the allow you to explain what the code is doing to either yourself or others

You need to preface or encapsulated all comments to prevent them being executed at runtime.

Example of single line and multi-line comments are shown below

 

Please login or register to see this code.

 

In general all scenes will have the following structure but over time most people develop your own style …

 

Please login or register to see this code.


Lua command syntax is generally the same whether it is used in a scene or used in a virtual device (exceptions to this are http requests)

 

Some useful lua command

Please login or register to see this code.


Some useful lua functions

ENCODING - replace a space in a string with another character, a + symbol in the example below

This function takes one parameter - s 

so The Cat and the Mat will be changed to The+Cat+and+the+Mat

Please login or register to see this code.

ROUNDIT

Rounds a number to a defined number of decimal places

This function takes one parameter - num (the number you want rounded) and idp (the number of decimal places)

so roundit(2.12348, 3) would result in 2.123 

Please login or register to see this code.

If there are any other useful functions that you feel would be of benefit to new users please let me know and I'll include them

 

Overall the best way to gain a greater understanding is to download the many scenes that are published to the forum and start to take them apart.
I hope this will be of benefit to some users and suggestions and corrections are always welcome

 

happy coding 

-f
 
 

Edited by AutoFrank
  • Like 1
  • Thanks 1
Link to post
Share on other sites

Very nice job @AutoFrank!!

A scene set to manual can be run from an other scene aswell, I do it this way with my CO2 alarm.

Once the admins approve it (since last sunday) you can see. Or if you want I can pm it to you

 

Jim

Link to post
Share on other sites
13 hours ago, AutoFrank said:

Some useful lua functions

Encoding 

Please login or register to see this code.


The best way to gain a greater understanding is to download the many scenes that are published to the forum and start to take them apart.


The one major difference in Fibaro’s interpretation of Lua between virtual devices and scenes is http  and tcp socket requests

The format to be used in a scene is as follows


REGULAR HTTP REQUEST

Please login or register to see this code.

 

ENCODED HTTP REQUEST

Please login or register to see this code.


TCP SOCKET CONNECTION 

Please login or register to see this code.

 

 

Perfect, thanks @AutoFrank

Great cheat sheet to go with VD tutorial 

Understood everything, kind off, but the LUA  "useful function" bit  was a mystery

Cheers

 

Edited by Jamie mccrostie
Link to post
Share on other sites
  • Topic Author
  • Thanks for the feedback @Jamie mccrostie

     

    I edited the first post with some additional detail to clarify some information

     

    I also removed the http request/api information as it wasnt a good fit for the topic and I will include that in an Introduction to api/HTTP requests tutorial I am putting together

    Edited by AutoFrank
    Link to post
    Share on other sites
    • T.Konopka featured this topic
    • 2 weeks later...

    Very nice!

     

    Is there a possibilty to backup all scenes (incl. hidden scenes) with just one click ?

     

    (and the same questions for the VDs)

    Link to post
    Share on other sites
  • Topic Author
  • 25 minutes ago, Orgli said:

    Very nice!

     

    Is there a possibilty to backup all scenes (incl. hidden scenes) with just one click ?

     

    (and the same questions for the VDs)

     

    Hi @Orgli

    I believe the HC2Toolkit written @Krikroff can do that but I have never used it.

    I think you can download it from the french forum

     

    -f

     

    Link to post
    Share on other sites
    • 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 post
    Share on other sites
    • 1 month later...
    • 1 year later...

     How can I create a block scene which can only be run manually or from another scene? I.e not run by a trigger and not run upon start of the HC2?

     

    It seems I have to choose a trigger at the top of the block sequence. Not choosing a trigger generates a LUA code which generates errors because it creates an empty if statement.

     

    Link to post
    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...