-
Notifications
You must be signed in to change notification settings - Fork 120
/
javascript.md
2080 lines (1689 loc) · 74.8 KB
/
javascript.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Content
- [Note](#note)
- [Global functions](#global-functions)
- [Best practice](#best-practice)
- [Functions](#following-functions-can-be-used-in-scripts)
- [require - load some module](#require---load-some-module)
- [console - Gives out the message into log](#console---gives-out-the-message-into-log)
- [exec - execute some OS command, like "cp file1 file2"](#exec---execute-some-os-command-like-cp-file1-file2)
- [on - Subscribe on changes or updates of some state](#on---subscribe-on-changes-or-updates-of-some-state)
- [once](#once)
- [subscribe - same as on](#subscribe---same-as-on)
- [unsubscribe](#unsubscribe)
- [getSubscriptions](#getsubscriptions)
- [getFileSubscriptions](#getfilesubscriptions)
- [schedule](#schedule)
- [Time schedule](#time-schedule)
- [Astro-function](#astro-function)
- [scheduleById](#schedulebyid)
- [getSchedules](#getschedules)
- [clearSchedule](#clearschedule)
- [getAttr](#getattr)
- [getAstroDate](#getastrodate)
- [isAstroDay](#isastroday)
- [compareTime](#comparetime)
- [setState](#setstate)
- [setStateAsync](#setstateasync)
- [setStateDelayed](#setstatedelayed)
- [clearStateDelayed](#clearstatedelayed)
- [getStateDelayed](#getstatedelayed)
- [getState](#getstate)
- [getStateAsync](#getstateasync)
- [existsState](#existsState)
- [getObject](#getobject)
- [setObject](#setobject)
- [existsObject](#existsObject)
- [extendObject](#extendobject)
- [deleteObject](#deleteobject)
- [getIdByName](#getidbyname)
- [getEnums](#getenums)
- [createState](#createstate)
- [createStateAsync](#createstateasync)
- [deleteState](#deletestate)
- [deleteStateAsync](#deletestateasync)
- [sendTo](#sendto)
- [sendToAsync](#sendtoasync)
- [sendToHost](#sendtohost)
- [sendToHostAsync](#sendtohostasync)
- [setInterval](#setinterval)
- [clearInterval](#clearinterval)
- [setTimeout](#settimeout)
- [clearTimeout](#cleartimeout)
- [setImmediate](#setImmediate)
- [formatDate](#formatdate)
- [formatTimeDiff](#formattimediff)
- [getDateObject](#getDateObject)
- [formatValue](#formatvalue)
- [adapterSubscribe](#adaptersubscribe)
- [adapterUnsubscribe](#adapterunsubscribe)
- [$ - Selector](#---selector)
- [readFile](#readfile)
- [writeFile](#writefile)
- [delFile](#delFile)
- [renameFile](#renameFile)
- [onFile](#onFile)
- [offFile](#offFile)
- [onStop](#onstop)
- [getHistory](#gethistory)
- [runScript](#runscript)
- [runScriptAsync](#runScriptAsync)
- [startScript](#startscript)
- [startScriptAsync](#startscriptasync)
- [stopScript](#stopscript)
- [stopScriptAsync](#stopScriptAsync)
- [isScriptActive](#isscriptactive)
- [name](#name)
- [instance](#instance)
- [messageTo](#messageto)
- [messageToAsync](#messagetoasync)
- [onMessage](#onmessage)
- [onMessageUnregister](#onmessageunregister)
- [onLog](#onlog)
- [onLogUnregister](#onlogunregister)
- [wait](#wait)
- [sleep](#sleep)
- [httpGet](#httpget)
- [httpPost](#httppost)
- [createTempFile](#createtempfile)
- [registerNotification](#registerNotification)
- [Scripts activity](#scripts-activity)
- [Changelog](#changelog)
## Global functions
You can define the global scripts in the `global` folder.
All global scripts are available on all instances. If a global script is disabled, it will not be used.
Global script will be just prepended to the normal script and compiled, so you cannot share data between scripts via global scripts. Use states for it.
To use global functions in TypeScript, you have to `declare` them first, so the compiler knows about the global functions. Example:
```typescript
// global script:
// ==============
function globalFn(arg: string): void {
// actual implementation
}
// normal script:
// ==============
declare function globalFn(arg: string): void;
// use as normal:
globalFn('test');
```
#### Best practice:
Create two instances of javascript adapter: one "test" and one "production".
After the script is tested in the "test" instance, it can be moved to "production". By that you can restart the "test" instance as you want.
## The following functions can be used in scripts:
### require - load some module
```js
const mod = require('module_name');
```
The following modules are preloaded: `node:dgram`, `node:crypto`, `node:dns`, `node:events`, `node:fs`, `node:http`, `node:https`, `node:http2`, `node:net`, `node:os`, `node:path`, `node:util`, `node:stream`, `node:zlib`, `suncalc2`, `axios`, `wake_on_lan`, `request` (deprecated)
To use other modules, enter the name (and version) of the module in the instance configuration. ioBroker will install the module. You can require and use it in your scripts afterwards.
### console - Gives out the message into log
Usage is the same as in `javascript`
### exec - execute some OS command, like `cp file1 file2`
```js
exec(cmd, [options], callback);
```
Execute system command and get the outputs.
```js
// Get the list of files and directories in /var/log
exec('ls /var/log', (error, stdout, stderr) => {
log('stdout: ' + stdout);
});
```
Node.js uses /bin/sh to execute commands. If you want to use another shell, you can use the option object as described in the [Node.js documentation](https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback) for child_process.exec.
It is the best practice to always use fill path names to commands to make sure the right command is executed.
**Notice:** you must enable *Enable command "setObject"* option to call it.
### on - Subscribe on changes or updates of some state
```js
on(pattern, callbackOrId, value);
```
The callback function will return the object as parameter with the following content:
```js
{
id: 'javascript.0.myplayer',
state: {
val: 'new state',
ts: 1416149118,
ack: true,
lc: 1416149118,
from: 'system.adapter.sonos.0'
},
oldState: {
val: 'old state',
ts: 1416148233,
ack: true,
lc: 1416145154,
from: 'system.adapter.sonos.0'
}
}
```
**Note:** `state` was previously called `newState`. That is still working.
Example:
```js
let timer;
// Create state "javascript.0.counter"
createState('counter', 0);
// On change
on('adapter.0.device.channel.sensor', (data) => {
// But not ofter than 30 seconds
if (!timer) {
timer = setTimeout(() => {
timer = null;
}, 30000);
// Set acknowledged value
setState('counter', 1 + getState('counter'), true);
// Or to set unacknowledged command
setState('adapter.0.device.channel.actor', true);
}
});
```
You can use the following parameters to specify the trigger:
| parameter | type/value | description |
|-------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| logic | string | "and" or "or" logic to combine the conditions \(default: "and"\) |
| | | |
| id | string | id is equal to given one |
| | RegExp | id matched to regular expression |
| | Array | id matched to a list of allowed IDs |
| | | |
| name | string | name is equal to given one |
| | RegExp | name matched to regular expression |
| | Array | name matched to a list of allowed names |
| | | |
| change | string | "eq", "ne", "gt", "ge", "lt", "le", "any" |
| | "eq" | (equal) New value must be equal to old one (state.val == oldState.val) |
| | "ne" | (not equal) New value must be not equal to the old one (state.val != oldState.val) **If pattern is id-string this value is used by default** |
| | "gt" | (greater) New value must be greater than old value (state.val > oldState.val) |
| | "ge" | (greater or equal) New value must be greater or equal to old one (state.val >= oldState.val) |
| | "lt" | (smaller) New value must be smaller than old one (state.val < oldState.val) |
| | "le" | (smaller or equal) New value must be smaller or equal to old value (state.val <= oldState.val) |
| | "any" | Trigger will be raised if just the new value comes |
| | | |
| val | mixed | New value must be equal to given one |
| valNe | mixed | New value must be not equal to given one |
| valGt | mixed | New value must be greater than given one |
| valGe | mixed | New value must be greater or equal to given one |
| valLt | mixed | New value must be smaller than given one |
| valLe | mixed | New value must be smaller or equal to given one |
| | | |
| ack | boolean | Acknowledged state of new value is equal to given one |
| q | number | Quality code state of new value is equal to given one. You can use '*' for matching to any code. **If not provided q = 0 is set as pattern!** |
| | | |
| oldVal | mixed | Previous value must be equal to given one |
| oldValNe | mixed | Previous value must be not equal to given one |
| oldValGt | mixed | Previous value must be greater than given one |
| oldValGe | mixed | Previous value must be greater or equal to given one |
| oldValLt | mixed | Previous value must be smaller than given one |
| oldValLe | mixed | Previous value must be smaller or equal to given one |
| | | |
| oldAck | bool | Acknowledged state of previous value is equal to given one |
| oldQ | number | Quality code state of previous value is equal to given one. You can use '*' for matching to any code |
| | | |
| ts | string | New value time stamp must be equal to given one (state.ts == ts) |
| tsGt | string | New value time stamp must be not equal to the given one (state.ts != ts) |
| tsGe | string | New value time stamp must be greater than given value (state.ts > ts) |
| tsLt | string | New value time stamp must be greater or equal to given one (state.ts >= ts) |
| tsLe | string | New value time stamp must be smaller than given one (state.ts < ts) |
| | | |
| oldTs | string | Previous time stamp must be equal to given one (oldState.ts == ts) |
| oldTsGt | string | Previous time stamp must be not equal to the given one (oldState.ts != ts) |
| oldTsGe | string | Previous time stamp must be greater than given value (oldState.ts > ts) |
| oldTsLt | string | Previous time stamp must be greater or equal to given one (oldState.ts >= ts) |
| oldTsLe | string | Previous time stamp must be smaller than given one (oldState.ts < ts) |
| | | |
| lc | string | Last change time stamp must be equal to given one (state.lc == lc) |
| lcGt | string | Last change time stamp must be not equal to the given one (state.lc != lc) |
| lcGe | string | Last change time stamp must be greater than given value (state.lc > lc) |
| lcLt | string | Last change time stamp must be greater or equal to given one (state.lc >= lc) |
| lcLe | string | Last change time stamp must be smaller than given one (state.lc < lc) |
| | | |
| oldLc | string | Previous last change time stamp must be equal to given one (oldState.lc == lc) |
| oldLcGt | string | Previous last change time stamp must be not equal to the given one (oldState.lc != lc) |
| oldLcGe | string | Previous last change time stamp must be greater than given value (oldState.lc > lc) |
| oldLcLt | string | Previous last change time stamp must be greater or equal to given one (oldState.lc >= lc) |
| oldLcLe | string | Previous last change time stamp must be smaller than given one (oldState.lc < lc) |
| | | |
| channelId | string | Channel ID must be equal to given one |
| | RegExp | Channel ID matched to regular expression |
| | Array | Channel ID matched to a list of allowed channel IDs |
| | | |
| channelName | string | Channel name must be equal to given one |
| | RegExp | Channel name matched to regular expression |
| | Array | Channel name matched to a list of allowed channel names |
| | | |
| deviceId | string | Device ID must be equal to given one |
| | RegExp | Device ID matched to regular expression |
| | Array | Device ID matched to a list of allowed device IDs |
| | | |
| deviceName | string | Device name must be equal to given one |
| | RegExp | Device name matched to regular expression |
| | Array | Device name matched to a list of allowed device names |
| | | |
| enumId | string | State belongs to given enum |
| | RegExp | One enum ID of the state satisfies the given regular expression |
| | Array | One enum ID of the state is in the given list of enum IDs |
| | | |
| enumName | string | State belongs to given enum |
| | RegExp | One enum name of the state satisfies the given regular expression |
| | Array | One enum name of the state is in the given list of enum names |
| | | |
| from | string | New value is from defined adapter |
| | RegExp | New value is from an adapter that matches the regular expression |
| | Array | New value is from an adapter that appears in the given list of allowed adapters |
| | | |
| fromNe | string | New value is not from defined adapter |
| | RegExp | New value is not from an adapter that matches the regular expression |
| | Array | New value is not from an adapter that appears in the given list of forbidden adapters |
| | | |
| oldFrom | string | Old value is from defined adapter |
| | RegExp | Old value is from an adapter that matches the regular expression |
| | Array | Old value is from an adapter that appears in the given list of allowed adapters |
| | | |
| oldFromNe | string | Old value is not from defined adapter |
| | RegExp | Old value is not from an adapter that matches the regular expression |
| | Array | Old value is not from an adapter that appears in the given list of forbidden adapters |
Examples:
Trigger on all states with ID `'*.STATE'` if they are acknowledged and have new value `true`.
```js
{
"id": /\.STATE$/,
"val": true,
"ack": true,
"logic": "and"
}
```
**Note:** you can use RegExp directly:
```js
on(/^system\.adapter\..*\.\d+\.memRss$/, function (obj) {
});
// same as
on({id: /^system\.adapter\..*\.\d+\.memRss$/, change: "ne"}, function (obj) {
});
```
To simply connect two states with each other, write:
```js
on('stateId1', 'stateId2');
```
All changes of *stateId1* will be written to *stateId2*.
If the `value` parameter is set in combination with state id as the second parameter, on any change the state will filled with the `value`.
```js
on('stateId1', 'stateId2', 'triggered');
setState('stateId1', 'new value');
// stateId2 will be set to 'triggered'.
```
Function `on` returns handler back. This handler can be used by unsubscribing.
*Notice:* By default only states with quality 0x00 will be passed to callback function. If you want to get all events, add `{q: '*'}` to pattern structure.
*Notice:* Please note, that by default "change" is equal to "any", except when only id as string is set (like `on('id', () => {});`). In last case change will be set to "ne".
*Notice:* If you want to also get state deletions/expires as trigger, you need to use change with `ne` or `any` AND q with `*` as filter!
*Notice:* from 4.3.2 it is possible to write a type of trigger as second parameter: `on('my.id.0', 'any', obj => log(obj.state.val));`
### once
Registers a one-time subscription which automatically unsubscribes after the first invocation. Same as [on](#on---subscribe-on-changes-or-updates-of-some-state), but just executed once.
```js
once(pattern, callback);
```
### subscribe - same as **[on](#on---subscribe-on-changes-or-updates-of-some-state)**
### unsubscribe
```js
unsubscribe(id);
// or
unsubscribe(handler);
```
Remove all subscriptions for given object ID or for given handler.
```js
// By handler
let mySubscription = on({ id: 'javascript.0.myState', change: 'any' }, (data) => {
// unsubscribe after first trigger
if (unsubscribe(mySubscription)) {
log('Subscription deleted');
}
});
// by Object ID
on({ id: 'javascript.0.myState1', change: 'ne' }, (data) => {
log('Some event');
});
on({ id: 'javascript.0.myState1', change: 'any' }, (data) => {
// unsubscribe
if (unsubscribe('javascript.0.myState1')) {
log('All subscriptions deleted');
}
});
```
### getSubscriptions
Get the list of subscriptions.
Example of a result:
```js
{
'megad.0.dataPointName': [
{
name : 'script.js.NameOfScript',
pattern : {
id : 'megad.0.dataPointName',
change : 'ne'
}
}
]
}
```
### getFileSubscriptions
Get the list of file subscriptions.
Example of a result:
```js
{
'vis.0$%$main/*': [
{
name : 'script.js.NameOfScript',
id : 'vis.0',
fileNamePattern: 'main/*'
}
]
}
```
### schedule
```js
schedule(pattern, callback);
```
Time scheduler with astro-function.
#### Time schedule
Pattern can be a string with [Cron-Syntax](http://en.wikipedia.org/wiki/Cron), which consists of 5 (without seconds) or 6 (with seconds) digits:
```
* * * * * *
│ │ │ │ │ │
│ │ │ │ │ │
│ │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ │ └────────── month (1 - 12)
│ │ │ └─────────────── day of month (1 - 31)
│ │ └──────────────────── hour (0 - 23)
│ └───────────────────────── min (0 - 59)
└───────────────────────────── [optional] sec (0 - 59)
```
```js
// Example with 5 digits:
schedule('*/2 * * * *', () => {
log('Will be triggered every 2 minutes!');
});
// Example with 6 digits:
schedule('*/3 * * * * *', () => {
log('Will be triggered every 3 seconds!');
});
```
The pattern can also be an object, it is used especially if seconds are required:
the object could have the following properties:
- `second`
- `minute`
- `hour`
- `date`
- `month`
- `year`
- `dayOfWeek`
```js
schedule({ second: [20, 25] }, () => {
log('Will be triggered at xx:xx:20 and xx:xx:25 of every minute!');
});
schedule({ hour: 12, minute: 30 }, () => {
log('Will be triggered at 12:30!');
});
```
Pattern can be a Javascript Date object (some specific time point) - in this case only it will be triggered only one time.
If start or end times for a schedule are needed, this could also be implemented with usage of an object. In this scenario, the object has the properties:
- `start`
- `end`
- `rule`
start and end defines a Date object a DateString or a number of milliseconds since 01 January 1970 00:00:00 UTC.
Rule is a schedule string with [Cron-Syntax](http://en.wikipedia.org/wiki/Cron) or an object:
```js
let startTime = new Date(Date.now() + 5000);
let endTime = new Date(startTime.getTime() + 5000);
schedule({ start: startTime, end: endTime, rule: '*/1 * * * * *' }, () => {
log('It will run after 5 seconds and stop after 10 seconds');
});
```
The rule itself could be also an object:
```js
let today = new Date();
let startTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
let endTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7);
let ruleData = { hour: 12, minute: 30 };
schedule({ start: startTime, end: endTime, rule: ruleData }, () => {
log('Will be triggered at 12:30, starting tomorow, ending in 7 days');
});
```
#### Astro-function
Astro-function can be used via "astro" attribute:
```js
schedule({ astro: 'sunrise' }, () => {
log("Sunrise!");
});
schedule({ astro: 'sunset', shift: 10 }, () => {
log("10 minutes after sunset!");
});
```
The attribute "shift" is the offset in minutes. It can be negative, too, to define time before astro event.
The following values can be used as attribute in astro-function:
- `"sunrise"`: sunrise (top edge of the sun appears on the horizon)
- `"sunriseEnd"`: sunrise ends (bottom edge of the sun touches the horizon)
- `"goldenHourEnd"`: morning golden hour (soft light, the best time for photography) ends
- `"solarNoon"`: solar noon (sun is in the highest position)
- `"goldenHour"`: evening golden hour starts
- `"sunsetStart"`: sunset starts (bottom edge of the sun touches the horizon)
- `"sunset"`: sunset (sun disappears below the horizon, evening civil twilight starts)
- `"dusk"`: dusk (evening nautical twilight starts)
- `"nauticalDusk"`: nautical dusk (evening astronomical twilight starts)
- `"night"`: night starts (dark enough for astronomical observations)
- `"nightEnd"`: night ends (morning astronomical twilight starts)
- `"nauticalDawn"`: nautical dawn (morning nautical twilight starts)
- `"dawn"`: dawn (morning nautical twilight ends, morning civil twilight starts)
- `"nadir"`: nadir (the darkest moment of the night, sun is in the lowest position)
**Note:** to use "astro"-function the "latitude" and "longitude" must be defined in javascript adapter settings.
**Note:** in some places sometimes it could be so that no night/nightEnd exists. Please read [here](https://github.com/mourner/suncalc/issues/70) about it.
**Note:** you can use "on" function for schedule with small modification:
```js
on({ time: '*/2 * * * *' }, () => {
log((new Date()).toString() + " - Will be triggered every 2 minutes!");
});
on({ time: { hour: 12, minute: 30 }}, () => {
log((new Date()).toString() + " - Will be triggered at 12:30!");
});
on({ astro: 'sunset', shift: 10 }, () => {
log((new Date()).toString() + " - 10 minutes after sunset!");
});
```
## scheduleById
```js
scheduleById(id, callback);
scheduleById(id, ack, callback);
```
Allows creating a schedule based on a state value. If the state value changes, the old schedule will be deleted and a new schedule is created automatically.
Supported formats:
- `[h]h:[m]m:ss` (e.g. `12:42:15`, `15:3:12`, `3:10:25`)
- `[h]h:[m]m` (e.g. `13:37`, `9:40`)
```js
scheduleById('0_userdata.0.configurableTimeFormat', () => {
log('Executed!');
});
```
Example: Create state and register schedule on changes:
```js
createState(
'0_userdata.0.myTime',
'00:00:00', // default value
{
type: 'string',
read: true,
write: true
},
() => {
scheduleById('0_userdata.0.myTime', () => {
log('Executed!');
});
}
);
```
### getSchedules
```js
const list = getSchedules(true);
```
Returns the list of all CRON jobs and schedules (except astro).
Argument must be `true` if you want to get the list for **every running script**. Otherwise, only schedules in the current script will be returned.
```js
const list = getSchedules(true);
list.forEach(schedule => log(JSON.stringify(schedule)));
// clear all schedules in all scripts!
list.forEach(schedule => clearSchedule(schedule));
```
Example output:
```
2020-11-01 20:15:19.929 - {"type":"cron","pattern":"0 * * * *","scriptName":"script.js.Heizung","id":"cron_1604258108384_74924"}
2020-11-01 20:15:19.931 - {"type":"schedule","schedule":"{"period":{}}","scriptName":"script.js.Heizung","id":"schedule_19576"}
```
### clearSchedule
If **no** "astro" function is used, you can cancel the schedule later. To allow this, the schedule object must be saved:
```js
let sch = schedule('*/2 * * * *', () => { /* ... */ });
// later:
clearSchedule(sch);
```
### getAttr
```js
getAttr({ attr1: { attr2: 5 } }, 'attr1.attr2');
```
Returns an attribute of the object. Path to attribute can be nested, like in the example.
If the first attribute is string, the function will try to parse the string as JSON string.
### getAstroDate
```js
getAstroDate(pattern, date, offsetMinutes);
```
Returns a javascript Date object for the specified astro-name (e.g. `"sunrise"` or `"sunriseEnd"`). For valid values, see the list of allowed values in the [Astro](#astro--function) section in the *schedule* function.
The returned Date object is calculated for the passed *date*. If no date is provided, the current day is used.
```js
let sunriseEnd = getAstroDate('sunriseEnd');
log(`Sunrise ends today at ${sunriseEnd.toLocaleTimeString()}`);
let today = new Date();
let tomorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
let tomorrowNight = getAstroDate('night', tomorrow);
```
**Note: Depending on your geographical location, there can be cases where e.g. 'night'/'nightEnd' do not exist on certain time points (e.g. locations north in May/June each year!**
You can use webpages like [suncalc.net](http://suncalc.net) to check if the time points are correct.
### isAstroDay
```js
isAstroDay();
```
Returns `true` if the current time is between the astro sunrise and sunset.
### compareTime
```js
compareTime(startTime, endTime, operation, timeToCompare);
```
Compare given time with limits.
If `timeToCompare` is not given, so the actual time will be used.
The following operations are possible:
- `">"` - if given time is greater than `startTime`
- `">="` - if given time is greater or equal to `startTime`
- `"<"` - if given time is less than `startTime`
- `"<="` - if given time is less or equal to `startTime`
- `"=="` - if given time is equal to `startTime`
- `"<>"` - if given time is not equal to `startTime`
- `"between"` - if given time is between `startTime` and `endTime`
- `"not between"` - if given time is not between `startTime` and `endTime`
Time can be Date object or Date with time or just time.
You can use astro-names for the time definition. All 3 parameters can be set as astro time.
Following values are possible: `sunrise`, `sunset`, `sunriseEnd`, `sunsetStart`, `dawn`, `dusk`, `nauticalDawn`, `nauticalDusk`, `nightEnd`, `night`, `goldenHourEnd`, `goldenHour`.
See [Astro](#astro--function) for detail.
```js
log(compareTime('sunsetStart', 'sunsetEnd', 'between') ? 'Now is sunrise' : 'Now is no sunrise');
```
It is possible to define the time with offset too:
```js
log(compareTime({ astro: 'sunsetStart', offset: 30 }, { astro: 'sunrise', offset: -30 }, '>') ? 'Now is at least 30 minutes after sunset' : 'No idea');
```
Structure of an astro object.
```js
{
astro: 'sunsetStart',// mandatory, can be written as string and not as object if offset and date are default
offset: 30, // optional
date: new Date() // optional
}
```
### setState
```js
setState(id, state, ack, callback);
```
*Note*: The following commands are identical
```js
setState('myState', 1, false);
setState('myState', { val: 1, ack: false });
setState('myState', 1);
```
Please refer to https://github.com/ioBroker/ioBroker/wiki/Adapter-Development-Documentation#commands-and-statuses for usage of `ack`.
Short:
- `ack` = `false` : Script wants to send a command to be executed by the target device/adapter
- `ack` = `true` : Command was successfully executed, and state is updated as a positive result
### setStateAsync
```js
await setStateAsync(id, state, ack);
```
Same as setState, but with `promise`.
### setStateDelayed
```js
setStateDelayed(id, state, isAck, delay, clearRunning, callback);
```
Same as setState but with delay in milliseconds. You can clear all running delays for this ID (by default). E.g.
```js
// Switch ON the light in the kitchen in one second
setStateDelayed('Kitchen.Light.Lamp', true, 1000);
// Switch OFF the light in the kitchen in 5 seconds and let first timeout run.
setStateDelayed('Kitchen.Light.Lamp', false, 5000, false, () => {
log('Lamp is OFF');
});
```
This function returns the handler of the timer, and this timer can be individually stopped by clearStateDelayed
### setStateChanged
```js
await setStateChanged(id, state, ack);
```
Same as setState, but set value only if the value is really changed.
### setStateChangedAsync
```js
await setStateChangedAsync(id, state, ack);
```
Same as setStateChanged, but with `promise`.
### clearStateDelayed
```js
clearStateDelayed(id);
```
Clears all delayed tasks for specified state ID or some specific delayed task.
```js
setStateDelayed('Kitchen.Light.Lamp', false, 10000); // Switch OFF the light in the kitchen in ten second
let timer = setStateDelayed('Kitchen.Light.Lamp', true, 5000, false); // Switch ON the light in the kitchen in five second
clearStateDelayed('Kitchen.Light.Lamp', timer); // Nothing will be switched on
clearStateDelayed('Kitchen.Light.Lamp'); // Clear all running delayed tasks for this ID
```
### getStateDelayed
```js
getStateDelayed(id);
```
This is a synchronous call, and you will get the list of all running timers (setStateDelayed) for this id.
You can call this function without id and get timers for all IDs.
In case you call this function for some specific object ID, you will get the following answer:
```js
getStateDelayed('hm-rpc.0.LQE91119.1.STATE');
// returns an array like
[
{ timerId: 1, left: 1123, delay: 5000, val: true, ack: false },
{ timerId: 2, left: 12555, delay: 15000, val: false, ack: false },
]
```
If you ask for all IDs, the answer will look like:
```js
getStateDelayed();
// returns an object like
{
'hm-rpc.0.LQE91119.1.STATE': [
{ timerId: 1, left: 1123, delay: 5000, val: true, ack: false },
{ timerId: 2, left: 12555, delay: 15000, val: false, ack: false },
],
'hm-rpc.0.LQE91119.2.LEVEL': [
{ timerId: 3, left: 5679, delay: 10000, val: 100, ack: false },
],
}
```
- `left` is the time left in milliseconds
- `delay` is the initial delay value in milliseconds
You can ask by timerId directly. In this case, the answer will be:
```js
getStateDelayed(3);
// returns an object like
{ id: 'hm-rpc.0.LQE91119.2.LEVEL', left: 5679, delay: 10000, val: 100, ack: false }
```
### getState
```js
getState(id);
```
Returns state with the given id in the following form:
```js
{
val: value,
ack: true/false,
ts: timestamp,
lc: lastchanged,
from: origin
}
```
If state does not exist, a warning will be printed in the logs and the object `{ val: null, notExist: true }` will be returned.
To suppress the warning, check if the state exists before calling getState (see [existsState](#existsState)).
### getStateAsync
```js
const stateObject = await getStateAsync(id);
```
Same as getState, but with `promise`.
### existsState
```js
existsState(id, (err, isExists) => {});
```
If option "Do not subscribe all states on start" is deactivated, you can use simpler call:
```js
existsState(id)
```
the function returns in this case true or false.
Check if a state exists.
### getObject
```js
getObject(id, enumName);
```
Get description of object id as stored in a system.
You can specify the enumeration name. If this is defined, two additional attributes will be added to result: enumIds and enumNames.
These arrays have all enumerations, where ID is a member of. E.g.:
```js
getObject('adapter.N.objectName', 'rooms');
```
gives back in enumIds all rooms, where the requested object is a member. You can define "true" as enumName to get back *all* enumerations.
### setObject
```js
setObject(id, obj, callback);
```
Write an object into DB. This command can be disabled in adapter's settings. Use this function carefully, while the global settings can be damaged.
You should use it to **modify** an existing object you read beforehand, e.g.:
```js
const obj = getObject('adapter.N.objectName');
obj.native.settings = 1;
setObject('adapter.N.objectName', obj, (err) => {
if (err) log('Cannot write object: ' + err);
});
```
### existsObject
```js
existsObject(id, function (err, isExists) {});
```
If the option "Do not subscribe all states on start" is deactivated, you can use simpler call:
```js
existsObject(id)
```
the function returns in this case true or false.
Check if an object exists.
### extendObject
```js
extendObject(id, obj, callback);
```
It is almost the same as `setObject`, but first it reads the object and tries to merge all settings together.
Use it like this:
```js
// Stop instance
extendObject('system.adapter.sayit.0', {common: {enabled: false}});
```
### deleteObject
```js
deleteObject(id, isRecursive, callback);
```
Delete an object from DB by ID. If the object has type `state`, the state value will be deleted too.
Additional parameter `isRecursive` could be provided, so all children of given ID will be deleted. Very dangerous!
Use it like this:
```js
// Delete state
deleteObject('javascript.0.createdState');
```
*Notice: `isRecursive` option is available only with js-controller >= 2.2.x*
### getIdByName
```js
getIdByName(name, alwaysArray);
```
Returns id of the object with given name.
If there is more than one object with this name, the result will be an array.
If `alwaysArray` flag is set, the result will always be an array if some ID found.
### getEnums
```js
getEnums(enumName);
```
Get the list of existing enumerations with members, like:
```js
getEnums('rooms');
// returns all rooms - e.g.:
[
{
id: 'enum.rooms.LivingRoom',
members: [ 'hm-rpc.0.JEQ0024123.1', 'hm-rpc.0.BidCoS-RF.4' ],
name: 'Living room'
},
{
id: 'enum.rooms.Bath',
members: [ 'hm-rpc.0.JEQ0024124.1', 'hm-rpc.0.BidCoS-RF.5' ],
name: 'Bath'
}
]
getEnums('functions');
// returns all functions - e.g.:
[
{
id: 'enum.functions.light',
members: [
'0_userdata.0.AnotherOne',
'0_userdata.0.MyLigh'
],
name: {
en: 'Light',
ru: 'Свет',
de: 'Licht',
fr: 'Lumière',
it: 'Leggero',
nl: 'Licht',
pl: 'Lekki',
pt: 'Luz',
es: 'Luz',
'zh-cn': '光'
}
}
]
```