Rules for latching relays group

Hi guys,

The code below is a basic rule to control 3 lamps from their state. These lamps are connected on latching relay. (schemas)

rule "pulse_at_Evening_start"  //configurato su astro dusk start
when 
	//System started or
	Channel "astro:sun:local:nauticDusk#event" triggered START // astroDusk#event
then
	if (FF_Terrace_Light.state == CLOSED){
		FF_Terrace_Pulse.sendCommand(OFF)
		Thread::sleep(1000)
	} else {}
	if (F4_Outside_Light.state == CLOSED){
		F4_Outside_Pulse.sendCommand(OFF)
		Thread::sleep(1000)
	} else {}
	if (GF_Entryway1_Light.state == CLOSED){
		GF_Entryway1_Pulse.sendCommand(OFF)
	} else {}	
		logInfo("rule Evening Night Start", "Done")
end

i would find a general rule for control an entire group on these lamps. I tried with code below without any results

rule "all pulse off"

when

  item all_lights_off received command ON

then

  gLight.members.filter[a|a.state==OPEN].forEach[s| s.substring(0,s.lenghth() - 5) + "Pulse").sendCommand(OFF)]

end 

I’m not a programmer be patient with this abomination…

Regards

Lorenzo

Strings don’t have a .sendCommand method.
Do it in steps - get hold of your Item name, do the string manipulation, then use the sendCommand(stringname, stringcommand) action.

Like this?

rule "all pulse off"

when

Item all_lights_off received command ON

then

gLight.members.filter[a|a.state==OPEN].forEach[s|

var nameOfLight = s.name

var nameofPulse = nameOfLight.substring(s.lenghth() - 5,s.length()).concat("Pulse")

sendCommand(nameofPulse, OFF)

Thread::sleep(1000)

]

end

If building another group with the pulses, you can simply do it this way:

gLight.members.filter[a|a.state==OPEN].forEach[s|
    gPulse.members.filter[i|
        i.name.split("_").get(0) == s.name.split("_").get(0) 
    ].head.sendCommand(OFF)
    Thread::sleep(1000)
]

The second group is filtered by the name of the current item. The result is a list with one item. .head gives the first item of the list. You don’t need to use the action :wink:

Please be aware that Thread::sleep(1000) may be critical. Your rule might run more than 3 Seconds, and there is a chance that the rule is triggered more than once, this will result in problems. Another option:

// global vars on top the file
var Timer tPulse = null

rule "all pulse off"
when
    Item all_lights_off received command ON
then
    if(tPulse === null)
        tPulse = createTimer(now.plusMillis(10), [ |
            if(gLight.members.filter[a|a.state==OPEN].size > 0) {
                gPulse.members.filter[i|
                    i.name.split("_").get(0) == gLight.members.filter[a|a.state==OPEN].head.name.split("_").get(0)
                ].head.sendCommand(OFF)
                tPulse.reschedule(now.plusSeconds(1))
            } else
                tPulse = null
        ])
end

a Timer is created and almost once started.
In the Timer, if there are any “OPEN” lights, the list of Pulse Items is filtered to the first “OPEN” light and switched OFF. Afterwards the Timer is rescheduled one second in future.
If no “OPEN” lights are left, the Timer is deleted.

Strange… Rule doesn’t start

Does your rules file get loaded? Look in your openhab.log

Checked now…

image

Yes, what are you going to do about it?
You cannot subtract strings.
Did you meant to split your string and use one or more of the parts?

EDIT - duh, forget that - I completely misread the screenshot of = for -

Ups, sorry, typo. this one is correct (I changed it also in my posting above):

i.name.split("_").get(0) == s.name.split("_").get(0)

@Udo_Hartmann thank you!!

other strange behaviour
image

Received an output an item from a lamp on state CLOSED and the items OPEN ignored… :sweat_smile:

Ignored by what? None of the rules here would be expected to do anything about that.

the first image was a preview of glight Members , one and second item was OPEN and the others CLOSED
On second image can view item all_lights_off changed from off to on, only pulse GF_Entryway2_pulse received a command, although GF_Entryway2_Light was on state CLOSED.

Regards
Lorenzo

I’d be more concerned about why you send commands to the same pulse Item many times. The group filter is not doing what you want. I think you are matching on the wrong parts of your name strings.

You forgot the Thread::sleep(1000) :wink:

same issue, other pulse in this case (added sleep) … and repeat the issue described from @rossko57
Crazy…

2019-12-16 18:22:09.624 [vent.ItemStateChangedEvent] - all_lights_off changed from OFF to ON

2019-12-16 18:22:09.716 [ome.event.ItemCommandEvent] - Item 'GF_Entryway2_Pulse' received command OFF

2019-12-16 18:22:09.722 [nt.ItemStatePredictedEvent] - GF_Entryway2_Pulse predicted to become OFF

2019-12-16 18:22:09.732 [vent.ItemStateChangedEvent] - GF_Entryway2_Pulse changed from ON to OFF

2019-12-16 18:22:09.810 [vent.ItemStateChangedEvent] - GF_Entryway2_Light changed from CLOSED to OPEN

2019-12-16 18:22:10.082 [ome.event.ItemCommandEvent] - Item 'GF_Entryway2_Pulse' received command ON

2019-12-16 18:22:10.089 [nt.ItemStatePredictedEvent] - GF_Entryway2_Pulse predicted to become ON

2019-12-16 18:22:10.098 [vent.ItemStateChangedEvent] - GF_Entryway2_Pulse changed from OFF to ON

2019-12-16 18:22:10.739 [ome.event.ItemCommandEvent] - Item 'F4_Outside_Pulse' received command OFF

2019-12-16 18:22:10.752 [nt.ItemStatePredictedEvent] - F4_Outside_Pulse predicted to become OFF

2019-12-16 18:22:10.765 [vent.ItemStateChangedEvent] - F4_Outside_Pulse changed from ON to OFF

2019-12-16 18:22:10.810 [vent.ItemStateChangedEvent] - F4_Outside_Light changed from OPEN to CLOSED

2019-12-16 18:22:11.114 [ome.event.ItemCommandEvent] - Item 'F4_Outside_Pulse' received command ON

2019-12-16 18:22:11.121 [nt.ItemStatePredictedEvent] - F4_Outside_Pulse predicted to become ON

2019-12-16 18:22:11.143 [vent.ItemStateChangedEvent] - F4_Outside_Pulse changed from OFF to ON

2019-12-16 18:22:11.819 [ome.event.ItemCommandEvent] - Item 'GF_Entryway2_Pulse' received command OFF

2019-12-16 18:22:11.836 [nt.ItemStatePredictedEvent] - GF_Entryway2_Pulse predicted to become OFF

2019-12-16 18:22:11.843 [vent.ItemStateChangedEvent] - GF_Entryway2_Pulse changed from ON to OFF

2019-12-16 18:22:11.888 [vent.ItemStateChangedEvent] - GF_Entryway2_Light changed from OPEN to CLOSED

2019-12-16 18:22:12.193 [ome.event.ItemCommandEvent] - Item 'GF_Entryway2_Pulse' received command ON

2019-12-16 18:22:12.198 [nt.ItemStatePredictedEvent] - GF_Entryway2_Pulse predicted to become ON

2019-12-16 18:22:12.204 [vent.ItemStateChangedEvent] - GF_Entryway2_Pulse changed from OFF to ON

2019-12-16 18:22:12.841 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command OFF

2019-12-16 18:22:12.851 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become OFF

2019-12-16 18:22:12.867 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from ON to OFF

2019-12-16 18:22:12.921 [vent.ItemStateChangedEvent] - FF_KidsRoom_Light changed from CLOSED to OPEN

2019-12-16 18:22:13.217 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command ON

2019-12-16 18:22:13.223 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become ON

2019-12-16 18:22:13.231 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from OFF to ON

2019-12-16 18:22:13.683 [vent.ItemStateChangedEvent] - Grid_Power changed from 772.62 to 800.06

2019-12-16 18:22:13.688 [vent.ItemStateChangedEvent] - Load_Power changed from -772.62 to -800.06

2019-12-16 18:22:13.869 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command OFF

2019-12-16 18:22:13.881 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become OFF

2019-12-16 18:22:13.890 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from ON to OFF

2019-12-16 18:22:13.930 [vent.ItemStateChangedEvent] - FF_KidsRoom_Light changed from OPEN to CLOSED

2019-12-16 18:22:14.233 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command ON

2019-12-16 18:22:14.241 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become ON

2019-12-16 18:22:14.247 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from OFF to ON

2019-12-16 18:22:14.902 [ome.event.ItemCommandEvent] - Item 'all_lights_off' received command OFF

2019-12-16 18:22:14.912 [vent.ItemStateChangedEvent] - all_lights_off changed from ON to OFF

p.s: i tried the second rules wrote… doensn’t work… probably caused from members of glight.

Well, if you haven’t changed the parts of the string names that you are matching, then that result isn’t going to change.
Do you have an understanding of how to access the different parts of a string split result?

The filter does not work, obviously. But as the filter works for me :wink: … Are you sure you saved the rule?

Maybe add some logging (will emerge in openhab.log):

rule "all pulse off"
when
    Item all_lights_off received command ON
then
    logInfo("pulseAll","Rule started.")
    gLight.members.filter[a|a.state==OPEN].forEach[s|
        logInfo("pulseAll","Processing Item {} ({})",s.name,s.state)
        gPulse.members.filter[i|
            i.name.split("_").get(0) == s.name.split("_").get(0)
        ].head.sendCommand(OFF)
        Thread::sleep(1000)
    ]
    logInfo("pulseAll","Rule ends.")
end

there he is ! Many Thanks!

2019-12-16 19:15:19.482 [INFO ] [ipse.smarthome.model.script.pulseAll] - Rule started.

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:19.487 [vent.ItemStateChangedEvent] - all_lights_off changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:19.499 [INFO ] [ipse.smarthome.model.script.pulseAll] - Processing Item GF_Entryway1_Light (OPEN)

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:19.514 [ome.event.ItemCommandEvent] - Item 'GF_Anteroom_Pulse' received command OFF

2019-12-16 19:15:19.524 [nt.ItemStatePredictedEvent] - GF_Anteroom_Pulse predicted to become OFF

2019-12-16 19:15:19.534 [vent.ItemStateChangedEvent] - GF_Anteroom_Pulse changed from ON to OFF

2019-12-16 19:15:19.606 [vent.ItemStateChangedEvent] - GF_Anteroom_Light changed from CLOSED to OPEN

2019-12-16 19:15:19.887 [ome.event.ItemCommandEvent] - Item 'GF_Anteroom_Pulse' received command ON

2019-12-16 19:15:19.896 [nt.ItemStatePredictedEvent] - GF_Anteroom_Pulse predicted to become ON

2019-12-16 19:15:19.905 [vent.ItemStateChangedEvent] - GF_Anteroom_Pulse changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:20.530 [INFO ] [ipse.smarthome.model.script.pulseAll] - Processing Item GF_Anteroom_Light (OPEN)

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:20.554 [ome.event.ItemCommandEvent] - Item 'GF_Anteroom_Pulse' received command OFF

2019-12-16 19:15:20.586 [nt.ItemStatePredictedEvent] - GF_Anteroom_Pulse predicted to become OFF

2019-12-16 19:15:20.593 [vent.ItemStateChangedEvent] - GF_Anteroom_Pulse changed from ON to OFF

2019-12-16 19:15:20.629 [vent.ItemStateChangedEvent] - GF_Anteroom_Light changed from OPEN to CLOSED

2019-12-16 19:15:20.755 [vent.ItemStateChangedEvent] - Grid_Power changed from 2564.8 to 2588.95

2019-12-16 19:15:20.758 [vent.ItemStateChangedEvent] - Load_Power changed from -2564.8 to -2588.95

2019-12-16 19:15:20.930 [ome.event.ItemCommandEvent] - Item 'GF_Anteroom_Pulse' received command ON

2019-12-16 19:15:20.937 [nt.ItemStatePredictedEvent] - GF_Anteroom_Pulse predicted to become ON

2019-12-16 19:15:20.951 [vent.ItemStateChangedEvent] - GF_Anteroom_Pulse changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:21.563 [INFO ] [ipse.smarthome.model.script.pulseAll] - Processing Item F4_Outside_Light (OPEN)

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:21.594 [ome.event.ItemCommandEvent] - Item 'F4_Outside_Pulse' received command OFF

2019-12-16 19:15:21.625 [nt.ItemStatePredictedEvent] - F4_Outside_Pulse predicted to become OFF

2019-12-16 19:15:21.632 [vent.ItemStateChangedEvent] - F4_Outside_Pulse changed from ON to OFF

2019-12-16 19:15:21.687 [vent.ItemStateChangedEvent] - F4_Outside_Light changed from OPEN to CLOSED

2019-12-16 19:15:21.968 [ome.event.ItemCommandEvent] - Item 'F4_Outside_Pulse' received command ON

2019-12-16 19:15:21.973 [nt.ItemStatePredictedEvent] - F4_Outside_Pulse predicted to become ON

2019-12-16 19:15:21.983 [vent.ItemStateChangedEvent] - F4_Outside_Pulse changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:22.615 [INFO ] [ipse.smarthome.model.script.pulseAll] - Processing Item FF_Terrace_Light (OPEN)

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:22.658 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command OFF

2019-12-16 19:15:22.673 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become OFF

2019-12-16 19:15:22.687 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from ON to OFF

2019-12-16 19:15:22.713 [vent.ItemStateChangedEvent] - FF_KidsRoom_Light changed from CLOSED to OPEN

2019-12-16 19:15:23.040 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command ON

2019-12-16 19:15:23.047 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become ON

2019-12-16 19:15:23.051 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:23.661 [INFO ] [ipse.smarthome.model.script.pulseAll] - Processing Item FF_KidsRoom_Light (OPEN)

==> /var/log/openhab2/events.log <==

2019-12-16 19:15:23.703 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command OFF

2019-12-16 19:15:23.725 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become OFF

2019-12-16 19:15:23.750 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from ON to OFF

2019-12-16 19:15:23.777 [vent.ItemStateChangedEvent] - FF_KidsRoom_Light changed from OPEN to CLOSED

2019-12-16 19:15:24.122 [ome.event.ItemCommandEvent] - Item 'FF_KidsRoom_Pulse' received command ON

2019-12-16 19:15:24.129 [nt.ItemStatePredictedEvent] - FF_KidsRoom_Pulse predicted to become ON

2019-12-16 19:15:24.141 [vent.ItemStateChangedEvent] - FF_KidsRoom_Pulse changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-12-16 19:15:24.721 [INFO ] [ipse.smarthome.model.script.pulseAll] - Rule ends.

So, the question is: Where does the command for GF_Anteroom_Pulse come from?

Another detail: Why do you use a contact item for the *_Light Items? would be better to use Switch Items here.

Maybe I should make my hint more explict.

If you have Items named
GF_Entryway1_Light
GF_Entryway2_Light
GF_Anteroom_Light
and you split the string name at _
How many string parts will you get in your results?
What do you think results [0] will be?
Will that allow you to select just one of your Items?