I’ve had the chance to do some more testing. Describing it as “not initalising” was the wrong way to describe it. All the rules go to an IDLE state, and I couldn’t see any issues in the logs.
I had some head scratching yesterday trying to gather more info, the scripts were working normally upon a warm reboot of the system, after days of not working properly upon cold boots. On a hunch, I left it overnight and tried again this morning, and surely enough the scripts go back to not working properly
I realised that the affected scripts were actually running, but the timer functions were not working properly. I’ve attached the code below.
Basically there are a couple of items which are motion sensor states (a boolean value). The script commands an item (also a bool value) so that it sets to 1 if any of the inputs are 1, or set
to 0 after a timeout period once all the inputs go to zero (a falling edge trigger essentially).
The timeout period is set to 5 seconds in this example for testing purposes. The timeout value is stored in an item called global_motion_sensor_timeout_01, which is read every time the script is run.
When the scripts are malfunctioning, it’s as if the timeout period is set to 0, ie the output goes to 0 as soon as all inputs go to 0.
I double checked that the value global_motion_sensor_timeout_01 was indeed 5 (I even manually set it to other values just to refresh it), but the script still wouldn’t operate normally. As mentioned, if I click save to update the rule, it starts working normally again.
I have other similar scripts exhibiting the same behaviour.
To summarise:
- Rule / Inline Script working perfectly for months
- Update to OH 4.3.2-1
- Script works fine on warm reboot but not cold boot
- The rules / inline scripts are running, but the timer functions within the script aren’t working.
- The timer functions are timing out immediately rather than waiting for 5 seconds
- When script isn’t working properly, saving/updating the rule makes it work fine again immediately.
One thing that comes to mind in a cold boot vs warm reboot is that the system time is going to very out of sync by 12 hours in this case. I’m just throwing ideas out there at this point. I don’t know how they may interact with the timer functions
var output_cmd_items_0, input_items_numbers, input_items_sw, sum_of_inputs, input_idx_num, input_idx_sw, input_temp_val;
output_cmd_items_0 = 'int_00_led_controller_ch_0_auto_motion_state';
input_items_numbers = ['int_01_led_controller_input_14', 'int_01_led_controller_input_15'];
input_items_sw = [];
sum_of_inputs = 0;
if (!!input_items_numbers.length) {
for (var input_idx_num_index in input_items_numbers) {
input_idx_num = input_items_numbers[input_idx_num_index];
sum_of_inputs = sum_of_inputs | parseFloat(items.getItem(input_idx_num).state);
}
}
if (!!input_items_sw.length) {
for (var input_idx_sw_index in input_items_sw) {
input_idx_sw = input_items_sw[input_idx_sw_index];
if (items.getItem(input_idx_sw).state == 'ON') {
input_temp_val = 1;
} else {
input_temp_val = 0;
}
sum_of_inputs = sum_of_inputs | input_temp_val;
}
}
if ((cache.private.get('previous_command')) == 0 && sum_of_inputs == 1) {
if (cache.private.exists('motion_timer')) { cache.private.remove('motion_timer').cancel(); };
items.getItem(output_cmd_items_0).sendCommand(1);
} else if ((cache.private.get('previous_command')) == 1 && sum_of_inputs == 0) {
if (cache.private.exists('motion_timer') === false || cache.private.get('motion_timer').hasTerminated()) {
cache.private.put('motion_timer', actions.ScriptExecution.createTimer('motion_timer', time.ZonedDateTime.now().plusSeconds(parseFloat(items.getItem('global_motion_sensor_timeout_01').state)), function (timer_context) {
items.getItem(output_cmd_items_0).sendCommand(0);
}, undefined));
} else {
cache.private.get('motion_timer').reschedule(time.ZonedDateTime.now().plusSeconds(parseFloat(items.getItem('global_motion_sensor_timeout_01').state)));
};
} else if (sum_of_inputs == 1) {
if (cache.private.exists('motion_timer')) { cache.private.remove('motion_timer').cancel(); };
items.getItem(output_cmd_items_0).sendCommand(1);
}
cache.private.put('previous_command', sum_of_inputs);