Sunrize sunset forward
-
@loic13
I found this:
static String SKEY_SUNRISE_TIME
static String SKEY_SUNSET_TIME
How can i retrieve the values and forward it (forward payload?) ?
Thanks -
@loic13 For now there is no way to get sunrise and sunset time on script. I have created a ticket to track this one. I will add support soon for this.
-
Thank you jkandasa.
-
@loic13 This feature introduced on https://github.com/mycontroller-org/mycontroller/issues/322
Example:
JavaScript
var myImports = new JavaImporter(java.io, java.lang, java.util); with(myImports) { var location = mcApi.utils().getServerLocationSettings(); }
Result:
{ "myImports": {}, "location": { "name": "Namakkal", "latitude": "11.2333", "longitude": "78.1667", "sunriseTime": 1481677080000, "sunsetTime": 1481718360000 } }
-
I, Thank you very much, that's perfect.
I'm sorry to ask again, i don't know javascript. How can i access the sunrizeTime and sunsetTime value on my script ?
I manage to send a string using the sendPayload() function but what is the correct way to access sunrizeTime value?:
mcApi.sensor().sendPayload(???????);
Thank you. -
@loic13 You can send sunrise, sunset time as
custom variable
to your node, or any other type as you want.As a first step create a sensor with variables, In this example I am using sensor type as
Custom
andSensorVariable
types asVariable 1
for sunrise andVariable 2
for sunset.
Add these sensorVariables to
UidTag
JavaScript: (Type: Operation)
var myImports = new JavaImporter(java.io, java.lang, java.util); with(myImports) { //Get Sunrise/Sunset details var locSettings = mcApi.utils().getServerLocationSettings(); //Get sensor variables for sunrise and sunset var sensorSunRise = mcApi.uidTag().getByUid("sunrise").getResource(); var sensorSunSet = mcApi.uidTag().getByUid("sunset").getResource(); //Update timezone offset values (by default gives GMT timestamp values) var sunrise = locSettings.sunriseTime + locSettings.timezoneOffset; var sunset = locSettings.sunsetTime + locSettings.timezoneOffset; //Convert milliseconds to seconds and load it in sensor variable value sensorSunRise.value = sunrise / 1000; sensorSunSet.value = sunset / 1000; //Send/Update thise sensorvariable with updated value mcApi.sensor().sendPayload(sensorSunRise); mcApi.sensor().sendPayload(sensorSunSet); }
Note: I have introduced
timezoneOffset
on most recent build. You have to download snapshot build to execute above script. Thank you!You can add this script on Timer, and you specify update this to your node on every day midnight.
-
@jkandasa Thank you very much.
Everything is working.
-
Hi,
I am attempting to use a relay and a PIR switch to turn on the lights when someone approaches the door.
So far, I got it working fine, and I'm using a simple 'Forward Payload' but I now I am looking for a way to have it trigger only at night.
Ideally, this should be done through the controller, and not on the script level. Being lazy, I have created a reusable snippet for all of my relays, and use it for a number of different applications.I tried following the instructions on this post in order to create rules that would allow the payload forwarding only between sunset and sunrise time, but am stuck at the javascript portion, as it gives me an error when I try to run the script: "errorMessage": "TypeError: mcApi.utils().getServerLocationSettings is not a function in <eval> at line number 6".
I could probably accomplish my goal in some other way, by using a photovoltaic cell, or modifying the script for this particular relay to request the time from the controller, but I'd really like to be able to create and use a rule on the controller itself.
Perhaps a revision of your step-by-step instruction on how to accomplish this is in order, as I am sure there are plenty people interested in using the sunrise/sunset times for enabling/disabling triggered actions.
PS. am using MySensors 2.1.0 and MyController 0.0.3.Final-SNAPSHOT
-
I tried following the instructions on this post in order to create rules that would allow the payload forwarding only between sunset and sunrise time, but am stuck at the javascript portion, as it gives me an error when I try to run the script: "errorMessage": "TypeError: mcApi.utils().getServerLocationSettings is not a function in <eval> at line number 6".
SNAPSHOT version changes very often, can you give a try on latest one?
-
@adulovic
Hi,
As jkandasa said, you should probably download the last SNAPSHOT version again. Your error message seems to be because you don't have the SNAPSHOT with his modification.
As for the step-by-step i can try to explain the way i did it:- First, i'm using a Raspberry Pi Zero for the controller with an up-to-date Raspbian distro.
- I have the last 0.0.3.Final-SNAPSHOT of MyController.org(at least the one from 24 days ago with jkandasa's modification).
- I'm using the 2.0.0 version of MySensors (soon to be updated to the last version).
- On MyControler.org i created 2 UID tags (sunrize and sunset) which point to 2 dummy sensors (child of a MySensors node).
- Then i have the java script which send the sunrize and sunset value to this UID tags:
var myImports = new JavaImporter(java.io, java.lang, java.util); with(myImports) { //Get Sunrise/Sunset details var locSettings = mcApi.utils().getServerLocationSettings(); //Get sensor variables for sunrise and sunset var sensorSunRise = mcApi.uidTag().getByUid("sunrize").getSensorVariable(); var sensorSunSet = mcApi.uidTag().getByUid("sunset").getSensorVariable(); //Update timezone offset values (by default gives GMT timestamp values) //sensorSunRise.value = locSettings.sunriseTime + locSettings.timezoneOffset; //sensorSunSet.value = locSettings.sunsetTime + locSettings.timezoneOffset; var dtr = new Date(locSettings.sunriseTime); sensorSunRise.value = dtr.getHours() + ':' + dtr.getMinutes(); var dts = new Date(locSettings.sunsetTime); sensorSunSet.value = dts.getHours() + ':' + dts.getMinutes(); //Send/Update thise sensorvariable with updated value mcApi.sensor().sendPayload(sensorSunRise); mcApi.sensor().sendPayload(sensorSunSet); }
- On MyControler.org i created an Operation to execute the java script
- and a Timer to start the Operation before Sunrize every day.
For your java script you just need to add an if statement comparing the sunrize and sunset values with the current time, and you should be good.