Jump to content
  • 0

Basic Code Clips for LUA starters


Question

To help those that are not familiar with LUA a starter library of clips that work in Fibaro.

Please add any you find useful.

Link to post
Share on other sites
  • Answers 151
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

Posted Images

Recommended Posts

  • 0
Guest dsilletti
look here

 
     if Pieza == 1 then
           if Rotacion == 1 then
               Piezas = Cuadrado1
           elseif Rotacion == 2 then
               Piezas = Cuadrado2
           elseif Rotacion == 3 then --this is Line 273
               Piezas = Cuadrado3
           elseif Rotacion == 4 then
               Piezas = Cuadrado4
           else

           end 
end

Hi, thank you for support, anyway in my case there are several commands to execute in every "if->then", my code seems OK, I don't understand where is the mistake, can you please help me understanding how to solve this issue?

regards

Domenico

[ Added: 2013-03-29, 14:51 ]

Ok, I solved removing all else, using only if conditions for the 3 rooms

Link to post
Share on other sites
  • 0
Guest dsilletti
dsilletti, for "elseif" you don't have "end" in proper LUA

like for "else" there is no "then"

thanks, got it, I'm a newbie in LUA :->

Finally we can do a lot of things with LUA, but according to me, a special section is required to store all LUA scenes examples, most useful scripts, tips and tricks sent by Fibaro Team and users.

Regards

Domenico

Link to post
Share on other sites
  • 0

Here is a scene to check if there are more then 1 instance of any scene running.

If there are more then 1 instances it will try to kill it and start again (unfortunately killScenes not working in 3.517 - send to Bug tracker)

It's important, as if you start the same scene many times accidentally or with the other scene at the end it may lock your HC. I hope it's helpful.

--[[
%% properties

%% globals
--]]

local maxscenes = 255

for i = 1, maxscenes do

local num = fibaro:countScenes(i);

if (num > 1) then

 			fibaro:debug('Scene '..i..' has '..num..' instances')
   		--fibaro:killScenes(i) -- not working BUG
   		--fibaro:startScene(i)
end  
 	i = i + 1;

end
fibaro:debug ('END')

Link to post
Share on other sites
  • 0

Refresh nightTime global variable every 60 sec.

--[[ 
%% autostart 
%% properties 
%% globals 
--]] 

while true do 

 local currentDate = os.date("*t");
 local sunriseHour = fibaro:getValue(1, 'sunriseHour');
 local sunsetHour = fibaro:getValue(1, 'sunsetHour');

 local currentSec = tonumber(currentDate.hour) * 60 + tonumber(currentDate.min);
 local sunriseSec = tonumber(string.sub(sunriseHour, 1, 2)) * 60 + tonumber(string.sub(sunriseHour, 4));
 local sunsetSec = tonumber(string.sub(sunsetHour, 1, 2)) * 60 + tonumber(string.sub(sunsetHour, 4));

 if (currentSec>sunriseSec and currentSec	fibaro:setGlobal("nightTime", "0");
 else
fibaro:setGlobal("nightTime", "1");
 end

 fibaro:debug('Now: ' .. currentSec .. '; Sunrise: ' .. sunriseSec .. '; Sunset: ' .. sunsetSec .. '; Night: ' .. fibaro:getGlobal("nightTime") .. ';');

 fibaro:sleep(60000);

end

Link to post
Share on other sites
  • 0

Problem with that is the HC2 don't have meteo in direct. For me HC2 refresh too late, i have the moon 1after.

Link to post
Share on other sites
  • 0
jrkalf, :)

if you will write id_device =1 then you gonna rebulid for all devices (leave for night that will take few h)

id_devices >1 then only for that one device

fibaro:call(2,'requestNodeNeighborUpdate')

for all devices

--[[
%% properties
%% globals
--]]
       local id_device = 1
       fibaro:call(id_device,'requestNodeNeighborUpdate')
       fibaro:debug('Rediscover all nodes')
       fibaro:sleep(5000) 

A nice piece of code from Fibaro for optimizing your network.

Link to post
Share on other sites
  • 0

example of Scene activation for dimmer

change 507 for id your device

--[[
%% properties
507 sceneActivation
%% globals
--]]
local id= 507;
local name = fibaro:getName(id)
fibaro:debug(name);

local name = tonumber(fibaro:getValue(id, "sceneActivation"));
if (tonumber(fibaro:getValue(id, "sceneActivation"))==16)
 then
 fibaro:debug("1 click s1 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==26)
 then
 fibaro:debug("1 click s2 "..name);
 elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==25)
 then
 fibaro:debug("3 click s2 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==15)
 then
 fibaro:debug("3 click s1 "..name);
 elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==14)
 then
 fibaro:debug("2 click s1 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==24)
 then
 fibaro:debug("2 click s2 "..name);
   elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==12)
 then
 fibaro:debug("hold click s1 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==13)
 then
 fibaro:debug("release click s1 "..name);
   elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==22)
 then
 fibaro:debug("hold click s2 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==23)
 then
 fibaro:debug("release click s2 "..name);
   elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==10)
 then
 fibaro:debug("change from turn off to turn on s1 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==11)
 then
 fibaro:debug("change from turn on to turn off s1 "..name);
   elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==20)
 then
 fibaro:debug("change from turn off to turn on s2 "..name);
elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==21)
 then
 fibaro:debug("change from turn on to turn off s2 "..name);
 elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==17)
 then
 fibaro:debug("up "..name);
 elseif (tonumber(fibaro:getValue(id, "sceneActivation"))==18)
 then
 fibaro:debug("down "..name);
 end;

Link to post
Share on other sites
  • 0

Open the gate for all guests coming for a party.

Here is a code that will automatically open the gate and turn on the lights for a specific time i.ex. 30 minutes after someone press the Bell ring. Very convenient when plenty of guests are coming at the same time.

In order for this to work the scene has to be "not active" otherwise it will always open the gate.Just before the party start the scene and during the preset time every guest will have the gate open after the ring. Make sure that the ring switch is active for more then 1 second or change the fibaro:sleep() parameter to lower.

"Dark" is a global variable set to 1 after sunset.

--[[
%% properties
15 value
%% globals
--]]

fibaro:debug('start');

local starttime = os.time();
local maxminutes = 20; --time when the scene will be active
local go = 1;

local startSource = fibaro:getSourceTrigger();

while go > 0 do

if 
		( tonumber(fibaro:getValue(15, "value")) > 0 )	 --ring   	
then
 		fibaro:debug('ring');
   	fibaro:call(13, "turnOn") --open the gate

   	if (tonumber(fibaro:getGlobalValue("Dark")) == 1) then
		fibaro:call(77, "turnOn");  --turn on the lights
		fibaro:call(120, "turnOn");
		fibaro:call(117, "turnOn");
       end
end
 	currenttime = os.time();

 	if currenttime - starttime >= maxminutes*60 then 
   	fibaro:debug(maxminutes..' minutes')
   	go = 0;
   	fibaro:abort();
end
 	fibaro:sleep(1000);
end
fibaro:debug('end');

Link to post
Share on other sites
  • 0

example virtual device who can control heating panel

In zone id 1 I want change temperature for Monday on 12

HC2 = Net.FHttp("192.168.100.53",80)
HC2:setBasicAuthentication("admin", "admin")

-- get harmonogram
response ,status, errorCode = HC2:GET("/api/panels/heating?id=1")
jsonTable = json.decode(response)

-- change tabele in Lua
jsonTable.properties.monday.morning.temperature = 12;

-- send back
json = json.encode(jsonTable);
HC2:PUT("/api/panels/heating?id=1", json)
fibaro:log("ready")	

and here you got example for Holiday Mode

0 - turn off Holiday Mode

>0 turn on Holiday Mode and set that temperature

HC2 = Net.FHttp("192.168.100.53",80)
HC2:setBasicAuthentication("admin", "admin")

-- get harmonogram
response ,status, errorCode = HC2:GET("/api/panels/heating?id=1")
jsonTable = json.decode(response)

-- change tabele in Lua
jsonTable.properties.vacationTemperature = 12;

-- send back
json = json.encode(jsonTable);
HC2:PUT("/api/panels/heating?id=1", json)
fibaro:log("ready")

Link to post
Share on other sites
  • 0

Ah this is great thanks!

Trying to build a thermostat for on the wall using a friendly arm... so this is great for that...

is there also a way to get the actual schedule of a room through json?

any way to control the alarm system? disarm / arm... with pincode? (using json)

Link to post
Share on other sites
  • 0

Hi All,

As a real newbie to LUA can someone point me in the right direction for more info in regards to coding for the HC2, for example in the following line of code:

fibaro:call(77, "turnOn")

“call” appears to be a valid option as well as “getvalue” and “debug”

Is there a more comprehensive list somewhere and how they relate to the HC2?

Also, “turnon” in the line above, is this predefined or is there a list that relates to HC2?

Thanks in advance for any help.

Link to post
Share on other sites
  • 0

amilanov, you can inspect all device actions and properties when you are in LUA Mode :-D see screen capture below.

post-1608-14230354250454_thumb.png

Link to post
Share on other sites
  • 0

Ah, that almost feels like a Homer Simposon "Doh!"" moment. I'll have a play around with the interface before I ask anymore questions :-P

Link to post
Share on other sites
  • 0
Guest Charly

For security reasons it may be smart to not always switch the lights off at the same hour and minute a small addition can change the time every day. Here I have added the math.random function and made the switch off time random between 23:18 and 23:43

--[[

%% properties

%% globals

--]]

local sourceTrigger = fibaro:getSourceTrigger();

if (sourceTrigger["type"] == "autostart") then

while true do

local currentDate = os.date("*t");

local startSource = fibaro:getSourceTrigger();

if (

( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "23:" .. string.format("%02d",math.random(18,43))) )

)

then

fibaro:call(6, "turnOff");

fibaro:sleep(320000);

fibaro:call(4, "turnOff");

end

fibaro:sleep(60*1000);

end

else

local currentDate = os.date("*t");

local startSource = fibaro:getSourceTrigger();

if (

startSource["type"] == "other"

)

then

fibaro:call(6, "turnOff");

fibaro:sleep(320000);

fibaro:call(4, "turnOff");

end

end

Link to post
Share on other sites
  • 0

A wake up light that increases the dimlevel of the light in a given interval. You can change the settings in the beginning of the script.

--[[

%% autostart

%% properties

%% globals

--]]

while true do

local currentDate = os.date("*t");

local wakeuptime = "21:34"; -- time to wake up

local startlevel = 40; -- start dim level

local dimlevel;

local maxlevel = 100; -- max dim level

local diminterval = 1; -- interval time in minutes to wait to next dimlevel

local levelsteps = 20; -- steps in procent to increase dim level

local light = 21; -- light to control

local debug = true;

if (maxlevel > 100) then maxlevel = 100; end

if (startlevel > maxlevel) then startlevel = maxlevel; end

if ( ( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == wakeuptime) ) )

then

fibaro:debug("Scene started at: " .. os.date());

for level = startlevel, maxlevel, levelsteps do

dimlevel = level;

if (dimlevel > 100) then dimlevel = 100; end

fibaro:call(light, "setValue", dimlevel);

if (debug) then fibaro:debug("Set dim level at: " .. os.date()); end

if (debug) then fibaro:debug("Dimlevel: " .. dimlevel); end

fibaro:sleep(diminterval*60*1000);

end

end

fibaro:sleep(60*1000);

end

Remko

Link to post
Share on other sites
  • 0

Hi

I am trying to use your script as below

--[[
%% autostart
%% properties
34 value
35 value
37 value
38 value
39 value
40 value
46 value
69 value
%% globals
--]]

windows = {34, 35, 37, 38, 39, 40, 46, 69}

local startSource = fibaro:getSourceTrigger();

while true do

for i = 1, #windows do
 local winname = fibaro:getName(windows[i]);

 if tonumber(fibaro:getValue(windows[i], "value")) > 0     
     then
     fibaro:debug("Open "..winname)
  fibaro:call(2, "sendEmail", "Breach", "Breach "..winname)
 end

 end
fibaro:sleep(30*60*1000)
 end

but every time I open the door I get 2-3 emails, as if the 30 minutes pause is not having any effect.. I'm running 3.548. What am I missing?

Thanks a lot

Riccardo

Link to post
Share on other sites
  • 0

rcanetta,

Probably you have started a few instances of the same scene. Stop the scene and Add the following code at the beginning before 'while' :

 if fibaro:countScenes() > 1 then fibaro:abort() end

I hope it helps.

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