The “when it’s dark” part is the challenge. We can’t count on them having a sensor and the calculations to do fake it based on solar radiation and cloudiness, while not super complex, are probably more complex than appropriate. See Virtual Solar Light Sensor [4.0.0.0;4.9.9.9].
If we just mean based on sunset, that’s pretty easy. But that doesn’t need a script to accomplish. That’s just a simple UI rule with no code, blockly or otherwise.
If we just want something like that, then turning on a light at sunset and off at a fixed time would be the way to go. It could either be two rules:
configuration: {}
triggers:
- id: "1"
configuration:
thingUID: astro:sun:local
event: START
channelUID: astro:sun:local:astroDusk#event
type: core.ChannelEventTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
itemName: FrontPorchLight
command: ON
type: core.ItemCommandAction
and
configuration: {}
triggers:
- id: "1"
configuration:
time: 23:00
type: timer.TimeOfDayTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
itemName: FrontPorchLight
command: OFF
type: core.ItemCommandAction
Or if we want to have some blockly involved combine the two into one:
configuration: {}
triggers:
- id: "1"
configuration:
thingUID: astro:sun:local
event: START
channelUID: astro:sun:local:astroDusk#event
type: core.ChannelEventTrigger
- id: "2"
configuration:
time: 23:00
type: timer.TimeOfDayTrigger
conditions: []
actions:
- inputs: {}
id: "3"
configuration:
blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block type="controls_if" id="3bp[qw/{m,Rsbex1V~md" x="139" y="117"><mutation else="1"></mutation><value name="IF0"><block type="oh_zdt_compare" id="F~h[,td/kaN)$wd4KbuG"><field name="operation">before</field><field name="dateComparison">time</field><field name="precision">seconds</field><value name="zdtOne"><shadow type="oh_zdt" id="^VH~6bGwM-mpgL_e8uRZ"><field name="day">2024-02-22</field></shadow><block type="oh_zdt_now" id="]d|p2g~Gz^%PgID}tZ~0"></block></value><value name="zdtTwo"><shadow type="oh_zdt" id="Xl8K=wia.q:*U0xLZIgA"><field name="day">2024-02-22</field></shadow><block type="oh_zdt_fromText" id="F/3AZDDZ/-ISY.[FSf@9"><value name="day"><shadow type="text" id="(Kyt,C9PmeAUsX`nH;Hf"><field name="TEXT">2024-02-22</field></shadow><block type="text" id="^0P2`r8$|L{Fm]7R6g-Z"><field name="TEXT">23:01</field></block></value></block></value></block></value><statement name="DO0"><block type="oh_event" id="/jQuOyN4I6JUzFP]9CiB"><field name="eventType">sendCommand</field><value name="value"><shadow type="text" id="AxIc(+~PG|i*Y$DZy$K*"><field name="TEXT">value</field></shadow><block type="text" id="-P[E%.Be-tG)XR(=C,Mi"><field name="TEXT">ON</field></block></value><value name="itemName"><shadow type="oh_item" id=")5YgIY_^jwndUnvwSW|J"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="sA_8`hv3ME++|m|5*2%/"><mutation itemName="FrontPorchLight" itemLabel="Front Porch Light Switch"></mutation><field name="itemName">Front Porch Light Switch</field></block></value></block></statement><statement name="ELSE"><block type="oh_event" id="Dyi0=8vs{6-d`MJ9!p8["><field name="eventType">sendCommand</field><value name="value"><shadow type="text" id="AxIc(+~PG|i*Y$DZy$K*"><field name="TEXT">value</field></shadow><block type="text" id="cR$5aK@ux@Kfwa3YJ:o]"><field name="TEXT">OFF</field></block></value><value name="itemName"><shadow type="oh_item" id=")5YgIY_^jwndUnvwSW|J"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="B[}IaV1Lk}BRgsHjD85F"><mutation itemName="FrontPorchLight" itemLabel="Front Porch Light Switch"></mutation><field name="itemName">Front Porch Light Switch</field></block></value></block></statement></block></xml>
type: application/javascript
script: >
// graalVM
function zdtCompare(zdt1, zdt2, compareOp, precision, compDate) {
switch (precision) {
case 'years':
zdt2 = zdt2.withMonth(zdt1.monthValue());
case 'months':
zdt2 = zdt2.withDayOfMonth(zdt1.dayOfMonth());
case 'days':
zdt2 = zdt2.withHour(zdt1.hour());
case 'hours':
zdt2 = zdt2.withMinute(zdt1.minute());
case 'minutes':
zdt2 = zdt2.withSecond(zdt1.second());
case 'seconds':
zdt2 = zdt2.withNano(zdt1.nano());
}
if (compDate === 'date') {
zdt1 = zdt1.toLocalDate();
zdt2 = zdt2.toLocalDate();
} else if (compDate === 'time') {
zdt1 = zdt1.toLocalTime();
zdt2 = zdt2.toLocalTime();
}
switch (compareOp) {
case 'before':
return zdt1.isBefore(zdt2);
case 'equal':
return zdt1.equals(zdt2);
case 'after':
return zdt1.isAfter(zdt2);
case 'beforeEqual':
return zdt1.isBefore(zdt2) || zdt1.equals(zdt2);
case 'afterEqual':
return zdt1.isAfter(zdt2) || zdt1.equals(zdt2);
}
}
if (zdtCompare((time.ZonedDateTime.now()), (time.toZDT('23:01')), 'before', 'seconds', 'time')) {
items.getItem('FrontPorchLight').sendCommand('ON');
} else {
items.getItem('FrontPorchLight').sendCommand('OFF');
}
type: script.ScriptAction
Blockly:
Either can be made into a rule template. However, rule templates cannot change the rule’s trigger through a parameter so the trigger at 23:00 can’t be a parameter.