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

[SOLVED]LUA Boolean Logic Problem


Question

I have a scene that is suppposed to check a variable called "Snow" at Monday through Friday at 5 am and 3 pm and weekends at 7 pm and 3 pm. If Snow = "Yes", a scene is activated, turning on my outdoors heating mats for 2 hours (and at the same time resetting Snow = "No"). My thinking is to limit the use of expensive snow melting electricity to max 2 + 2 = 4 hours / 24 hours

 

The problem with the code below is that the scene triggers with variable Snow = "Yes" also outside the stated hours. What is it I am not getting right here ? I thought the AND operator had hierarchial priority over OR and that the code below would turn on the heater if the variable Snow = "Yes" AND the time is 5 am or 3 pm on weekdays or 7 am and 3 pm in weekends. But it seem to trigger whenever the variable Snow = "Yes", also outside these hours.

 

Help will be highly appreciated! (Scene 51 is the scene actually turning on the heating mats for two hours):

 

Best regards

Jo

 

--[[
%% autostart
%% properties
%% weather
%% events
%% globals
Snow
--]]

local sourceTrigger = fibaro:getSourceTrigger();
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
 ( ((currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "05:00") )
and
 ( fibaro:getGlobalValue("Snow") == "Yes" )
or
 ( ((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) == "15:00") )
and
 ( fibaro:getGlobalValue("Snow") == "Yes" )
or
 ( ((currentDate.wday == 1 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "07:00") )
and
 ( fibaro:getGlobalValue("Snow") == "Yes" )
)
then
    fibaro:setGlobal("Snow", "No");
    fibaro:startScene(51);
end

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
7 godzin temu, jodohl napisał:

The problem with the code below is that the scene triggers with variable Snow = "Yes" also outside the stated hours.

 

--[[
%% autostart
%% properties
%% weather
%% events
%% globals
Snow
--]]

 

 

Hi @jodohl
Remove the Snow trigger variable from Your code.

BR

Link to post
Share on other sites
  • 0
  • Inquirer
  • Hi!

     

    I do not understand your advice. Remove the Snow variable...? The wole point is to trigger the scene at certain times IF Snow = Yes. 

    Link to post
    Share on other sites
    • 0

    Hi, 

    this is Your triggers what You use to start the scene:

    --[[
    %% autostart
    %% properties
    %% weather
    %% events
    %% globals
    Snow
    --]]

    every time when those variables change the scene will run , your scene based on time events 2 times a day, so You don't need Snow here , %%weather also is no need. 

    Edited by domin12
    Link to post
    Share on other sites
    • 0
  • Inquirer
  • Great! Thanks a lot !! 

    This is what happens when a lazy person like myself convert code from block scene to LUA and start modifying without bothering to learn things properly.

    I have struggled a lot with understanding what "The scene will run" really means. Does it mean:

     

    (1) Execute the IF test and do nothing if the IF conditions is not true

    (2) Execute the THEN statement, regardless whether the preceding IF condition is true or false

     

    I guess (2) is what happens. This is equivalent to saying that you do not "trigger" the IF test. The IF test is one way of triggering. Manual execution another. And a change in one of the trigger variables a third. Am i right ?

    Edited by jodohl
    Link to post
    Share on other sites
    • 0

    HI @jodohl

    I did not look at your scene how you use IF statement.

     

    In your case if condition is true then set variable Snow to NO, i think that this change of variable should be in scene No 51.

    i will change a little bit your scene and here You are:

     

    --[[
    %% autostart
    %% properties
    %% events
    %% globals
    --]]
    local sourceTrigger = fibaro:getSourceTrigger();
    function tempFunc()
    local currentDate = os.date("*t");
    local startSource = fibaro:getSourceTrigger();
    if (
     ( ((currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "05:00") )
    and
     ( fibaro:getGlobalValue("Snow") == "Yes" )
    or
     ( ((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) == "15:00") )
    and
     ( fibaro:getGlobalValue("Snow") == "Yes" )
    or
     ( ((currentDate.wday == 1 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "07:00") )
    and
     ( fibaro:getGlobalValue("Snow") == "Yes" )
    )
    then
            --fibaro:setGlobal("Snow", "No"); -- move to 51 scene or move to the bootm as is now
            fibaro:startScene(51);
            fibaro:sleep(5000)
            fibaro:setGlobal("Snow", "No");
        end
    setTimeout(tempFunc, 60*1000)
    end
    if (sourceTrigger["type"] == "autostart") then
    tempFunc()
    end

    Edited by domin12
    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...