So I wrote an autoexec script inspired by miegapele's script. It is possibly overly complicated, but it is more general. It can handle triggering on any day index of any week index of any month, and the daylight saving period can span the middle of the year or cross over the new year (like Australia). Parameters are store in private channels and are typed in only once per parameter.
To be honest, I don't know how to explain how the indexing formulas work. I found a procedure that worked and then I simplified the logic equations. The resulting simplified equations kinda obfuscate whats going in. But, they do calculate the days to go or days past the nth weekday of the month without using modulo operators.
I developed the script on open beken version 1.17.740, but noticed it fails on 1.17.689, which most of my sensors had installed. I am not sure why. When I OTA updated them they worked. I did try checking formulas though, and putting comments on their own line with no white space.
Even if this script is overcooked, maybe it can serve as just another example in the diverse collection of scripts that can run on these open beken devices.
Attached is a derivation of the indexing formulae
To be honest, I don't know how to explain how the indexing formulas work. I found a procedure that worked and then I simplified the logic equations. The resulting simplified equations kinda obfuscate whats going in. But, they do calculate the days to go or days past the nth weekday of the month without using modulo operators.
I developed the script on open beken version 1.17.740, but noticed it fails on 1.17.689, which most of my sensors had installed. I am not sure why. When I OTA updated them they worked. I did try checking formulas though, and putting comments on their own line with no white space.
Even if this script is overcooked, maybe it can serve as just another example in the diverse collection of scripts that can run on these open beken devices.
// ************************************************
// ** GENERIC TEMP HUMIDITY SENSOR WITH LCD + IR **
// ************************************************
// AUTOEXEC.BAT
// **** Setup Base Drivers and TuyaMCU Channels ****
startDriver TuyaMCU
startDriver NTP
setChannelType 1 temperature_div10
linkTuyaMCUOutputToChannel 101 val 1
setChannelType 2 Humidity
linkTuyaMCUOutputToChannel 102 val 2
tuyaMcu_defWiFiState 4 // This setting is required to prevent the TuyaMCU resetting when it receives 'not connected' message when MQTT is offline
// **** Set custom NTP Server address ****
//ntp_setServer 192.168.4.45
// **** Define Daylight Savings Parameters ****
SetChannelType 10 ReadOnly
SetChannelPrivate 10 1
SetChannelLabel 10 "10 Base timezone offset"
SetChannel 10 10
SetChannelType 11 ReadOnly
SetChannelPrivate 11 1
SetChannelLabel 11 "11 Daylight Savings offset" // The difference between DST inactive and DST active
SetChannel 11 1
SetChannelType 20 ReadOnly
SetChannelPrivate 20 1
SetChannelLabel 20 "20 Daylight Savings Start Month"
SetChannel 20 10
SetChannelType 21 ReadOnly
SetChannelPrivate 21 1
SetChannelLabel 21 "21 Daylight Savings Start Week index" // 1st occurance = 1
SetChannel 21 1
SetChannelType 22 ReadOnly
SetChannelPrivate 22 1
SetChannelLabel 22 "22 Daylight Savings Start Day index" // Sunday = 0, Saturday = 6
SetChannel 22 0
SetChannelType 23 ReadOnly
SetChannelPrivate 23 1
SetChannelLabel 23 "23 Daylight Savings Start Hour" // 24hr time
SetChannel 23 2
SetChannelType 30 ReadOnly
SetChannelPrivate 30 1
SetChannelLabel 30 "30 Daylight Savings Stop Month"
SetChannel 30 4
SetChannelType 31 ReadOnly
SetChannelPrivate 31 1
SetChannelLabel 31 "31 Daylight Savings Stop Week index"
SetChannel 31 1
SetChannelType 32 ReadOnly
SetChannelPrivate 32 1
SetChannelLabel 32 "32 Daylight Savings Stop Day index"
SetChannel 32 0
SetChannelType 33 ReadOnly
SetChannelPrivate 33 1
SetChannelLabel 33 "33 Daylight Savings Stop Hour"
SetChannel 33 3
// **** Define Daylight Savings Calculations ****
// Calculations to see where we are reletive to the nth occurance of index day for start
SetChannelPrivate 25 1
SetChannelLabel 25 "25 Days to trigger (DTT) - Start"
SetChannelPrivate 26 1
SetChannelLabel 26 "26 First Ineq Result - Start"
SetChannelPrivate 27 1
SetChannelLabel 27 "27 Second Ineq Result - Start"
alias Run_Strt_inq_1 SetChannel 26 ($CH21*7-7+$day-$mday)<0
alias Run_Strt_inq_2 SetChannel 27 (($CH26+$CH21)*7-6+$day-$mday-$CH22)>0
alias Run_Strt_DTT SetChannel 25 ($CH27-$CH26)*7+$CH22-$day
alias Run_Strt_Day_Logic backlog Run_Strt_inq_1; Run_Strt_inq_2; Run_Strt_DTT
// Calculations to see where we are reletive to the nth occurance of index day for start
SetChannelPrivate 35 1
SetChannelLabel 35 "35 Days to trigger (DTT) - Stop"
SetChannelPrivate 36 1
SetChannelLabel 36 "36 First Ineq Result - Stop"
SetChannelPrivate 37 1
SetChannelLabel 37 "37 Second Ineq Result - Stop"
alias Run_Stop_inq_1 SetChannel 36 ($CH31*7-7+$day-$mday)<0
alias Run_Stop_inq_2 SetChannel 37 (($CH36+$CH31)*7-6+$day-$mday-$CH32)>0
alias Run_Stop_DTT SetChannel 35 ($CH37-$CH36)*7+$CH32-$day
alias Run_Stop_Day_Logic backlog Run_Stop_inq_1; Run_Stop_inq_2; Run_Stop_DTT
// Test if we are before or after the trigger days
SetChannelPrivate 28 1
SetChannelLabel 28 "28 Past Start Trigger"
alias Eval_Strt_Trigger SetChannel 28 ($month>$CH20)||(($month==$CH20)&&(($CH25<0)||(($CH25==0)&&($hour>=$CH23))))
SetChannelPrivate 38 1
SetChannelLabel 38 "38 Before Stop Trigger"
alias Eval_Stop_Trigger SetChannel 38 ($month<$CH30)||(($month==$CH30)&&(($CH35>0)||(($CH35==0)&&($hour<($CH33-$CH11)))))
// Test if we are in daylight savings based on the trigger days
// Note that if the start time is later in the year than end, ie DST rolls over newyears, different logic applies as shown in Set_Daylight_Savings_Bool_2
SetChannelType 15 ReadOnly
SetChannelPrivate 15 1
SetChannelLabel 15 "15 Daylight Savings Active"
alias Set_Daylight_Savings_Bool_1 SetChannel 15 $CH28&&$CH38
alias Set_Daylight_Savings_Bool_2 if (2016*$CH20+168*$CH21+24*$CH22+$CH23)>(2016*$CH30+168*$CH31+24*$CH32+$CH33) then SetChannel 15 ($CH28+$CH38)==1
// Set up alias's to run the required commands
alias Run_Daylight_Savings backlog Run_Strt_Day_Logic; Run_Stop_Day_Logic; Eval_Strt_Trigger; Eval_Stop_Trigger; Set_Daylight_Savings_Bool_1; Set_Daylight_Savings_Bool_2
alias Set_Time backlog ntp_timeZoneOfs $CH10; Run_Daylight_Savings; ntp_timeZoneOfs $CH10+$CH11*$CH15
alias ReSet_Time backlog clearClockEvents; Set_Time; addClockEvent ($CH33*$CH15+$CH23*(!$CH15))*3600 0xff 1 ReSet_Time
// **** Run Daylight savings logic ****
waitFor NTPState 1
ReSet_Time
Attached is a derivation of the indexing formulae