The main thing I am aiming to communicate in this tutorial is the concept of using the logic built into groups to lessen the complexity of rules. By setting up your Items and groups in the following way, you will be able to use one rule for much of the automation in your home. A second rule is needed if you’d like to have the lights adjust to Mode (Time of Day) and outside lux levels. The concept is that each area within a home has triggers and the combined state of those triggers can represent whether there is activity/presence within that area. By putting the triggers into a group, actions can take place when the state of that group changes.
Sensor state change → Area trigger group state change → Rule triggered → Action(s) determined from metadata → Action(s) called
Putting the logic into groups takes some getting used to, but by working with groups you will not need to build the area/room specific logic into your rules. I have been able to build out group logic to fit all of the areas inside and outside of our home for use with lighting (including colors and brightness level based on outside lux), music/sound, and notifications (e.g., when the doors are unlocked for too long). I originally wrote this about two years ago using the rules DSL and stored the mode based light levels in a Hashmap. Moving to the new rule engine and scripted automation, I was able to incorporate Item metadata.
Setup groups and Items
-
Create regular groups named
gArea_Trigger
andgArea_Action
. These groups will hold the respective trigger and action groups. ThegArea_Trigger
group will be used in the rule trigger.Group gArea_Trigger "Area Triggers" Group gArea_Action “Area Actions”
-
Section out your home into areas where a lighting or sound automation would take place.
-
Create an area trigger group for one of the areas using a name ending in “_Trigger”. This group will hold all of the Items that can be used to identify if the area is active or not. All of the Items in a trigger group must be of the same type (Switch or Contact). Reread that last sentence… it’s important! Most of my devices use Z-Wave and have both switch and contact Channels to choose from. Area trigger groups need to have a type of Switch or Contact, an aggregation function, and they need to be members of the
gArea_Trigger
group. When a trigger group is ON or OPEN, the area is active.Group:Switch:OR(ON,OFF) gDS_FamilyRoom_Trigger "Family Room [%s]" (gArea_Trigger)
-
Add the triggering Items to the area trigger group that you have created. If you do not have devices to use for the trigger, you can create dummy Items that can be used to manually trigger the rule.
-
Create another group with the same name as the area trigger group but this time ending in “_Action”. The naming is important, since the rule will be using the area action group associated with the area trigger group based on the name. This group will hold all of the Items in the area that you want to take action on when activity in the area starts and stops. Add this group to the
gArea_Action
group.Group gDS_FamilyRoom_Action "Family Room" (gArea_Action)
-
Add the action Items into the area action group that you have created.
-
Repeat steps 3-6 for each area in your home.
At times, you’ll need to invert the state of an Item or group of Items. To do this, create another group inside of the trigger group using Group:Switch:NAND(ON,OFF)
and place the Item(s) you need to reverse the state for inside this group. For example, when door locks are unlocked, their state is OFF. By adding a lock to a NAND area trigger group, the group will be ON when the door is unlocked (OFF). If you add more than one lock to the group, the group will stay ON until all locks are ON (locked). There are several interesting solutions that can be created using group logic. Experiment with the possibilities and look through the Example Lighting Scenarios in Github. As I find time, I will document more complex scenarios. If you have questions or difficulty, just ask!
Here are some truth tables that are helpful for understanding the states of groups using logic based aggregation functions:
Example Group and Item structure:
Group gArea_Trigger
Group:Switch:OR(ON,OFF) gDS_FamilyRoom_Trigger (gArea_Trigger)
Group:Switch:OR(ON,OFF) gUS_LivingRoom_Trigger (gArea_Trigger)
Group:Switch:OR(ON,OFF) gUS_EntranceGarage_Trigger (gArea_Trigger)
Group:Switch:NAND(ON,OFF) gUS_EntranceGarage_Bathroom_Trigger (gUS_EntranceGarage_Trigger)
Group:Switch:NAND(ON,OFF) gUS_EntranceGarage_Lock_Trigger (gUS_EntranceGarage_Trigger)
gArea_Trigger
|_ gFamilyRoom_Trigger
|_ DS_FamilyRoom_Motion
|_ DS_FamilyRoom_Slider_Contact
|_ gLivingRoom_Trigger
|_ US_LivingRooom_Motion
|_ US_LivingRoom_Slider_Contact
|_ gEntranceGarage_Trigger
|_ US_EntranceGarage_Motion
|_ US_Laundry_Contact
|_ gUS_EntranceGarage_Bathroom_Trigger
|_ US_GuestBathroom_Contact
|_ gUS_EntranceGarage_Lock_Trigger
|_ Lock_GarageAttached_Inner
gArea_Action
|_ gFamilyRoom_Action
|_ gDS_FamilyRoom_Bulbs
|_ gLivingRoom_Action
|_ US_LivingRoom_Dimmer
|_ gEntranceGarage_Action
|_ US_EntranceGarage_Dimmer
Setup your rule
The link below is a full example that will work for everyone out of the box for lighting. By adding metadata, the lights will adjust based on the mode (time of day) and/or outside lux level. I’ve also included examples for speaker and notification area actions, though these are specific to my implementation. Custom area actions are easily integrated. If you build one that you feel will be useful for others, please post it here or submit it as a PR!
There is a lot of more information in github… Area Triggers and Actions.
Visualization
To see the state of the areas, a simple sitemap entry…
Group item=gArea_Trigger
… will give you a view into which are active or inactive. You can drill down into the groups to see which Items are triggering the activity. Depending on your mode and lux settings, an active area does not mean that the lights are turned ON.
This is much better illustrated with an SVG floorplan. Active areas are green and inactive are grey. Note, the sun was bright when I took this screenshot, so most lights are OFF in the active areas of the house.
Related Concepts:
- Scripted Automation
- Helper Libraries
- Group types and states
- Associated Items
- State Machine Driven Groups
- Time of Day
I appreciate any feedback!