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

Main loop vs pressbutton


Quikdav
 Share

Question

Hi all

Here's what I noticed with main loop.

I have a virtual device. In the main loop, I request an ethernet card. Here is the code in the main loop :

HC2 = Net.FHttp("192.168.11.20")

response ,status, errorCode = HC2:GET("/api/xdevices.json?cmd=10")

fibaro:log("Status = " .. status)

if (tonumber(status) == 200) then

response = json.decode(response)

fibaro:setGlobal("ABO", response.T1_PTEC)

fibaro:setGlobal("CONSO", response.T1_PAPP)

else

fibaro:log("ErrorCode = " .. errorCode)

end

fibaro:sleep(5*1000)

After a few time, my code fails and I have errorcode=2. The only way to restart it is to click on the debug button and to stop it. The code seems to restart

So I used an other way to do the same. I use a scene with my virtual device.

I keep the same virtual device, I add it a button where I copy my code removing the sleep command. And I clean the main loop of the virtual device

Then I create a scene that is autostarted and run each 5 seconds and I call the pressbutton of my virtual device.

In that case, it works very well. I only have status=200 and no errorcode=2 !!!

So for me, there is something wrong that occurs with the main loop that is run each second..

Regards

David

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

+1

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" />

there is a disconnect function in new beta but loop appears very resource hungry

Link to comment
Share on other sites

  • 0

Quikdav, robmac,

Please login or register to see this code.

function has solved all my problems in main loop but i think work only with

Please login or register to see this code.

robmac, +1000

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" /> loop appears very resource hungry... work, but I'm curious to see with 20 virtual devices

Please login or register to see this image.

/emoticons/default_icon_question.gif" alt=":?:" />

Link to comment
Share on other sites

  • 0

Does it not release when not executing? Does it run a continuous loop by default and you need a sleep to allow other processes execution time?

Link to comment
Share on other sites

  • 0

A combination of .sleep(1000) and FTCPSocket:disconnect() in MainLoop made my virtual devices take up much less resources than before v3.562

Link to comment
Share on other sites

  • 0

I sort of get the disconnect as LUA uses lazzzy garbage collection but

the loop executes every 1s and does not release resources when out of user code

Please login or register to see this image.

/emoticons/default_icon_eek.gif" alt=":shock:" />

......warning teenager mode....

like WHY????

Please login or register to see this image.

/emoticons/default_icon_curve.gif" alt=":-/" />

[ Added: 2013-09-17, 23:46 ]

A combination of .sleep(1000) and FTCPSocket:disconnect() in MainLoop made my virtual devices take up much less resources than before v3.562

Just tried that

It is a good idea but as I wanted to count down in 1 second steps it does not work for me.

Do you think that you are using less as it only executes every two seconds now?

Link to comment
Share on other sites

  • 0

Hello, not sure if I have the same case...

what I have on the main loop of a virtual device:

Please login or register to see this code.

I am sure that the URL always has data feedback such as ON or OFF, but somehow ModeStatus come back empty after a while. If I save the main loop again, it works for a while. After a while, it comes back empty again and I have to save the main loop again to make it work. Any idea how to fix this?

Link to comment
Share on other sites

  • 0
Hello, not sure if I have the same case...

what I have on the main loop of a virtual device:

Please login or register to see this code.

I am sure that the URL always has data feedback such as ON or OFF, but somehow ModeStatus come back empty after a while. If I save the main loop again, it works for a while. After a while, it comes back empty again and I have to save the main loop again to make it work. Any idea how to fix this?

One way I solved this is to put the code in a, sort of, "Try-Catch":

Please login or register to see this code.

If something go wrong it prints out the error, sleep for 5 minutes and then restart the main loop.

Link to comment
Share on other sites

  • 0

lindehoff, not really restart main loop

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" /> , it continues the execution of the loop (it's important because the global variables are not reseted without restart) and for example: if json.decode failed and enter in infinite loop process (T_END error ex.) you have not the choice: you must restart your device !

To work around the problem i use:

Please login or register to see this code.

Please login or register to see this image.

/emoticons/default_icon_biggrin.gif" alt=":-D" />

Link to comment
Share on other sites

  • 0
lindehoff, not really restart main loop

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" /> , it continues the execution of the loop (it's important because the global variables are not reseted without restart) and for example: if json.decode failed and enter in infinite loop process (T_END error ex.) you have not the choice: you must restart your device !

To work around the problem i use:

Please login or register to see this code.

Please login or register to see this image.

/emoticons/default_icon_biggrin.gif" alt=":-D" />

i tried the other example for pcall as suggested by A.Socha aswell and your right it does still fail!

Would you be kind enough to explain how to incorporate your code in the following code please... thank you

function getIcon(watt)

if(watt > 9000) then return 1070

elseif(watt >= 8000) then return 1069

elseif(watt >= 7000) then return 1068

elseif(watt >= 6000) then return 1067

elseif(watt >= 5000) then return 1066

elseif(watt >= 4500) then return 1065

elseif(watt >= 4000) then return 1064

elseif(watt >= 3500) then return 1063

elseif(watt >= 3400) then return 1062

elseif(watt >= 3300) then return 1061

elseif(watt >= 3200) then return 1060

elseif(watt >= 3100) then return 1059

elseif(watt >= 3000) then return 1058

elseif(watt >= 2900) then return 1057

elseif(watt >= 2800) then return 1056

elseif(watt >= 2700) then return 1055

elseif(watt >= 2600) then return 1054

elseif(watt >= 2500) then return 1053

elseif(watt >= 2400) then return 1052

elseif(watt >= 2300) then return 1051

elseif(watt >= 2200) then return 1050

elseif(watt >= 2100) then return 1049

elseif(watt >= 2000) then return 1048

elseif(watt >= 1900) then return 1047

elseif(watt >= 1800) then return 1046

elseif(watt >= 1700) then return 1045

elseif(watt >= 1600) then return 1044

elseif(watt >= 1500) then return 1043

elseif(watt >= 1400) then return 1042

elseif(watt >= 1300) then return 1041

elseif(watt >= 1200) then return 1040

elseif(watt >= 1100) then return 1039

elseif(watt >= 1000) then return 1038

elseif(watt >= 900) then return 1037

elseif(watt >= 800) then return 1036

elseif(watt >= 700) then return 1035

elseif(watt >= 600) then return 1034

elseif(watt >= 500) then return 1033

elseif(watt >= 400) then return 1032

elseif(watt >= 300) then return 1031

elseif(watt >= 200) then return 1030

elseif(watt >= 100) then return 1029

else return 1010 end

end

local time = os.time()

local date = os.date("*t", time)

local year = date.year

local month = date.month

local unixtime= os.time{year=year, month=month, day=1}

local rwatt = 0;

local rkwhour = 0;

local rkwhmonth = 0;

local watt = 0;

local kwh = 0;

local month = 0;

thisId = 280

started = os.date("%Y-%m-%d %H:%M:%S")

fibaro:debug("-- ["..started .."] Function Start --")

HC2 = Net.FHttp("192.168.0.8")

rwatt, status, errorCode = HC2:GET("/api/energy/now/now/single/devices/power/276")

rkwhour, status, errorCode = HC2:GET("/api/energy/now-3600/now/single/devices/power/276")

rkwhmonth, status, errorCode = HC2:GET("/api/energy/"..unixtime.."/now/single/devices/power/276")

jsonTable = json.decode(rwatt)

jsonTable = json.decode(rkwhour)

jsonTable2 = json.decode(rkwhmonth)

watt = (jsonTable.W)

kwh = (jsonTable.kWh)

month = (jsonTable2.kWh)

time = os.date("%H:%M:%S")

fibaro:log("Now: "..watt.."W | Last hour: "..kwh.."kWh")

fibaro:call(thisId, "setProperty", "ui.LabelNow.value", watt.." Watt");

fibaro:call(thisId, "setProperty", "ui.LabelHour.value",kwh.." kWh");

fibaro:call(thisId, "setProperty", "ui.LabelMonth.value", month.." kWh");

fibaro:call(thisId, "setProperty", "ui.LabelWhen.value", time);

fibaro:call(thisId, "setProperty", "currentIcon", getIcon(watt))

fibaro:call(thisId, "setProperty", "ui.Button11.icon", getIcon(watt))

ended = os.date("%Y-%m-%d %H:%M:%S")

fibaro:debug("-- [".. ended .."] Function Ended --")

fibaro:debug("Error: "..errorCode)

--fibaro:debug("Sleep 60 sec, then restart")

--fibaro:sleep(60*1000)

Link to comment
Share on other sites

  • 0

deanrparry, what return you pcall? because i dont see it in your code. That same with errorcode and status. USE them because w/o them almost everything can kill your script

-timeout

- no access

- unexpected EOF in json

- empty json

and many more other situations

Link to comment
Share on other sites

  • 0

[DEBUG] 22:38:44: Error: [string "while true do..."]:35: Expected value but found T_END at character 1

[ Added: 2014-02-07, 22:41 ]

Please note this happens after about 15-30 mins everytime... different devices / json decodes / hc2 / xbmc etc...

Link to comment
Share on other sites

  • 0

A.Socha, so you got crash on EOF in json

protect that part

Please login or register to see this code.

by

Please login or register to see this code.

before you will use

Please login or register to see this code.

Link to comment
Share on other sites

  • 0

I think i understand what your saying

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" /> protect the json table in that if statement just below the HC2:GET so if error is none then decode? if there is an error what would happen?

i assume it will just try again on next mainloop cycle?

Link to comment
Share on other sites

  • 0

yes

nothing will happen because if json is damaged you should not work on him and with next cycle when he will be ok script will go to end (in that if use all script)

if status ==200 then

rest script here

else

--waiting for next cycle

end

Link to comment
Share on other sites

  • 0

ok so why when i use a scene and button to run the same code it works fine? or if it does error then i just press save on virtual device with no changes and it works again? to me that says that the virtual device is at fault no?

i make no changes to json data at all EVER!

i will learn you are the master i am the pupil !!

Please login or register to see this image.

/emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

Link to comment
Share on other sites

  • 0

deanrparry, there is 1 main difference

in scene and button code start stand up and on end dying,

in main loop after save code start stand up and working all time till get crash or next save action

Link to comment
Share on other sites

  • 0

exactly how i thought it happened! in fact i just watched the virtual device crash with decode "undefined" labels then few seconds later spring back to life

Please login or register to see this image.

/emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />)))))

i think the if statement has fixed it!!! i have so many virtual devices that need this if statement... hopefully this is the final fix!

thank you

did not mind using scene and button but did not like the way i could not define a button icon it always refreshed everytime... if i could define button11.icon = icon 1079 if value = 500w etc then i would have been happy!

[ Added: 2014-02-08, 11:00 ]

my head hurts!!!!!! still crashing boo hoo

Please login or register to see this image.

/emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> back to scenes i guess something is not right!

[ Added: 2014-02-08, 12:01 ]

DEBUG] 11:52:09: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:52:13: ERRORCODE [2] : STATUS []

[DEBUG] 11:52:13: Error: Waiting 5 seconds to see if JSON resolves itself!

[DEBUG] 11:52:21: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:52:24: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:52:36: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:52:39: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:52:42: ERRORCODE [2] : STATUS []

[DEBUG] 11:52:51: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:02: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:06: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:09: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:21: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:24: ERRORCODE [0] : STATUS [200]

[DEBUG] 11:53:28: ERRORCODE [0] : STATUS [200]

[ERROR] 11:53:28: line 64: Expected value but found T_END at character 1

any help fixing this... i have exhausted as much knowledge as i have

Please login or register to see this image.

/emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> very limited!

Thank you

[ Added: 2014-02-10, 22:49 ]

back to scenes for push button any json errors being detected after a whie killed ALL my home automation!

So now not running 1 main loop and box is SOOOOOOO responsive again!

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

 Share

×
×
  • Create New...