[SOLVED] Switch multiple timer in succession

Hello Udo

You are awesome - as soon as I am at home I will give it a try.
What is really hard for me to understand is, that I don´t send a command directly to the single steps.

Do I have to chnge the name of my step items?

BG
Daniel

No, as long as the name contains the numbers from 1 to n (where n is maximum 9) with no gaps (i.e. 1,2,3 1,2,3,4,5,6,7 but not 1,3,4,6) the names should be fine.

Hi Udo

I gave the code a try and at least the first or the last lampt (depending on up or downstairs) turned on, but never off again, All the steps between didn´t turn on.

Those are my items:

Group glight_stair
Switch Shelly1_1 “Stufe 1” (glight_stair)
Switch Shelly1_2 “Stufe 2” (glight_stair)
Switch Shelly1_3 “Stufe 3” (glight_stair)
Switch Shelly1_4 “Stufe 4” (glight_stair)
Switch precence_upstairs “Bewegung oben”
Switch precence_downstairs “bewegung unten”
Number:Time stepTime “Einschaltzeit Treppe [%.0f s]”
Number:Time stepOnDuration “Einschaltdauer Treppe [%.0f s]”

duration ist set to 10 second and time to 1 second
I haven´t changed your rule.

this is the outcome:

2020-03-18 00:05:24.221 [ome.event.ItemCommandEvent] - Item ‘stepOnDuration’ received command 10
2020-03-18 00:05:24.241 [vent.ItemStateChangedEvent] - stepOnDuration changed from 1.0 s to 10.0 s
2020-03-18 00:05:27.904 [ome.event.ItemCommandEvent] - Item ‘precence_downstairs’ received command ON
2020-03-18 00:05:27.922 [vent.ItemStateChangedEvent] - precence_downstairs changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2020-03-18 00:05:28.007 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘steps ON and OFF’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusSeconds(int) on instance: 2020-03-18T00:05:28.003+01:00
==> /var/log/openhab2/events.log <==
2020-03-18 00:05:28.047 [ome.event.ItemCommandEvent] - Item ‘Shelly1_4’ received command ON
2020-03-18 00:05:28.051 [nt.ItemStatePredictedEvent] - Shelly1_4 predicted to become ON
2020-03-18 00:05:28.077 [vent.ItemStateChangedEvent] - Shelly1_4 changed from OFF to ON

after that Shelly 1_4 stays on.

I’m really sorry that I need so much help with something as simple as this

BR
Daniel

Please don’t use UoM Items, these Items are unbound, so this will not work.
Please also change the Names of the Items slightly (I just did not realize that the names start with Shelly1 … which is a Number…)

Hi Udo

ok thank you.

So the Number Items should only be
Number stepTime
Numer stepOnDuration

and nothing else. The selection happens in habpanel with a comma separeted list.

BR
Daniel

ok there seems to still be an error,
Chenged the Items to:

Group glight_stair
Switch ShellyStair_1 “Stufe 1” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel1#power”}
Switch ShellyStair_2 “Stufe 2” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel2#power”}
Switch ShellyStair_3 “Stufe 3” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel3#power”}
Switch ShellyStair_4 “Stufe 4” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel4#power”}
Switch precence_upstairs “Bewegung oben”
Switch precence_downstairs “bewegung unten”
Number stepTime “Einschaltzeit Treppe [%.0f s]”
Number stepOnDuration “Einschaltdauer Treppe [%.0f s]”

Rule is still the same:
Upstairs → Stair 1 turns on and stays on
Downstairs → Stair 4 turns on and stays on
First up then down → both turn on and stay on

Log:

2020-03-18 21:08:28.121 [vent.ItemStateChangedEvent] - precence_upstairs changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2020-03-18 21:08:28.165 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘steps ON and OFF’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusSeconds(int) on instance: 2020-03-18T21:08:28.162+01:00
==> /var/log/openhab2/events.log <==
2020-03-18 21:08:28.184 [ome.event.ItemCommandEvent] - Item ‘ShellyStair_1’ received command ON
2020-03-18 21:08:28.208 [nt.ItemStatePredictedEvent] - ShellyStair_1 predicted to become ON
2020-03-18 21:08:28.226 [vent.ItemStateChangedEvent] - ShellyStair_1 changed from OFF to ON
2020-03-18 21:08:30.701 [vent.ItemStateChangedEvent] - Windgeschwindigkeit changed from 0.5 to 0.35000000000000003
2020-03-18 21:08:34.248 [ome.event.ItemCommandEvent] - Item ‘precence_downstairs’ received command ON
2020-03-18 21:08:34.272 [vent.ItemStateChangedEvent] - precence_downstairs changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2020-03-18 21:08:34.306 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘steps ON and OFF’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusSeconds(int) on instance: 2020-03-18T21:08:34.303+01:00
==> /var/log/openhab2/events.log <==
2020-03-18 21:08:34.329 [ome.event.ItemCommandEvent] - Item ‘ShellyStair_4’ received command ON
2020-03-18 21:08:34.373 [nt.ItemStatePredictedEvent] - ShellyStair_4 predicted to become ON
2020-03-18 21:08:34.397 [vent.ItemStateChangedEvent] - ShellyStair_4 changed from OFF to ON

I never thought it would be so hard to turn on a few lights in sequence.
:slight_smile:
BR
Daniel

Change
nDur = duration.state as Number
to
nDur = (duration.state as Number).intValue

The .plusSeconds() method is picky about being fed integers only

Things that seem easy for people can be hard for machines, and vice-versa :wink:

Oh, that’s not the last version of the rule… :wink:

at least, something has changed. the duration item has an influence :slight_smile:
UP --> Stair 1 turns on an then off after duration time is over
DOWN --> Stair 4 turns on an then off after duration time is over
UP and DOWN --> only the first one triggered --> works as it should
The error messages are gone.

So whats missing is to turn on the next few steps.

BR

What’s the current rule?

Hi Udo

Rule

// create global vars and vals always on top of file
var Timer tLightOn = null // object for ON timer
var Timer tLightOff = null // object for OFF timer
var Integer iStepOn = 0 // counter for ON
var Integer iStepOff = 0 // counter for OFF
var Boolean bDir = true // true == up, false == down
var Integer nStep = 1 // setup default stepTime
var Integer nDuration = 10 // setup default time between ON and OFF
var Integer nMaxStep = 0 // number of steps (automatic)
rule “steps ON and OFF”
when
Item precence_upstairs received command ON or // detected movement upstairs
Item precence_downstairs received command ON // detected movement downstairs
then
if(tLightOn !== null || tLightOff !== null )
return;
bDir = if(triggeringItem.name.contains(“up”)) true else false // which direction?
iStepOn = if(triggeringItem.name.contains(“up”)) 0 else nMaxStep + 1 // count up or down?
iStepOff = if(triggeringItem.name.contains(“up”)) 0 else nMaxStep + 1 // count up or down?
if(stepTime.state instanceof Number) // if stepTime is set (item must exist!)
nStep = (stepTime.state as Number).intValue // get stepTime from item
if(stepOnDuration.state instanceof Number) // if stepOnDuration is set (item must exist!)
nDuration = (stepOnDuration.state as Number).intValue // get stepOnDuration from item
nMaxStep = glight_stair.members.size
tLightOn = createTimer(now.plusMillis(10), [ // create the timer for ON
iStepOn = iStepOn + if(bDir) 1 else -1 // count up or down depending on direction
glight_stair.members.filter[i | i.name.contains(iStepOn.toString)].head.sendCommand(ON) // get the actual step and send command ON
if(iStepOn > 1 && iStepOn < nMaxStep) // if not last step
tLightOn.reschedule(now.plusSeconds(nStep)) // schedule the next step
else
tLightOn = null
])
tLightOff = createTimer(now.plusSeconds(nDuration), [ // create the timer for OFF
iStepOff = iStepOff + if(bDir) 1 else -1 // count up or down depending on direction
glight_stair.members.filter[i | i.name.contains(iStepOff.toString)].head.sendCommand(OFF) // get the actual step and send command OFF
if(iStepOff > 1 && iStepOff < nMaxStep) // if not last step
tLightOff.reschedule(now.plusSeconds(nStep)) // schedule the next step
else
tLightOff = null
])
end

Items:

Group glight_stair
Switch ShellyStair_1 “Stufe 1” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel1#power”}
Switch ShellyStair_2 “Stufe 2” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel2#power”}
Switch ShellyStair_3 “Stufe 3” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel3#power”}
Switch ShellyStair_4 “Stufe 4” (glight_stair) {channel=“shelly:shellyrgbw2-white:5a3c55:channel4#power”}
Switch precence_upstairs “Bewegung oben”
Switch precence_downstairs “bewegung unten”
Number stepTime “Einschaltzeit Treppe [%.0f s]”
Number stepOnDuration “Einschaltdauer Treppe [%.0f s]”

BR
Daniel

Ah. I think I got the issue. Please change this line:

if(iStepOn > 1 && iStepOn < nMaxStep) // if not last step

to this:

if((iStepOn > 1 && !bDir)||( bDir && iStepOn < nMaxStep)) // if not last step

Please keep in mind, that there is a second line for iStepOff which does the same for switching off. Change this line in the same manner.

2 Likes

Thank you Udo

now it works like a charm.

I will test it with two Shellys and 8 steps as soon as possible

Br
Daniel

You’re welcome! :slight_smile:

Hi Udo

I got another issue. As long as i use a UI switch for presence_upstairs or presence_downstairs, the rule gets triggered. The problem is, that the real trigger is the input state of a shelly rgbw. If the input changes, the output isnt a “received command XX” ist alwas a changed from XX to XX, which doesnt fire the rule.

I changed the rule to the following, which doesnt work either, because of errors within the rule.

“steps ON and OFF”
when
//Item precence_upstairs received command ON or // detected movement upstairs
Item precence_upstairs changed from OFF to ON or // detected movement upstairs
//Item precence_downstairs received command ON // detected movement downstairs
Item precence_downstairs changed from OFF to ON
then
if(tLightOn !== null || tLightOff !== null )

help is really appreciated

BR
Daniel

ok haven´t changed anything but now it seems to work.

Hello Udo

I have been using your rule in practice for some time now. Everything works so far - the problem is that if I set the nStep default or the corresponding item < 1, e.g. to 0.5 seconds, then only a few milliseconds pass between the steps. Everything > greater than 1 and 1 works perfectly.

Can you please take another look at this?

Thank you
Daniel

now.plusSeconds() only accepts integer values.

If you want to deal in fractions you’ll have to change to using milliseconds.

thank you

that did the job.

BR