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

CO2 scene anomaly


Question

Posted

Hello,

 

I made the following scene in LUA (to start ventilation when CO2 level is getting high and to stop it when it has gone back down). See code below.

It uses Netatmo device for CO2 reading (polling frequency 10 minutes).

 

I noticed that the scene is acting strangely - every 10 minutes (the time the new CO2 reading arrives from Device 24), the fans (devices 40 and 42) are switched on very briefly, for 1...3 seconds, and then back off.

CO2 reading has all the time I'm testing been around 600, so well below the threshold levels.

 

What can cause this anomaly?

 

=========================

 

--[[
%% autostart
%% properties
24 value
%% weather
%% events
%% globals
--]]

local startSource = fibaro:getSourceTrigger();
if (
 ( tonumber(fibaro:getValue(24, "value")) > 900 )
or
startSource["type"] == "other"
)
then
    fibaro:call(40, "turnOn");
    fibaro:call(42, "turnOn");
else if (
( tonumber(fibaro:getValue(24, "value")) <= 800 )
or
startSource["type"] == "other"
)
then
    fibaro:call(40, "turnOff");
    fibaro:call(42, "turnOff");
end
end

 

========================

 

 

5 answers to this question

Recommended Posts

  • 0
Posted

This is how I would do it. If I had a such a sensor, of course :-) So I can't test it.

Do you need the "autostart" (meaning: do you want to run the scene when the HC boots?).

Please login or register to see this code.

 

  • 0
  • Inquirer
  • Posted (edited)

    Vow. Very short and elegant, will try this.

    Thank you!

    I'm new to this LUA programming but I like the challenge.

     

    Meanwhile, I've been breaking my teeth on another ventilation scene.

    What I want this scene to do is - to switch on ventilation fans every evening at 22 o'clock, have them working for 1 hour, then switch them off again.

    Except when the room is below 20C (sensor 23) for some reason (heating off? holiday mode?) - then the scene should not be run.

    No need to say I haven't got it to work so far. :-) What are the error(s)?

     

    ==============================

     

    --[[
    %% autostart
    %% properties
    23 value
    %% weather
    %% events
    %% globals
    --]]

    local currentDate = os.date("*t");
    if (
     (currentDate.hour == 22) and
     (currentDate.min > 00 and currentDate.min < 06) and
     (tonumber(fibaro:getValue(23, "value")) > 20 )
    )
    then
        fibaro:call(40, "turnOn");
        fibaro:call(42, "turnOn");
        
    fibaro:sleep(3600)

          
        fibaro:call(40, "turnOff");
        fibaro:call(42, "turnOff");

     

    end

     

    ====================================

    Edited by Marti
    additions
    • 0
    Posted
    1 minute ago, Marti said:

    Vow. Very short and elegant, will try this.

    Thank you!

    I'm new to this LUA programming but I like the challenge.

     

    Meanwhile, I've been breaking my teeth on another ventilation scene.

    What I want this scene to do is - to switch on ventilation fans every evening at 22 o'clock, have them working for 1 hour, then switch them off again.

    No need to say I haven't got it to work so far. :-) What are the error(s)?

     

    Welcome, welcome!

     

    That Lua code has two "triggers": autostart and "23 value". So, it starts when the HC boots, and each time 23 (whatever device that may be) changes its value. It then compares the time. It will either run for an hour, or it ends immediately. After the last line, everything ends... So it isn't actually sitting and waiting until that particular time passes.

     

    I'm working at the moment, so I may offer more a bit explanation tomorrow. But here's a quick fix.

     

    Before the "local currentDate" add:

      while true do

     

    Then below the last line, add:

     

       fibaro:sleep(50000)

       end

     

    The "while ... end" wraps your code in an infinite loop. So it'll keep comparing the time.

     

    The sleep slows down the loop, so other process can keep running on your HC. It's important.

     

    Oh, fibaro:sleep(3600) is 3.6 seconds. You probably want: fibaro:sleep(3600*1000)

     

    BTW there are certainly pre made scripts on the forum to do this kind of stuff.

    • 0
  • Inquirer
  • Posted

    Thank you! It worked.

    A bit later of course I realised if I want to run these two scenes side by side, they will be in conflict.

    For example - one scene tells ventilation fans to switch on at 22:00, then the other (CO2 checking) scene will kick in and as the CO2 is below threshold, tells to switch off the fans.

     

    So today I have been trying to create a single scene with all my ventilation related requirements in it - complicated, still remains to see if I can figure this out.

    I have searched this forum and in Google and am moving closer to a working solution step by step.

    • 0
  • Inquirer
  • Posted

    After a few hours of trial and error, I figured out how to put all of my ventilation requirements into a working single "master" scene.

    It became quite a long script with a lot of IF.... ELSEIF... expressions, sensors and actors, but everything seems to be functioning.

    As it is only 2nd day of my career as a "LUA coder" (and I do quite different things for living), I can't help but feel a little proud of myself :-)

     

    I also got a new question (as always:) - 

    -  is there a way to use WHILE .... DO expressions in Fibaro LUA?

    For example, for CO2 control I wanted to use something like this, see code below, but didn't seem to get it to work.

     

     

    =====================

     

    WHILE (tonumber(fibaro:getValue(35, "value")) <= 800) DO

        fibaro:call(40, "turnOn");
        fibaro:call(42, "turnOn");
        fibaro:call(50, "turnOn");

        fibaro:sleep(50*1000);

    END

     

    =====================

     

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