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

Light randomly on and off


Question

Posted (edited)

Hello,

 

My customer wants to create following scene. Unfortunately I do not handle LUA-coding so any help is really appreciated !

 

Customer has following devices: Home Center 2,  a lux sensor and wall plug.

 

Goal is: Creating a Scene that checks if lux is below value 50 and when lux is below that value then put light ON between 19:30 - 23:30 randomly. 

 

So in another way to put it... if lux is below value 50 then put light ON if time is between 19:30 - 23:30 randomly everyday for 15-45 minutes which is random.

 

Example:

Monday: Time is 19:30 and lux value is <50. Put light on behind wall plug for 45 minutes. Then shut down the light (time 20:15). Put light again 20:45 and shut down it 21:00. Again ON at time 21:20 and so on until 23:30 put the light off completely for that day.

 

If lux is belov value <50 for example 19:45 then put light ON (Wall plug) like mentioned above (randomly and for random time).

 

Tuesday and rest of the week same thing...

Edited by janton

7 answers to this question

Recommended Posts

  • 0
Posted

Please login or register to see this code.

look like this

  • Like 1
  • 0
  • Inquirer
  • Posted (edited)

    Thanks 

    Please login or register to see this link.

     

    Please login or register to see this link.

     ,

     

     

    Can you please tell how long the light will be lit per time or is there random time in the code ? 

     

    Because I can not "read" the LUA-code ... can you explain how the code works :-D

     

    I could not get it working... the wall plug (local powerId) do not start at all between local rules times.

     

    Can I just copy the code or is there anything to add or change except following parts:

     

    Please login or register to see this code.

     

    I tried this morning (09:22-10:00) below code and wall plug did not turn ON ? Did I miss something ?

     

     

    --[[
    %% properties
    449 value
    %% globals
    --]]


    function between(timestr)
        local dayMap = {["1"] = 1, ["2"] = 2, ["3"] = 3, ["4"] = 4, ["5"] = 5, ["6"] = 6, ["7"] = 7}
        local smap = {
            ["Sunset"] = "sunsetHour",
            ["Sunrise"] = "sunriseHour",
            ["Evening"] = "sunsetHour",
            ["Morning"] = "sunriseHour"
        }

        local function trim(s)
            return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
        end

        local function offset(s, o, p)
            if (not (o and p)) or o == "" then
                return s
            end
            local h, m = s:match("(%d+):(%d+)")
            local t = 60 * h + m + (o == "+" and p or -tonumber(p))
            return string.format("%02d:%02d", math.floor(t / 60), t % 60)
        end

        local function sec2hmStr(hms)
            local h, m, s = math.floor(hms / 3600), math.floor((hms % 3600) / 60), hms % 60
            return string.format("%02d:%02d", h, m)
        end

        local function betw(interv)
            local dt, start, stop = os.date("*t"), interv:match("^([%S%a0-9%.%:]+) ?(.*)")
            stop = stop == "" and start or stop
            local d1, h1, m1 = start:match("^([%S%a%.]-)%.?(%d+):(%d+)$")
            local d2, h2, m2 = stop:match("^([%S%a]-)%.?(%d+):(%d+)$")

            d1 = d1 == "" and (dt.wday == 1 and 7 or dt.wday - 1) or dayMap[d1]
            d2 = d2 == "" and d1 or dayMap[d2]
            local d = dt.wday == 1 and 7 or dt.wday - 1
            if not (d >= d1 and d <= d2) then
                return false
            end
            m1, m2, t = h1 * 60 + m1, h2 * 60 + m2, dt.hour * 60 + dt.min
            if (m1 <= m2) then
                return m1 <= t and t <= m2 -- 01:00-02:00
            else
                return m1 <= t or t <= m2 -- 23:00-21:00
            end
        end
        timestr =
            timestr:gsub(
            "$(%w+)([%+%-]?)(%d*)",
            function(s, o, p)
                return offset(smap and fibaro:getValue(1, smap) or fibaro:getGlobal(s), o, p)
            end
        )
        for _, interv in ipairs(split(timestr, ",")) do
            if betw(trim(string.gsub(interv, "%.%.", " "))) then
                return true
            end
        end
        return false
    end


    local powerId = 729;
    local luxId = 449;
    local minLux = 50;

    -- rules sample
    -- Mo - Fr = 09:22..10:00 and 20:45..21:00
    -- Weeakend (Sa, Su) = 19:30-22:00
    -- also you may use mnemonics
    -- $Sunset..22:00
    -- $Sunset+15:00..22:00

    local rules = "1.09:22..5.10:00,1.20:45..5.21:00,6.19:30..7.22:00";
    while true do

        local status = fibaro:getValue(powerId, "value");
        local lux = fibaro:getValue(luxId, "value");

        if between(rules) then
            if lux < minLux then
                if tonumber(status) == 0 then
                    fibaro:call(powerId, "turnOn");
                end
            end
        else
            if tonumber(status) > 0 then
                fibaro:call(powerId, "turnOff");
            end
        end
        fibaro:sleep(1000 * 10);    
    end

     

     

    error code is:

    [DEBUG] 09:22:19: [1;31m2018-08-30 09:22:19.547975 [ fatal] Unknown exception: /opt/fibaro/scenes/601.lua:88: attempt to compare string with number

     

    Please login or register to see this attachment.

    Edited by janton
    • 0
    Posted (edited)

    sorry. I provided code like an idea. 

    I never tested this code.

    if you can't develop as smb to help this. I can't.

     

    this should works. 

     

    no support! as-is!

     

    thank you for understanding!

    Please login or register to see this code.

     

    Edited by 10der
    code updated
    • 0
  • Inquirer
  • Posted

    Hi,

     

    thanks !

     

    Now the code works almost great. The problem is now that the light will lit exactly 5min. and pauses exactly for 5 minutes. Is there still something wrong with the random timings ?

    • 0
    Posted (edited)

    Ah! sorry! u rigth!

     

    prev. post have updated!

     

     

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    pls do not spam me to PM

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Edited by 10der
    • 0
  • Inquirer
  • Posted

    Hi,

     

    thank you very very much ! I am really appreciated for your help.

     

    Now the code works perfectly :D

     

     

    • 0
    Posted

    welcome!

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