Trigger on/off for all items in a group

I have groups for DayOn, DayOf, NightOn and NightOff and events for sunrise/sunset i want to trigger items of those groups to turn on or off depending on what group they belong to.
My items are setup like this

Group NightOn "Turn on during day" Switch Fib_Propp2_Switch "Grindstolpar" <socket> (Outdoor, Light, NightOn, DayOff) Switch Fib_Propp3_Switch "Altan" <socket> (Outdoor,Light, NightOn, DayOff)

And the rules: (all are the same but with different groups and on/off commands)

rule "sunrise" when
	Item Sunrise_Event changed to ON
then
	logInfo("Day events", "Sunset")
	NightOn.members.filter(x | x.state != ON).forEach[ item |
			logInfo("Day events","Sunset: Turning on " + item)
            postUpdate(item, ON)
        ]
end

All i get in the log is the “Day events: Sunset” part. nothing else, and no error. What have i messed up?

/C

Just to clarify: is there a reason why you mention Sunrise_Event in the item, but ‘Sunset’ in the log info? You are looking at the night group to define which ones need to be on during the day? I would personally expect something like this:

rule “sunrise” when
Item Sunrise_Event changed to ON
then
logInfo(“Day events”, “Sunrise”)
DayOn.members.forEach[ item |
logInfo(“Day events”,"Sunrise: Turning on " + item)
sendCommand(item, ON)
]
DayOff.members.forEach[ item |
logInfo(“Day events”,"Sunrise: Turning off " + item)
sendCommand(item, OFF)
]
end

This does send ON and OFF commands to items regardless of their state, but that usually doesn’t hurt.

What do you think?

The sunrise/sunset is just due to me doing a quick rewrite after posting and noticing that i had copied the items that turn on during the night but had copied the day event.
Also i had it your way before but switched to 4 separate events just to be able to trigger on sunset start/end and sunrise start/end to get the lights to stay on a little longer.
However, the events are not the issue. Its the foreach-loop that doesnt seem to update a single item for me…
Posting from the phone now so cant check your exemple while posting, but where there something wrong/not recomended in my loop that you changed?

Ah, the filter is gone… i can give that a try.

Thanks.

I think you addressed the wrong group. You were trying to switch the devices in group NightOn ON during Sunrise. Combined with the filter that left you with an empty group I think.
Also, you used postUpdate, I would use sendCommand. postUpdate is meant to be used when the state needs updating because it changed from the outside. sendCommand is used to effectuate a change in physical state of the item.

I checked with this code:

‘’’`rule “sunrise” when
Item sunrise changed to ON
then
logInfo(“Day events”, “Sunrise”)
DayOn.members.forEach[ item |
logInfo(“Day events”,"Sunrise: Turning on " + item.toString)
sendCommand(item, “ON”)
]
DayOff.members.forEach[ item |
logInfo(“Day events”,"Sunrise: Turning off " + item.toString)
sendCommand(item, “OFF”)
]
end’’’

and this should be the result:

2016-04-26 01:17:34.964 [INFO ] [runtime.busevents ] - sunrise received command ON
2016-04-26 01:17:35.110 [INFO ] [penhab.model.script.Day events] - Sunrise
2016-04-26 01:17:35.622 [INFO ] [penhab.model.script.Day events] - Sunrise: Turning on TestItem (Type=SwitchItem, State=Uninitialized)
2016-04-26 01:17:35.945 [INFO ] [runtime.busevents ] - TestItem received command ON

So that should be what you are looking for methinks…

Regarding your remark on leaving lights on: you can of course split the dayon and dayoff and put them in two separate rules. Another way of keeping the lights on longer that gives more control is to create more groups, one for each period in the day. So periods dawnon, dawnoff, dayon, dayoff, dusk…, night…, with items mapping accordingly.

Maybe use item.sendCommand(OFF) instead, more reliable I heard…

For reference i removed the filter so i allways send the commands even though they are already in the correct state.
And i change to item.sendCommand instead as skatun suggested and it seems to work now. Here is the log for a complete cycle of all the events.

(That nothing happens for sunset end and sunrise start is by design since i want the lights to turn on when the sunset starts, and turn of when the sunrise ends to get a kind of extended night since swedish nights are so short during summer)

Sunset_Event changed from OFF to ON
Sunset
Sunset: Turning on Fib_Propp2_Switch (Type=SwitchItem, State=ON, Label=Grindstolpar, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Sunset: Turning on Fib_Propp3_Switch (Type=SwitchItem, State=ON, Label=Altan, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Item 'Fib_Propp2_Switch' received command ON
Sunset: Turning on zws1 (Type=SwitchItem, State=ON, Label=ZWS1, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Item 'Fib_Propp3_Switch' received command ON
Item 'zws1' received command ON
Sunset: Turning on zws3 (Type=SwitchItem, State=ON, Label=ZWS3 (IP44), Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Item 'zws3' received command ON

Sunset_Event changed from ON to OFF
Night

Sunrise_Event changed from NULL to ON
Sunrise

Sunrise_Event changed from ON to OFF
Day
Sunrise: Turning off Fib_Propp2_Switch (Type=SwitchItem, State=ON, Label=Grindstolpar, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Sunrise: Turning off Fib_Propp3_Switch (Type=SwitchItem, State=ON, Label=Altan, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Item 'Fib_Propp2_Switch' received command OFF
Sunrise: Turning off zws1 (Type=SwitchItem, State=ON, Label=ZWS1, Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Sunrise: Turning off zws3 (Type=SwitchItem, State=ON, Label=ZWS3 (IP44), Category=socket, Groups=[Outdoor, Light, NightOn, DayOff])
Item 'Fib_Propp3_Switch' received command OFF
Item 'zws1' received command OFF
Item 'zws3' received command OFF
NightOn changed from ON to UNDEF through Fib_Propp2_Switch
DayOff changed from ON to UNDEF through Fib_Propp2_Switch
Fib_Propp2_Switch changed from ON to OFF
Fib_Propp3_Switch changed from ON to OFF
zws1 changed from ON to OFF
NightOn changed from UNDEF to OFF through zws3
DayOff changed from UNDEF to OFF through zws3
zws3 changed from ON to OFF

As a sidenote. When dealing with day/night events its a good idea to bind the items to a switch so they can be triggered manually. its a lot faster to flick a switch then it is to wait half a day for the events to trigger =)

Thanks for the help people.

/C