datetime.now().time() + 10 Minutes...

Oliver Lenz shared this thanks 15 months ago

I've created a Rule to Switch on a Bulp when there is a Motion-Event and

( timeof(now) >= timeof(Sunset)) OR ( timeof(now) <= timeof(Sunrise) ).


That works fine; but I want to use the light 10 Minutes before Sunset and 10 Minutes after Sunrise.

Is there an ability to interact "now" plus/minus 10 minutes?

I tried

( timeof( now ) >= timeof(Sunset) - timeof(1980 00:10) ) OR ( timeof( now ) <= timeof(Sunrise) + timeof(1980 00:10) ) and( timeof( now ) >= timeof(Sunset) - hour(00:10) ) OR ( timeof( now ) <= timeof(Sunrise) + hour(00:10) ) and much more -

but i only get an error for a wrong object assignment.

Comments (20)

photo
2

If you are using a virtual weather station, you can use the scheduler.

There is an option (Astro offset) which controls the time difference.

In your case you have to check your motion sensor and then check the scheduler.

Check my attachment.

photo
1

it's not exact what i need - but so i am able to set a virtual switch or a variable as condition for rules.

Thanks for the idea.

photo
1

You can use a rule based on sunset - 10 mn to set a variable and check for that variable in your rule

photo
1

This another of Zipato's severe limitations - that you can't do calculations with date/time values or create time stamps. Actually, the problem with trying to store the date-time values from date of (now) is that the number exceeds the capacity of variables and meter attributes.


This is why I ended up creating a date-time meter where I maintain a numeric value for time (in seconds) and date-time (in hours, including fractions of hours - this is the maximum resolution that can be stored). These values can be regularly refreshed from the Zipabox's own date/time or from external sources. These values can be stored at time stamps, compared with each other, added/subtracted, etc. VERY, VERY useful. I have date or time stamps for sunset/sunrise, for the last time I received updates from sensors, for the time when peak and off-peak electricity charges kick in and for the start of each month of the year, and much more. I use those values to calculate how many minutes old a sensor reading is, how many hours of off-peak electricity remain, how long since/until sunset/sunrise, etc.


https://community.zipato.com/topic/a-method-for-creating-manipulable-time-and-date-values


Once you've made the effort to set it up (it's not that hard), you'll find it invaluable.

photo
1

I can provide some updated rule screenshots if you want. I have doubtless fixed and tweaked things since then.

photo
1

David, yes - please provide some updated screenshots! -This is very useful for many, including myself :-)

photo
1

I create a variable that is the standard linux/epoch. I have devices that post their current epoch to virtual meters and I compare Zipato's epoch value to the reported value to get alerted when those devices do not report-in for a certain amount of time.

/977f27b6c0f2da8565d2a09535dee0c7

photo
1

Vernon - if that's a standard Unix value (is it seconds since 1/1/1970?), I think you'll find that Zipabox's variable can't handle a number of that size. I've discussed this at length on other threads.

photo
1

Interesting, I have not had an issue yet with this (knock on wood) and it has been working for a year or so now. Here is a value I pulled this morning

/0506a5cb5cd8e8bc68099f29027316e9

photo
1

It may be that the problem only arises when trying to copy values from a (time of) piece. If you calculate the values yourself perhaps it works - although I think I tested that and it didn't work.


The NCurrentEpoch value doesn't take account of leap years, does it?

photo
1

Is it a Zipatile or a Zipabox? Perhaps there's a difference.

photo
1

I just tried it and you can indeed store large numbers if you calculate them yourself. Unfortunately, any attempt to store Zipabox date-time values in variables/meters fails (although, oddly, you can send the variable to yourself in an email, and see that they are Unix dates under the hood. But all calculations and comparisons fail).


But unless you populate the values from a date-time value obtained from outside, you'd have to do some fiddly calculations (for the Zipabox) to get around the leap-year problem, right?

photo
photo
1

OK. First, this is the code I use in all my Google App Scripts to update the date-time and time values. I run these scripts to fetch information from weather APIs, etc., and keep the date/time values up-to-date as a side benefit. The time value could actually be worked out within a Zipabox rule by just adding up the seconds using (Part of) pieces, but there's no way to do that with date-times values.


// Get current date/time
var dateNow = new Date();
var nYear = dateNow.getFullYear();
var nMonth = dateNow.getMonth() + 1;
var nDay = dateNow.getDate();
var nHour = dateNow.getHours();
var nMinute = dateNow.getMinutes();


// Create date-time value for the Zipabox (number of hours since 1 January 1970)
var nZipaboxDateTime = dateNow.valueOf() / 1000 / 60 / 60;

// Create time value (hours, minutes and seconds expressed in seconds; don't calculate seconds since midnight because it will be wrong when the clocks go forward/back!)
var nZipaboxTimeValue = (dateNow.getHours() * 3600) + (dateNow.getMinutes() * 60) + dateNow.getSeconds();

// Create values for 1st February and 1st March this year. Send these to the date-time meter so we can work out values for the start of all months
var dateFeb1st = new Date(nYear, 1 /*zero-based*/, 1, 0, 0, 0);
var dateMar1st = new Date(nYear, 2 /*zero-based*/, 1, 0, 0, 0);
var nZipaboxDateTimeFeb1st = dateFeb1st.valueOf() / 1000 / 60 / 60;
var nZipaboxDateTimeMar1st = dateMar1st.valueOf() / 1000 / 60 / 60;

// Send values to Zipabox date-time meter
var resultZipaboxDateTime = UrlFetchApp.fetch(sZIPABOX_REMOTING_PREFIX+sZIP_DATETIMEMETER_SUFFIX+
"&value9=" + parseFloat(nZipaboxDateTime) + "&value1=" + parseInt(nZipaboxTimeValue) + "&value10=" + parseFloat(nZipaboxDateTimeFeb1st) + 
"&value11=" + parseFloat(nZipaboxDateTimeMar1st), options);

This is the date-time meter. It also keeps date-time values for the 1st February and 1st March for the current year - all other months can be worked out from there. This allows you to have conditions such as (if [current date-time] <= 15th February), or work out the total time span between two dates-time values. Like I mentioned, for date-time values, the maximum resolution that Zipabox variables and meter attributes can handle is fractions of hours.

62982927d567bf196171da4647080a55

photo
1

This means that the date-time values are not ultra-precise - but then I doubt anyone needs them to be. Here's a sample from the log.


b7a8e2721066a67471612d13752d7c25


I have a rule that runs every minute, which controlling the scheduling of most other events, and tries to keep the time and date-time values up-to-date. But because of the imprecision, those values get a little out of whack until an update from a Google App Script corrects them. I have three Google App Scripts running, and so an update comes in roughly every three minutes.


94ad8a4d306292b13a9a504ea73f0b2d

photo
1

I create a series of month values using the values for February 1st and March 1st. It's not a perfect system but much better than nothing.


94298a5cf55f9fe26378ec470d32ad4d

It's then possible to test conditions like this (if current date between October 1st and November 25th):


c82a95e0b5d1b0867e28d0745cb1892d

photo
1

You can store and compare time values:


981ce2e9515741e93082b48bd7b81477Including sunset/sunrise times:

6f7abb85322b8fc8670bea56fc34f741and you can store date-time stamps for events such as when the heating was last on, when you last received information from a sensor...


00642621a4465e73f7101a1b8ef4856b

3b2e8088824287789602ce48960ed6e0

photo
1

and then use those stamps to work out time spans, such as the age of the last sensor reading:


ce400b0e3e98a387b7fe9458a9b822e8

photo
1

Actually, when I detect that the box has rebooted, I do recalculate the time value as I explained above. I can't do much about the date-time until I get an update from one of the scripts.


2cde105b9a1cf2db9ba08d6b85a4f07e


I use the update date-time stamps to monitor information sources and warn myself if they go offline for too long.


9c5f932d611db3653a2a7a5644808f96

photo
1

To get sunrise and sunset values, I import date-time values (in fractions of hours format) from multiple sources. I check them, in descending order of reliability, to ensure that there are no nonsensical values in there. I take the most reliable source available.


e82666f04eedc64e8846fbea0bb83cc3

photo
1

I think that's about it.