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

run scene based on date variable


Question

Posted

Hi all, would really appreciate any help which can be provided.

Using HC3, I want to store a date as a variable and then at sunset of the following day run a scene.

Was thinking of storing the date as global variable and then I need to run a scene which adds 1 day to the stored date if it matches then run scene.

 

Global Variable Name Is HoildayDate = "11/20/20"

 

I am stuck on how to manipulate the stored value to be able to trigger the scene.

 Thank you

 

Recommended Posts

  • 0
Posted (edited)
2 hours ago, Jay Ess said:

Hi all, would really appreciate any help which can be provided.

Using HC3, I want to store a date as a variable and then at sunset of the following day run a scene.

Was thinking of storing the date as global variable and then I need to run a scene which adds 1 day to the stored date if it matches then run scene.

 

Global Variable Name Is HoildayDate = "11/20/20"

 

I am stuck on how to manipulate the stored value to be able to trigger the scene.

 Thank you

 

Save in global variable timestamp HolidayDate=os.time({year=2020, month=11,day=20})

Now use sunset as a trigger in you scene and verify that the current time is a day after HolidayDate and execute whatever you need.

For example:

if os.time()-HolidayDate>24*3600 and os.time()-HolidayDate<48*3600 then

 --write your code.....

end

Edited by cag014
  • 0
  • Inquirer
  • Posted
    28 minutes ago, cag014 said:

    Save in global variable timestamp HolidayDate=os.time({year=2020, month=11,day=20})

    Now use sunset as a trigger in you scene and verify that the current time is a day after HolidayDate and execute whatever you need.

    For example:

    if os.time()-HolidayDate>24*3600 and os.time()-HolidayDate<48*3600 then

     --write your code.....

    end

    Do you mean that the global variable should be saved as os.time({year=2020, month=11,day=20})? how would i take todays date and save it as that? by running os.date(%..) to get the year month and day? and then add them as a string and then setthe global variable as this?

     

    thank you for you help

    • 0
    Posted
    20 minutes ago, Jay Ess said:

    Do you mean that the global variable should be saved as os.time({year=2020, month=11,day=20})? how would i take todays date and save it as that? by running os.date(%..) to get the year month and day? and then add them as a string and then setthe global variable as this?

     

    thank you for you help

    If you need to store today's date, just use HolidayDate=os.time(). Please do it before sunset.

    • 0
  • Inquirer
  • Posted

    i am confused and as a newbie to lua might be missing the point a bit, allow me to clarify please.

     

    i have a qa which allows me to select one of seven holidays and when i select any holiday it saves the date when pressed as holiday date, under the assumption that we will set the holiday the day before the holiday starts. the seven different holidays have different length of days, so for example holiday 1 is for 2 days. so when selecting holiday one it will set today's date as holidaydate and the holdiaylength as 2. so i have 12/12/20 and 2 as the variables. at 60 minutes after sunset on day 2 i want to switch off holiday mode, so i run a scene which runs 60 minutes after sunset and checks if today is 12/12/20 + 2 is today ie, on the 14/12/20 it should run.

    Just thinking instead of running two variables it might be easier to set holidaydate as the date when holiday mode should go off, so in the qa instead of setting todays date i should set the date of the end of the holiday ie 14/12/20. that might make it easier to check? how do i add two days to todays date in order to then save as the date?

    • 0
    Posted
    17 minutes ago, Jay Ess said:

    i am confused and as a newbie to lua might be missing the point a bit, allow me to clarify please.

     

    i have a qa which allows me to select one of seven holidays and when i select any holiday it saves the date when pressed as holiday date, under the assumption that we will set the holiday the day before the holiday starts. the seven different holidays have different length of days, so for example holiday 1 is for 2 days. so when selecting holiday one it will set today's date as holidaydate and the holdiaylength as 2. so i have 12/12/20 and 2 as the variables. at 60 minutes after sunset on day 2 i want to switch off holiday mode, so i run a scene which runs 60 minutes after sunset and checks if today is 12/12/20 + 2 is today ie, on the 14/12/20 it should run.

    Just thinking instead of running two variables it might be easier to set holidaydate as the date when holiday mode should go off, so in the qa instead of setting todays date i should set the date of the end of the holiday ie 14/12/20. that might make it easier to check? how do i add two days to todays date in order to then save as the date?

    Could you please post the QA

    • 0
  • Inquirer
  • Posted
    this is main os the device which i am trying to build with seven buttons to select the holiday mode we want to set, it is only the very beginning of this qa and will need more help later, the idea is to have the option to easily select different holidays and then run different scripts based on them. i have used the wrong device to build it by using a dimmer module but can fix it if needed. As i am just starting to use HC3 and lua.
     
    local today = os.date("%x")
    local gvDate = "yomtovdate"
    local gvLength = "yomtovlength"
    local gvName = "yomtovname"
     
    function QuickApp:pesach1()
        local length = "2"
        local yomtovname="Pesach I"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
    end
     
    function QuickApp:pesach2()
        local length = "2"
        local yomtovname="Pesach II"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
        
    end
     
    function QuickApp:shavous()
        local length = "2"
        local yomtovname="Shavous"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
     
    end
     
    function QuickApp:rosh()
        local length = "2"
        local yomtovname="Rosh Hashana"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
     
    end
     
    function QuickApp:yom()
        local length = "1"
        local yomtovname="Yom Kippur"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
        
    end
     
    function QuickApp:succos()
        local length = "2"
        local yomtovname="Succos"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
        
    end
     
    function QuickApp:shemini()
        local length = "2"
        local yomtovname="Shemini Atzeres"
        fibaro.setGlobalVariable(gvDate, today)
        fibaro.setGlobalVariable(gvLength, length)
        fibaro.setGlobalVariable(gvName, yomtovname)
        
    end
     
    function QuickApp:onInit()
        self:debug("onInit")
    end
     
     
    • 0
    Posted (edited)

    May I suggest to use calendar REST API... for example

    Please login or register to see this link.

    For more info how configure the request, see

    Please login or register to see this link.

     

    Now in your scene at sunset get this json and check for holiday dates. If today is a day after holiday turn off holiday mode (I hope you've defined holiday profile in HC3)

    By the way using same scene you can set at sunset holiday mode also. (like erev hag)

    No you don't need to set anything manually... fully automated.

     

     

    Edited by cag014
    • 0
  • Inquirer
  • Posted

    that sounds amazing and much better but i have no idea how to use calender rest api, can you guide me please?

     

     

    • 0
  • Inquirer
  • Posted

    i have got this far

     

     
    http = net.HTTPClient
    local address = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&year=2020&month=12"
    http:request(address, {
            options={
                headers = { 
                    Accept = "application/json"
                },
                method = 'GET'
            },
            success = function(response)
                fibaro.debug("Scene15","response status:"..response.status)
                fibaro.debug("Scene15","response:"..response.data)
                local data = json.decode(response.data)  
                fibaro.debug("Scene15","data: "..response.data)
            end,
            error = function(error)
                fibaro.debug("Scene15","error: "..json.encode(error))
            end
        }
     
    it shows me the data in the debug mode but it also shows an error below the data but it does not show what the error is
     
    Thank you so very much for your help with this.

    after a bit of tinkering the error is on this line

    local data = json.decode(response.data)
     
    the code i got from fibaro qa article so might need to be chnaged as am trying to run this from a scene?
    • 0
  • Inquirer
  • Posted

    found this link which has a field "yomtov"=true

    Please login or register to see this link.

    so i suppose i would need to run through the fields returned to check if this is in the data?

     

    • 0
    Posted (edited)
    1 hour ago, Jay Ess said:

    i have got this far

     

     
    http = net.HTTPClient
    local address = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&year=2020&month=12"
    http:request(address, {
            options={
                headers = { 
                    Accept = "application/json"
                },
                method = 'GET'
            },
            success = function(response)
                fibaro.debug("Scene15","response status:"..response.status)
                fibaro.debug("Scene15","response:"..response.data)
                local data = json.decode(response.data)  
                fibaro.debug("Scene15","data: "..response.data)
            end,
            error = function(error)
                fibaro.debug("Scene15","error: "..json.encode(error))
            end
        }
     
    it shows me the data in the debug mode but it also shows an error below the data but it does not show what the error is
     
    Thank you so very much for your help with this.

    after a bit of tinkering the error is on this line

    local data = json.decode(response.data)
     
    the code i got from fibaro qa article so might need to be chnaged as am trying to run this from a scene?

    the code is OK... but the error is in

     fibaro.debug("Scene15","response status:"..response.status)

     fibaro.debug("Scene15","response:"..response.data)

    You need to decode the response before and not after

     

    local data = json.decode(response.data) 

     fibaro.debug("Scene15","response status:"..response.status)
    fibaro.debug("Scene15","response:"..json.encode(response.data))  --- response.data is a table
     
    Please update you code as shown above

     

    Edited by cag014
    • 0
  • Inquirer
  • Posted
    2 hours ago, cag014 said:

    the code is OK... but the error is in

     fibaro.debug("Scene15","response status:"..response.status)

     fibaro.debug("Scene15","response:"..response.data)

    You need to decode the response before and not after

     

    local data = json.decode(response.data) 

     fibaro.debug("Scene15","response status:"..response.status)
    fibaro.debug("Scene15","response:"..json.encode(response.data))  --- response.data is a table
     
    Please update you code as shown above

     

    by putting in this line

     
    http = net.HTTPClient
    --local address = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&year=2021&month=03"
    local address ="https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&min=off&mod=off&nx=off&year=2021&month=x&ss=off&mf=off&c=off&geo=geoname&geonameid=3448439&M=on&s=off"
     
    http:request(address, {
            options={
                headers = { 
                    Accept = "application/json"
                },
                method = 'GET'
            },
            success = function(response)
                fibaro.debug("Scene15","response status:"..response.status)
                local data = json.decode(response.data) 
     
                --fibaro.debug("Scene15", json.encode(response.data))
                
                --local data = json.decode(response)
                
                fibaro.debug("Scene15", data)
            end,
            error = function(error)
                --fibaro.debug("Scene15","error: "..json.encode(error))
            end
        }
       

    i get an error on the debug window as follows:

     

    [20.12.2020] [19:50:12] [DEBUG] [SCENE15]: response status:200[20.12.2020]

    [19:50:12] [ERROR] [SCENE15]:

     

    • 0
    Posted

    Shalom @Jay Ess,

    Try this snippet:

    Please login or register to see this code.

    I was just playing a bit that http call :-D The code is only example...

    Below the part of debug window:

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: response status:200

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Hebcal: Hebcal Diaspora 2020

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Location {"geo":"none"}

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Asara B'Tevet 2020-01-07

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Sh'vat 2020-01-27

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Tu BiShvat 2020-02-10

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Shabbat Shekalim 2020-02-22

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Adar 2020-02-25

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Adar 2020-02-26

    ...... etc.

    Hope it helps. Enjoy coding :-D

     

     

    • 0
  • Inquirer
  • Posted
    9 hours ago, Bodyart said:

    Shalom @Jay Ess,

    Try this snippet:

    Please login or register to see this code.

    I was just playing a bit that http call :-D The code is only example...

    Below the part of debug window:

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: response status:200

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Hebcal: Hebcal Diaspora 2020

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Location {"geo":"none"}

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Asara B'Tevet 2020-01-07

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Sh'vat 2020-01-27

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Tu BiShvat 2020-02-10

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Shabbat Shekalim 2020-02-22

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Adar 2020-02-25

    [21.12.2020] [00:41:06] [DEBUG] [QUICKAPP1060]: Rosh Chodesh Adar 2020-02-26

    ...... etc.

    Hope it helps. Enjoy coding :-D

     

     

    When i run this code i run in to an error and the debug shows me response status:200 and then the next line shows a red error but without anything written beyond it.  this is my code and i have had to comment all the lines in order to get no errors, and am at a loss to know how to continue as i cant seem to run json.decode.

     

    Here is my code as it is with the lines which dont work commented out in order to have no errors and to show what my problem is.

     

    many thanks for any help and contribution.

     

    http = net.HTTPClient
    local year = "2021"
    local month = "03"
    local httpstring = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on"
    local address  = httpstring.."&year="..year.."&month="..month
     
    http:request(address,{options={headers={Accept="application/json"},method='GET'},
            success = function(response)
                print("response status:"..response.status)
                --local cal=json.decode(response.data) 
                --print("Hebcal:", cal.title)
                --for i=1,#cal.items do
                --    print(cal.items[i].memo)
                --    print(cal.items[i].title,cal.items[i].date)
                --end
            end,
            error = function(error)
                --fibaro.debug("error: "..json.encode(error))
            end
        }
    )







     
    • 0
    Posted
    http =net.HTTPClient()
    • 0
  • Inquirer
  • Posted
    http =net.HTTPClient()
    local year = "2021"
    local month = "03"
    local httpstring = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on"
    local address  = httpstring.."&year="..year.."&month="..month
     
    http:request(address,{options={headers={Accept="application/json"},method='GET'},
            success = function(response)
                print("response status:"..response.status)
                local cal=json.decode(response.data) 
                print("Hebcal:", cal.title)
                for i=1,#cal.items do
                    print(cal.items[i].memo)
                    print(cal.items[i].title,cal.items[i].date)
                end
            end,
            error = function(error)
                fibaro.debug("error: "..json.encode(error))
            end
        }
    )
     
    Still getting Error, in the debug window as below any help would be great 
    [21.12.2020] [14:55:37] [DEBUG] [SCENE15]: response status:200
    [21.12.2020] [14:55:37] [ERROR] [SCENE15]:
    • 0
    Posted

    Hi @Jay Ess,

    the above code gives here following debug:

    Please login or register to see this attachment.

    • 0
  • Inquirer
  • Posted

    oh you are running this in a quickapp? I was trying to run this in a scene!

     

    Also i am looking to see if the field 

    Please login or register to see this code.

    Please login or register to see this code.

    • 0
    Posted

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted
    local variableErevChag = fibaro.getGlobalVariable("yomtovdate")
    local variableChag = fibaro.getGlobalVariable("yomtov")
     
    function QuickApp:checkYomTov()
        http =net.HTTPClient()
        --local year = "2021"
        --local month = "03"
        --local day = "28"
        
        local year = os.date("%Y")
        local month = os.date("%m")
        local day = os.date("%d")
        local checkdate = year.."-"..month.."-"..day
        
        local httpstring = "https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on"
        --local httpstring = "https://www.hebcal.com/shabbat?cfg=json&geonameid=3448439"
        local address  = httpstring.."&year="..year.."&month="..month
        --local address  = httpstring.."&gy="..year.."&gm="..month.."&gd="..day
        
        http:request(address,{options={headers={Accept="application/json"},method='GET'},
                success = function(response)
                    --print("response status:"..response.status)
                    local cal=json.decode(response.data) 
                    --print("Hebcal:", cal.title)
                    for i=1,#cal.items do
                        if cal.items[i].date==checkdate then
                            --print(cal.items[i].memo)
                            print(cal.items[i].date)
                            print(cal.items[i].yomtov)
                            --print(cal.items[i].title,cal.items[i].date)
                        end
                    end
                end,
                error = function(error)
                    fibaro.debug("error: "..json.encode(error))
                end
            }
        )
    end
        
     
    function QuickApp:onInit()
        self:debug("onInit")
        self:checkYomTov()
    end
     
     
    a quick question how do i check for tomorrow's date please so need to add 1 todayes date in order to check if tomorrow is yomtov true too?

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