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.

How to calculate a duration

I would like to calculate how long a window was open with the use of a contact sensor. When the windows opens it should store the time in a (time)variable. When the windows closes it should calculate the duration (closing time minus the start time). 
Anybody any Idea? Thanks in advance.

Comments

  • bvdbosbvdbos Member
    edited November 2017
    With Better Logic: http://mathjs.org/examples/units.js.html
    math.eval(460 V * 20 A * 30 days to kWh)should give: 6624 kWh
    so you might build further on this?
  • Yes, but now the time (seconds since 1970).
  • That's really difficult. I've tried to calculate the time with "hours" and "minutes". I get a problem at midnight. An app like countdown could help you, or better an app that could count up.
    When using the countdown app, use a very high counter. Then you could count the time.
  • Hi, author of CountDown here. It is not easy to change the CountDown app to count up, in its design each counter is counted down ;-) 

    You could start indeed with a very high counter and then retrieve (with the global tags) the remaining seconds and then do some calculations. 

    I could make a 'CountUp' variant as a separate app, that shouldn't be too hard ( at least much easier than change the current countdown app design).  Would this something more than a handful of people are interested in? 
  • aloftaloft Member
    edited November 2017
    For the daring people: https://github.com/ralfvd/nl.bevlogenheid.countup ( very experimental ! )  ; feedback appreciated if this is a tool that warrants more serious development than basically changing '-1' to '+1' ;-)
  • aloft said:
    Hi, author of CountDown here. It is not easy to change the CountDown app to count up, in its design each counter is counted down ;-) 

    You could start indeed with a very high counter and then retrieve (with the global tags) the remaining seconds and then do some calculations. 

    I could make a 'CountUp' variant as a separate app, that shouldn't be too hard ( at least much easier than change the current countdown app design).  Would this something more than a handful of people are interested in? 
    It would be great to have a count-up timer. If the value can be stored to a variable it would be even better.
  • Why not use $timenow$? thats epoch time, so set a variable when the windows opens, set another when closed and substract the first from the second. That will give you the seconds and can be recalculated to hours and minutes
  • cbhcbh Member
    edited November 2017
    Or:
    Flow 1: Triggerflow
    IF "conditions met" THEN "Set Better Logic variable X = true" AND trigger Flow 2:

    Flow 2: Count seconds
    IF "This flow is triggered" AND "Better Logic variable X = true" THEN "increment Better Logic variable Y with 1" AND "Trigger flow 2" (delayed 1 sec)

    Flow 3: Stopflow
    IF "conditions not met anymore" THEN "Set Better Logic variable X = false"

    Don't know if the precision is like an atomic clock, but for daily use, it might be enough?

  • You probably don't need seconds, this flow gives you window-open minutes in a better-logic variable
  • Just remember something somewhere that resets the variable after the value has been read - otherwise you cummulate the time for all occasions :-)
  • I would not recommend using (timed) recurring flows to detect something that is easily detectable by using flows that are triggered by sensors.
  • Thanks to everyone for the answers. Stupid question maybe: but how can I access variables in for example the "Voice" then card? Are there any global variables? I tried {{$timenow$}} and a self defined better logic variable for example but then they are interpreted literally.
  • @larsmachiels klik the "tags" button on the top of the flow page :wink:
  • caseda said:
    @larsmachiels klik the "tags" button on the top of the flow page :wink:
    Stupid me. Never looked at that. Thanks!
  • larsmachielslarsmachiels Member
    edited November 2017
    Got it. When the window opens I start a count-up timer. When the windows closes I use the value of the count-up timer and divide that by 60 and store the result to a variable (with the better logic math.js card). I stop the count-up counter and I send an e-mail with the result.  

    Everyone thanks again for all the help.

    I BTW use the Xiaomi Zigbee contact sensor which only costs +/- € 10,00 and works perfectly well.

  • aloft said:
    For the daring people: https://github.com/ralfvd/nl.bevlogenheid.countup ( very experimental ! )  ; feedback appreciated if this is a tool that warrants more serious development than basically changing '-1' to '+1' ;-)
    I downloaded the app, installed it manually on my Homey. It works just fine. Thanks.
  • @aloft why did you count-up app never reach app store? I have multiple needs for calculating duration of stuff. Just curious!
Sign In or Register to comment.