how do i need to change the rule to work properly again.
i had this in oh2, was working great, and since oh3 i have seen that the timer is always 10min, the SchlafzimmerLichtDauer can be set to 2, but the timer is still 10 min, i get no error in oh3
i know there is something changed in oh3 with the timer, but my skills with scribts not the best
var Timer timer = null
rule "Licht Schlafzimmer bei Bewegung Einschalten"
when
Item Motion_FF_Bed received update ON
then
if((timer === null && flagItemsunset.state == ON && Bewegungsmelder.state == ON) ||
//(timer === null && Light_Sensor_Outdoor.state <= 60 && Bewegungsmelder.state == ON) ||
(timer === null && Motion_FF_Bed_illuminance.state <= 40 && Bewegungsmelder.state == ON)) {
var time=Integer::parseInt(SchlafzimmerLichtDauer.state.toString)
logInfo("FILE", "Setting to ON and creating timer "+time.toString)
Light_FF_Bed_Ceiling.sendCommand(ON)
timer = createTimer(now.plusMinutes(time), [|
logInfo("FILE", "Timer expired and setting to OFF")
Motion_FF_Bed.postUpdate(OFF)
Light_FF_Bed_Ceiling.sendCommand(OFF)
timer = null
])
}
else if (timer !== null) {
logInfo("FILE", "Timer rescheduled")
timer.reschedule(now.plusMinutes(10))
}
else {
logInfo("File","Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun")
}
end
If itâs always 10 minutes that means that huge and complicated if statement at the top is not working as you expect.
Log out everything in that if statement and you will see why itâs not working.
In the process, consider what you can do to simplify that super long if statement. For example, you always look for timer === null and Bewegungsmelder.state == ON. So why test for it over and over?
Nothing changed with Timer. This code will work as is in OH 3. And if there were a change, OH 3 would simply fail the rule with an error. Thatâs not the problem.
Try breaking it up and adding logging.
if(timer !== null) {
timer.reschedule(now.plusMinutes(10));
return; // rule exits, we don't have to worry about this case again for the rest of the rule
}
if(Bewegungsmelder.state != ON){
return; // rule exits as there is nothing to do when the Item isn't ON
}
// We now know timer has to be null so we don't have to test for it
// We now know that Bewegungsmelder.state is ON so we don't have to test for it
if(flagItemsunset.state == ON || Motion_FF_Bed_illuminance.state <= 40) {
// create the timer
}
Sprinkle the above with lots of logging and you will see whatâs wrong.
This kind of thing jumps out. Many device channels that were simple numbers in OH2 are now Quantity types in OH3, and come with units. Comparing a Quantity with a simple number does not go well, apples and oranges.
But you will see that if you log values out as already suggested.
A question just arose in my mind. What happens if you try to link a plain old Number Item to a UoM Channel? That should generate errors right? So it should be apparent in the logs that something is wrong. At the very least the UI should show the Item as NULL or at least unchanging.
But I could also see it just stripping the units off and passing the number on down which can make these sorts of migration issues trickier to diagnose.
Iâm not sure I like that behavior. Itâs like failing silently. A Number without UoM should be a number without UoM. Iâll have to experiment with that and maybe file an issue because thatâs clearly biting a lot of people in the bum.
Itâs been there since OH2, you can postUpdate plain Numbers with quantity values from rule or REST. Channel uses same path I believe,
I believe something in the binding XML config can suggest permitted types for linking, but it may not be policed thoroughly. Given that profile transforms could match any to any, we might not want it policed.
Iâll bet the root of the problem here is importing from old xxx.items file, which has always been able to force 'inappropriateâlinks anyway.
I donât argue that it doesnât work, but itâs very counter intuitive and Iâd even argue inconsistent for a Number Item without units to carry a QuantityType at all. That should be an error or, at the very least have the QuantityType converted to a DecimalType. Otherwise you simply cannot ever trust what a plain old Number Item carries. If the binding, at some later date decides to add UoM to a Channel that formerly did not use UoM, rules will start to explode with cryptic errors that you or I could figure out but most average users would not.
Since you get a relatively meaningful error when you try to assign a QuantityType that is incompatible with a Number:X (e.g. try to assign 25 V to a Number:Temperature), to be consistent one would expect trying to assign any QuantityType to a Number would either result in an error, or in the units being stripped off. The fact that the units are retained is a surprise and surprises like this will end up creating a whole lot of work for us here on the forum.
Well, what are going to do now? Youâve already got some logging that shows the path through your rule. Youâre even logging out what that time variable is, but the output is still secret from us.
Any of that look wrong to you?
i have the same rule also in every other room with different times, also set some to 10, is it possible that the timer is changed by them, or when the motion sensor set more than one ON command?
i am not at home befor friday, but at the weekend, i had checked the time with the smartphone in the bedromm and it was 10min instead of 2
Well, yes. In theory it will be a different âtimerâ in each file, but all rules in one file will share it. That could lead to weird behaviour as this rule cancels or reschedules that rules timer.
In practice no-one has ever quite trusted the separation of global xx in this file from global xx in that file. Itâs just asking for trouble really.
Iâll refine that, I was mistaken - if you link say a Number:Angle channel to a plain Number Item, you get a plain Number state.
It might not be the number you expect, depending on defaults and state descriptions, I havenât sorted that out yet.
You canpostUpdate quantities with units to plain Number Items from rules, but there are definitely odd effects, and they appear to be different to channel linking.
If you have a Number with no [pattern], post what you like.
â37 kWâ , â41 radâ , â47 °â , â57â
gives Item states just as requested, with or without the unit as requested.
post â67 bananasâ, get log (and no update) Cannot convert '67 bananas' to a state type which item 'xxxx' accepts: [DecimalType, QuantityType, UnDefType].
If you have a Number with a quantity-like pattern, say [%.0f °], and repeat the updates
â37 kWâ is silently ignored completely. No event, no log, weird.
â41 radâ , â47 °â , â57â do the updates as expected.
Of note, radians is not converted to degrees.
â67 bananasâ is rejected as before.
Definitely something wrong with â37 kWâ behaviour.
i have now changed the variable to timerar for this one file that is turning on the light in the workingroom:
var Timer timerar = null
rule "Licht Arbeitszimmer bei Bewegung Einschalten"
when
Item Motion_GF_Working received update ON
then
if((timerar === null && flagItemsunset.state == ON && Bewegungsmelder.state == ON) ||
//(timerar === null && Light_Sensor_Outdoor.state <= 60 && Bewegungsmelder.state == ON) ||
(timerar === null && Motion_GF_Working_illuminance.state <= 40 && Bewegungsmelder.state == ON))
{
var time=Integer::parseInt(ArbeitszimmerLichtDauer.state.toString)
logInfo("FILE", "Setting to ON and creating timer")
Sonoff_Touch_GF_Working.sendCommand(ON)
timerar = createTimer(now.plusMinutes(time), [|
logInfo("FILE", "Timer expired and setting to OFF")
Motion_GF_Working.postUpdate(OFF)
Sonoff_Touch_GF_Working.sendCommand(OFF)
timerar = null
])
}
else if (timerar !== null) {
logInfo("FILE", "Timer rescheduled")
timerar.reschedule(now.plusMinutes(10))
}
else {
logInfo("File","Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun")
}
end
and get these error:
2021-05-28 23:27:30.384 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'licht_arbeitszimmer_bei_bewegung_einschalten_nach_sonnenuntergang_oder_bei_dunklen_tagen-1' failed: For input string: "NULL" in licht_arbeitszimmer_bei_bewegung_einschalten_nach_sonnenuntergang_oder_bei_dunklen_tagen