Script for event timer deactivation in certain cond.
-
Hi
I need help with a script.
I have a garage door that opens and close with a toggle. Joel kindly did a script for me to allow the relay to return to normal state when ever it closes. I did a timer even that opens the door at 0700 hrs and closes at 2200 hrs, my problem was that if anyone overrides the BMS and opens or closes the door before the event timer then the door does the opposite for example if anyone opens the door before 0700 hrs then at 0700 hrs the door closes, I have now put a sensor on the door to see if it is open or closed, I need a script to do the following:-
if the sensor reading is false then the door is closed, therefore if event timer 2200 hours is reached then the event timer is not activated, and if the sensor reading is true then the door is open, therefore if event timer 0700 hrs is reached then the event timer is not activated and the door remains open.
this one way of doing it. I can do it in another way in that during the period between 2200 hrs and 0700 hrs every time the sensor reading is true then the door is closed. but that would create a whole new set of problems, and it would take the manual override offline.
so please help me with this.
much appreciated.
-
If I'm understanding correctly, you have an event handler for a scheduled event which sets the relay state.
Would it be possible to do the whole thing from a script? Let's say we have two context points, sensor and control...
Script executes on cron pattern 0 0 7,22 * * ? ==> only 0700 and 2200
Script: ===========
var hour = (new Date()).getHours();
if( hour == 7 && !sensor.value )
control.set( 1 );
else if( hour == 22 && sensor.value )
control.set( 0 );Then you would remove the old event handlers that were performing control.
-
Thanks for the reply. But I have a question the cron time I use is as follows:-
to close door active = 0 00 22 ? * SUN-THU
to open door active = 0 00 07 ? * SUN-THUFriday and Saturday door is on Manual operation only.
how can I implement this in the script.
and also !sensor.value is this a variable? the Variable for the Relay Data point "Board 2 - Relay 8" where does that go in the script?
ok I tried to wrap my head around it. your script is treating the relay as on off when in fact it's a toggle because I have another script running that keep checking the relay, if it is true then it returns it to false.
so :
- if sensor value is 0 (zero) at 0700 then the script should turn the relay to true, but if it is 1 (one) then it will not send any commands to the relay, because the door is open,
- if sensor value is 1 (one) at 2200 then the script should turn the relay to true, but if it is 0 (zero) then it will not send any commands to the relay because the door is close.
I set in the data source the following:-
Cron pattern = 0 00 07,22 ? * SUN-THU
Variable control = Board 2 - Relay 8
Variable sensor = tM-P3R3 - Garage Door Sensorvar hour = (new Date()).getHours();
if( hour == 7 && !sensor.value ) <-- Where do you get the sensor value (0 or 1) is the "!" trigger???
control.set( true );
else if( hour == 22 && sensor.value )
control.set( true );I hope this is logical?
forgive me, I'm a total noob with scripts.
-
Looks good to me. I would take the extra zeroes out of the cron pattern (only 1 zero for minutes, no leading zero on 7). I was treating sensor as a Binary point, so it would have a boolean .value. "!" is the logical negation operator, true==>false, false==>true, or with numbers 0==>1, not 0==>0. If it is numeric or multistate, consider doing the value test explicitly for clarity (i.e. sensor.value == 0), but it should be functionally equivalent.
-
OK I did and tested, the script is not being triggered on either time, I took out the leading zero but cron gave me an error., here is a screenshot of the script.
Any suggestions?
-
I am able to save a script with cron 0 0 7,22 ? * SUN-THU without issue.
The only thing that looks bad to me is the triple equals on the test sensor.value === 0. This will never be true, since a triple equals in JavaScript is a value and type check. sensor is a binary point, so it has a boolean value. While the value of boolean false is equal to the value of zero, the types are different so the triple equals condition is always false. Either use the keyword 'false' or use a double equals instead.
-
I did the === because the script objected to it. I fixed it now as follows, the words 'zero and one' are configured in the datapoint itself.:-
var hour = (new Date()).getHours();
if( hour == 7 && sensor.value == zero )
control.set( true );
else if( hour == 22 && sensor.value == one )
control.set( true );now I did the time as you suggested. will test it and see how it goes..
FYI although I did a log level of info, I'm not getting any log entries at all for the script.
-
one and zero don't look defined. 1 and 0?
-
OK if I put 0 then the script objects. if I put one or zero then I get and error.
what to do?
QQ is !sensor.value mean 0 or 1 ?
the script ran and I got this error:
'Garage Door Sensor':
at line: 4, column: -1
is this script ok
var hour = (new Date()).getHours();
var t = 1;
var f = 0;
if( hour == 7 && sensor.value == f )
control.set( true );
else if( hour == 22 && sensor.value == t )
control.set( true ); -
Phil
That last script worked like a charm.
thank you for all your help my friend.