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
Search the Community
Showing results for tags 'overview'.
-
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 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 fibaro:startScene (ID) 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. if (fibaro:countScenes()>2) then fibaro:abort() end -- or if (fibaro:countScenes()>1) then fibaro:debug("stop scene") fibaro:abort() end 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) 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 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 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. --[[ %% autostart %% properties %% events %% globals --]] %%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. --[[ %% autostart %% properties 1429 value %% events %% globals --]] 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 --[[ %% autostart %% properties %% events %% globals G_AREA__1 --]] 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. -- Trigger Management local currentDate = os.date("*t") local startSource = fibaro:getSourceTrigger() --Start of Scene Execution if (startSource["type"] == 'property') then fibaro:debug("Started through property") tempFunc1() -- this is calling the the function if something happens. elseif (startSource["type"] == 'global') then fibaro:debug("Started through variable") tempFunc2() -- this is calling the the function if something happens. elseif (startSource["type"] == "autostart") then fibaro:debug("Started through autostart") tempFunc3() -- this is calling the the function if something happens. elseif (startSource["type"] == "other") then fibaro:debug("Started through other ") tempFunc4() -- this is calling the the function if something happens. else fibaro:debug("Scene not started, this can only be started through other, property, global or autostart!"); end 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 -- example of local variable declaration local HC2email = "true" -- Notification Alerts using Fibaro email service local HC2popUp = "true" -- Notification Alerts using Fibaro popup service notifications local pushOver = "false" -- Notification Alerts using Pushover Notification Service (Tutorial by AutoFrank) local ALLINONE = "false" -- Notification Alerts using 'All in one Notification' scene (created by jompa68) local updateData = "false" -- Set to true to update the data in the table. ** SET BACK TO FALSE ONCE COMPLETE ** 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 -- Example of a single line comment --[[ example of a multiline comment where the sentence spans one or more lines in the scene --]] In general all scenes will have the following structure but over time most people develop your own style … --[[ %% autostart %% properties %% events %% globals --]] if (fibaro:countScenes()>2) then fibaro:abort() end -- explanation of the code purpose or usage or installation -- local variable declarations -- functions -- Main Code sequence (calling the functions above) 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 if fibaro:getGlobalValue("Occupancy_HOUSE") == "no" -- compare global value if fibaro:setGlobal("Alarm_TrigCount", "0") then -- setting global value if tonumber(fibaro:getValue(GardenSensor, "value")) == 1 -- get device status value fibaro:startScene(AlarmSet) -- Start a scene fibaro:call(1387, "pressButton", 10) -- press a button on a vd fibaro:debug("Notify Alert! "..jN[i].device.." is offline") -- print a mix of variable and text string fibaro:call(2, "sendEmail","Alert!", "Garden Sensor triggered") -- send email using HCC2 native email service -- HC2 native popup notirfication service HomeCenter.PopupService.publish({ title = 'Home Alarm Activated ', subtitle = os.date("%I:%M:%S %p | %B %d, %Y"), contentTitle = 'Zones Status', contentBody = fibaro:getGlobalValue("G_SPC_AREA_STATUS_1"), type = 'Success', }) 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 -- function to replace a space in a text string with a + function urlencode(s) if (s) then s = string.gsub (s, "\n", "\r\n") s = string.gsub (s, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) s = string.gsub (s, " ", "+") end return s end test = urlencode(message) 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 function roundit(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end 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