We add a second function to our script that triggers upon the update and give it a unique rule name. We use a predefined “constant” for the cron expression and we import yet another trigger type from lucid.triggers
named ItemStateUpdateTrigger
. As you know, an item update is not the same as an item change. In case you need a recap.
Update your scripts to this:
from lucid.rules import rule, addRule # Needed for defining and adding a rule
from lucid.triggers import ItemStateChangeTrigger, ItemStateUpdateTrigger, CronTrigger # Import the different type of triggers that you will be using
from lucid.utils import getItemValue
@rule
class StepByStep(object): # Giving the class a unique name
def getEventTriggers(self):
return [
ItemStateChangeTrigger('My_TestSwitch_1'), # Triggering when the switch changes its state.
ItemStateChangeTrigger('My_TestSwitch_2', ON), # Only trigger when switch turns on
CronTrigger(EVERY_30_SECONDS), # Using a cron expression constant
]
def execute(self, modules, inputs):
self.log.setLevel(DEBUG)
if self.event.isItem:
self.log.debug('One of the test switches has changed its state')
self.log.debug('Triggering item name: \'' + unicode(self.event.itemName) + '\', state: ' + str(self.event.state))
# Only set Test_String_1 to "CAT" when both switches are ON and only when an item trigger has occurred. (Not at cron events)
bothSwitchesAreOn = ((getItemValue("My_TestSwitch_1", OFF) == ON) and (getItemValue("My_TestSwitch_2", OFF) == ON))
if bothSwitchesAreOn:
events.postUpdate("Test_String_1", "CAT")
self.log.debug('CAT!!!')
elif event.isCron:
self.log.debug('A cron event occurred')
events.postUpdate("Test_String_1", "DOG")
addRule(StepByStep()) # Needed to add the rule, use the same name as defined for the class
@rule
class WatchMyString(object): # Giving the class a unique name
def getEventTriggers(self):
return [ItemStateUpdateTrigger('Test_String_1')] # Triggering when the Test_String_1 items updates
def execute(self, modules, inputs):
self.log.setLevel(DEBUG)
self.log.debug('Item: \'' + unicode(self.event.itemName) + '\', was updated')
addRule(WatchMyString()) # Needed to add the rule, use the same name as defined for the class
After you’ve saved the new script, you’ll notice in the log that Test_String_1
gets an update every 30 seconds!
We don’t want that to happen.
When we send an update to the Test_String_1
in the first rule, we’d like to check first if the new value differs from the value stored so that the second rule only triggers whenever there is an actual change.
It can be done by inspecting the old value together with an if statement. However we will use another handy function in lucid.utils
named postUpdateCheckFirst
.
# Checks if the current state of the item is different than the desired new state.
# If the target state is the same, no update is posted.
Use it like this: postUpdateCheckFirst('Item', 'New Value')
Your job is to modify the script to do that.