This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.
Solved HomeyScript

Ok Homey, What's the Current Day Number?

LyoshaDovolenLyoshaDovolen Member
edited April 2018 in Questions & Help
How to make Homey sent me a push (or tell me when I ask) "the number of the current day"? :)
"Today is 19 of 365" 
https://www.epochconverter.com/daynumbers

Comments

  • Logic +1when 00:00 reset on 31-12-**
  • GeurtDijkerGeurtDijker Member
    edited January 2018
    (Thanks for this nice Challenge....)

    Above is a nice Try, but probably failing when Homey is not online at midnight and without notice one day behind...

    I think the best solution is to let HomeyScript calculate the day on request.
    To use  the calculated number  in a Flow you can use Better Logic.
    (not possible with the build in Logic at the moment, see note.)

    So install HomeyScript and Better Logic on your Homey.
    Create a Better Logic Variable named 'DayOfYear' of type number.
    Create a HomeyScript  at https://homeyscript.athom.com/
    Name it for example getDayOfYear.js 
    Past the script below:
    // getDayOfYear.js 
    // Get the Day of the Year and set it to a BetterLogic Var 
    // first create a numeric variable in BetterLogic with the name below: DayOfYear 
    // Relevant educational material: https://www.youtube.com/watch?v=-5wpm-gesOY
    let BLVarName="DayOfYear"; 
    let testDay = new Date(); 
    Date.prototype.getDayOfYear = function() { let thisYear = new Date(this.getFullYear(),0,1,0,0,0,0); //yyyy,jan=0, 1st day = 1, 0h,0min.,0 Sec.,0 mSec. let today = new Date(this.getFullYear(), this.getMonth(), this.getDate(),0,0,0,0); //today , 0h,0min.,0 Sec.,0 mSec. let aday = 24*60*60*1000; // 24h x Min x Sec x mSec. return Math.round(1+(today - thisYear)/aday); // Varies ~0.04 on non-DST vs DST days
    };
    let dayOfYear = testDay.getDayOfYear(); console.log(dayOfYear); // for HomeyScript Tags/tokens await setTagValue(BLVarName, {type: "number", title: BLVarName}, dayOfYear ); // or for for Better Logic, just use What you want! let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" }); let result = await BLApp.apiPut("/" + BLVarName + "/" + dayOfYear ); return true;

    Create a Flow to test:


    By using it as a Dummy condition (...and...) it will calculate "just is time" the actual day number once again and set the Better Logic Variable for you to use in the Action (...Then)

    Test it: 


    (Although I like BetterLogic, I tried without but setting (Homey) Tags from  HomeyScript is not possible  at the moment. See GH Issue https://github.com/athombv/homey/issues/2171)

    Edit: Math.round for result  Varies ~ 0.04 on non-DST vs DST days
    Edit 2:  updated to use HomeyScript Tags, Better Logic is not necessary anymore. 
  • updated to use HomeyScript Tags (Possible from App version 1.0.4),
    Better Logic is not necessary anymore. 

  • LyoshaDovolenLyoshaDovolen Member
    edited January 2018
    Wow
Sign In or Register to comment.