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

Check after sleep before action is taken.


Bad33nd

Question

Hi Everyone,

 

Since a couple of weeks im trying to learn a bit about Lua programming. At the moment im struggling to find an answer to my problem. 

 

To switch floor heating pump i used a power plug on my central heating boiler to measure the power usage to turn on the power of the floor heating pump (also a power plug). First i made a simple block scene which worked fine at first but now i found out that also when im using tap water the central heating boiler is using a significant amount of power. so the idea is to use the living room temp. sensor and the power measurement of the central heating boiler to switch the floor heating pump. Since the margin is very small im trying to do a double check so check if the conditions match, sleep for 3 minutes and check the conditions again before powering off. 

This is a bit of the code i used but get a error.

 

if (PowerCV <'1') and (PompStatus >'1') then
  fibaro:debug('wacht 3 min voor uitschakelen')
  fibaro:sleep(10000)
  fibaro:debug(' 3 min later')

and (PowerCV <'1') and (PompStatus >'1') then
  fibaro:call(191, "turnOff")
  fibaro:debug('Pomp staat nu uit')
end 

 

 

it seems like im not able to check another set of conditions after the "sleep". is there a way to do this? all help is appreciated. thank!

Edited by Bad33nd
Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

When you write with "if" and "then" the code only runs once. 

 

You need to use "while" and "loop" so the code runs forever. 

 

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • @amilanov  Thanks for your reply. Im having trouble finding examples of the "while" and "loop" functions. I don't know how to implement them. But even wen I run the code multiple times is does not let me recheck the value I guess. is the only way to get this working with more local variables?  

    Link to comment
    Share on other sites

    • 0

    Just write:

     

    Please login or register to see this code.

     

    The above will work until the condition [add condition] has been met.

     

    To get a value of a device use this, replace [add device id here] with the device id = 57

     

    Please login or register to see this code.

     

    If the above doesn't help, please explain to me again what you are trying to do and I can write the code for you in its entirety.

     

    Apologies, I'm a bit busy now, but can help more later if you still need it.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @amilanov Thanks for the explanation. However I still think that won't do the trick for me. 

     

    I will try to tell you what im looking for:

     

    Check power usage of central heating boiler and if power usage is lower than 10W, Wait 3 min. Then Check again if central heating boiler is using less than 10W. if so then turn floor heating pump of, if not end.

     

    The second check is the problem for me i don't know how this can be done. It is needed since the power usages of the central heating boiler sometimes drops below the 10w for just a few seconds, without the second check it will switch off the pump every time so the  central heating boiler will overheat and turn of and reignite a few minutes later. 

     

    If it it still unclear please let me know so i can rephrase. Thanks for your help! 

    Link to comment
    Share on other sites

    • 0

    I feel bad that I haven't just written the code for you... The problem you are suggesting is very doable. 

     

    I need to know how many times do you want to check that power is less than 10 within the 3 minutes? 

     

    I have assumed every 5 seconds for now ie whets the line shows sleep for 5000 below

     

    I'm on a phone so writing in semi-pseudo code. 

    Where I have written "power" , you need to insert the Fibaro:call to get the power value. 

     

     

    Local status = 1

     

    While. status == 1 do

     

    If power < 10 then

      power_low = true

     

      While power_low == true do

        fibaro:sleep(5000)

        If power > 10 then power_low = false end

        Power_timer = Power_timer + 5

        If Power_timer > 600 then power_low = pumpoff  Power_timer = 0 end

       End

     

    If power_low == pumpoff then fibaro:call(pump id, turnoff)  end

    -- if you want to exit the scene here then set "status" to something other than 1 and the scene will end

     

    Fibaro:sleep(1000)

     

    End

     

     

     

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @amilanov Merry Christmas!  Thanks again for your help. I think I'm nearly there however there are some issues with my code. This is what i got at the moment:

     

    Please login or register to see this code.

    I don't know what im doing wrong but I keep getting a the following error: 

    [DEBUG] 14:21:14: 2019-12-24 14:21:14.164166 [ fatal] Unknown exception: /opt/fibaro/scenes/31.lua:39: attempt to perform arithmetic on global 'Power_timer' (a nil value)

    What am I missing?

    Link to comment
    Share on other sites

    • 0

    Where you have

    Please login or register to see this code.

     

    Add below it 

    Please login or register to see this code.

     

    I think this will resolve the issue

     

     

    I would have thought that you wanted to keep checking the latest value for PowerCV

     

    Please login or register to see this code.

     

    Currently you only check update the value once at the top of your code.

     

    You then have 2x conditions that check PowerCV < 10, the value of PowerCV will never change, from the initial value obtained in the line of code above, once you are in the main loop. You should be checking the latest value below in this line:

     

     

    Please login or register to see this code.

     

    Rather than testing the static variable value of PowerCV.

     

    EDIT: Are you sure that once the main loop finishes you do not want this code to run again?

    If you want it to run again, then you may want to create an outer while, then, do loop that never stops running.
    Otherwise you will need to keep restarting this scene each time the main loop completes.

     

    Edited by amilanov
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Sorry for the late reply, i have added the local power timer as suggested.

    On 12/26/2019 at 11:07 PM, amilanov said:

    I would have thought that you wanted to keep checking the latest value for PowerCV

     

    Please login or register to see this code.

     

    Currently you only check update the value once at the top of your code.

    I did not even realise that I was doing this thanks for explaining. I really learned something now.

     

    However i still have an error that i don't understand. first part of the code runs fine but since it keeps on running it will not restart when power is adjusted and send this error after a few minutes. 

     

    [DEBUG] 20:01:37: 2020-01-02 20:01:37.443546 [ fatal] Unknown exception: /opt/fibaro/scenes/31.lua:47: Assertion failed: Expected string
     

    this is

     the code now.

    Please login or register to see this code.

    On 12/26/2019 at 11:07 PM, amilanov said:

    EDIT: Are you sure that once the main loop finishes you do not want this code to run again?

    If you want it to run again, then you may want to create an outer while, then, do loop that never stops running.
    Otherwise you will need to keep restarting this scene each time the main loop completes.

    Since "Power 273" is in the properties it will initiate a start when the value of this changes right? or do you suggest another way? Since this is my first lua program im open to suggestions. 

     

    Thanks for your help.

    Link to comment
    Share on other sites

    • 0

    Having a very quick look at your code, first when you set power low = pumpoff on line 42:

    You need "pumpoff" to be in quotes:

    Please login or register to see this code.

     

    Also, you need the quotes on line 47:

    Please login or register to see this code.

     

    I'll check the rest of your code and get back to you if I see anything unusual.

     

     

    Edited by amilanov
    Link to comment
    Share on other sites

    • 0
    On 1/2/2020 at 7:13 PM, Bad33nd said:

    Since "Power 273" is in the properties it will initiate a start when the value of this changes right? or do you suggest another way? Since this is my first lua program im open to suggestions. 

     

    Yes, this is a good basic way of triggering the scene. Each time the power changes the scene will run. And you have written the right code here to stop any other scenes from running at the same time:

    Please login or register to see this code.

    You could also set max running instances of the scene = 1 

     

    Please login or register to see this image.

    /monthly_2020_01/Capture.PNG.170b6a733aebd05b96d4ba4761060da6.PNG" />

    Link to comment
    Share on other sites

    • 0

    From looking at your code, if your intention is to:

    - turn on device = 191 when device = 273 power is >10 and device = 84 is < 25 then the first part works

    - the second part looks to me, barring any syntax errors, to turn off  device = 191 when device = 273 power is below 10 for 300 units which given you have a 5000 millisecond sleep in the while loop, would be after a continuous period of 150 seconds; if this continuous period of 150 seconds isn't reached and power of 273 goes above 10 then you drop out of the loop until power drops to below 10 again, which all looks good.


    I think the only issue you had was when you set power_low = "pumpoff" you need to ensure that it was a string value you where setting. You could alternatively set power_low to a value like 100000 and then test for a value at line 47 below, but I think using a string makes it easier to read, so I would keep it as "pumpoff".

     

    Well done!

     

     

     

    Link to comment
    Share on other sites

    • 0
    13 minutes ago, amilanov said:

    You could also set max running instances of the scene = 1 

     

    Please login or register to see this link.

     

    Yes you could set Max. running instances to 1, but I don't recommend it since it is always possible that first instance still didn't stop running and second start running. Before second scene instance is stopped with that line of code will send you warning that too many instances is running which can be annoying.

     

    I recommend to always set Max. running instances on greater number than 2 and only use that line of code:

    Please login or register to see this code.

    to terminate any other scene instances. Of course this line of code is recommended to put at the beginning of scene code.

     

    Link to comment
    Share on other sites

    • 0
    On 12/10/2019 at 3:38 PM, Bad33nd said:

    Is there no one able to help me out with this? alle help is appreciated..

    Try to use

    Please login or register to see this link.

    no Lua knowledge required and fits your need.

    Link to comment
    Share on other sites

    • 0
    3 hours ago, Sankotronic said:

     

    Yes you could set Max. running instances to 1, but I don't recommend it since it is always possible that first instance still didn't stop running and second start running. Before second scene instance is stopped with that line of code will send you warning that too many instances is running which can be annoying.

     

    I recommend to always set Max. running instances on greater number than 2 and only use that line of code:

    Please login or register to see this code.

    to terminate any other scene instances. Of course this line of code is recommended to put at the beginning of scene code.

     

    Thanks for the pickup! 

     If I only cared to look at my scenes before posting I would have seen this is what I have done myself. 

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks for the help guys! It looks like we got it working. I'm implanting it now and will test for a few days.

    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.

    ×
    ×
    • Create New...