# The lucid step_by_step script will take you through some very basic steps using lucid and Jython # This is the starting script and we will slowly add some complicity. from lucid.rules import rule, addRule # Needed for defining and adding a rule from lucid.triggers import ItemStateChangeTrigger # Import the different type of triggers that you will be using # First we add a rule that triggers upon the change of any of 2 switches. # To run this, you need to define 2 openHAB switch items and name them "My_TestSwitch_1" and "My_TestSwitch_2" # Add them to a site map so that you can operate them and watch the debug output. # You will find that the script won't trigger when My_TestSwitch_2 when changes from ON to OFF @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 ] def execute(self, modules, inputs): self.log.info('One of the test switches has changed its state' ) # That's all we do addRule(StepByStep() ) # Needed to add the rule, use the same name as defined for the class
from lucid.rules import rule, addRule from lucid.triggers import CronTrigger @rule class HelloWorld(object): def getEventTriggers(self): return [ CronTrigger(EVERY_MINUTE), # Runs every minute ] def execute(self, modules, inputs): self.log.info('Hello world from lucid!') addRule(HelloWorld())
cmd += '--data-urlencode "windgustmph_10m=' + windgustmph_10m + '" ' self.log.debug("windgustmph_10m: " + windgustmph_10m) if windgustdir_10m is not None: cmd += '--data-urlencode "windgustdir_10m=' + windgustdir_10m + '" ' self.log.debug("windgustdir_10m: " + windgustdir_10m) if solarradiation is not None: cmd += '--data-urlencode "solarradiation=' + solarradiation + '" ' self.log.debug("solarradiation: " + solarradiation) cmd += ' 1>/dev/null 2>&1 &' self.log.debug("") if self.config.wunderground['stationdata']['weather_upload']: self.log.debug('WeatherUpload version ' + SCRIPT_VERSION + ', performing an upload. (loop count is: ' + str(wu_loop_count) + ')') self.log.debug('cmd: ' + cmd) os.system(cmd) else: self.log.debug('WeatherUpload version ' + SCRIPT_VERSION + ', skipping upload. (loop count is: ' + str(wu_loop_count) + ')') if (wu_loop_count % self.config.wunderground['stationdata']['upload_frequency'] == 0): wu_loop_count = 0 wu_loop_count = wu_loop_count + 1 addRule(WeatherUpload())
from lucid.rules import rule, addRule from lucid.triggers import ItemStateUpdateTrigger from lucid.speak import tts @rule class SpeakThisPhrase(object): """ Make openHAB speak a message that you've posted to the openHAB REST API from an external host This script watches an openHAB string item, e.g: String Speak_This "Speak this [%s]" <text> Use curl from an external host to set the item to the text string that shall be spoken e.g. curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "Hello world" "http://OPENHABHOST:8080/rest/items/Speak_This/state" """ def getEventTriggers(self): return [ItemStateUpdateTrigger('Speak_This')] def execute(self, modules, inputs): tts(self.event.state) addRule(SpeakThisPhrase())
# Add entries to the config file using your favorite editor. # The syntax for the configuration file is pure python 2.7. from lucid.rules import rule, addRule from lucid.triggers import CronTrigger @rule class readConfigExample(object): def getEventTriggers(self): return [CronTrigger(EVERY_MINUTE)] # Runs every minute def execute(self, modules, inputs): self.log.setLevel(DEBUG) # Set the logging level to DEBUG # Get a single value: sonsorName = self.config.wunderground['sensors']['tempc'] self.log.debug('Read sensorname from the lucid config file: ' + sonsorName) # wunderground['sensors'] is a dictionary. Let's iterate through it self.log.debug( 'Iterate through a dictionary in the lucid configuration file') for the_key, the_value in self.config.wunderground[ 'sensors'].iteritems(): self.log.debug('Key: ' + the_key + ', Value: ' + the_value) addRule(readConfigExample())
from lucid.rules import rule, addRule from idealarm import ideAlarm from lucid.utils import hasReloadFinished @rule class ideAlarmTrigger(object): """Make ideAlarm trigger on item changes""" def getEventTriggers(self): return ideAlarm.getTriggers() def execute(self, modules, inputs): if not hasReloadFinished(True): return ideAlarm.execute(self, modules, inputs) addRule(ideAlarmTrigger())
from lucid.triggers import ItemStateUpdateTrigger, CronTrigger @rule class ShowSomeEventInfo(object): def getEventTriggers(self): return [ ItemStateUpdateTrigger('Button_Box_Sw_4'), CronTrigger(EVERY_MINUTE), # Runs every minute ] def execute(self, modules, inputs): self.log.setLevel(DEBUG) if self.event.isItem: self.log.debug('Triggering item name: \'' + unicode(self.event.itemName) + '\', state: ' + str(self.event.state)) item = self.event.item # Get the item object for item that caused the event self.log.debug('item: ' + unicode(item.name) + ' isActive(): ' + str(self.event.isActive)) self.log.debug('item: ' + unicode(item.name) + ' has a new state: ' + str(item.state)) self.log.debug('event type: ' + self.event.type) self.log.debug('This was a cron event: ' + str(self.event.isCron)) self.log.debug('This was a command event: ' + str(self.event.isCommand)) self.log.debug('This was an update event: ' + str(self.event.isUpdate)) addRule(ShowSomeEventInfo())
@rule class SayHello(object): def getEventTriggers(self): return [ CronTrigger(EVERY_MINUTE), # Runs every minute ] def execute(self, modules, inputs): greetings = [greeting(), 'Hello', 'How are you', 'How are you doing', 'Good to see you', 'Long time no see', 'It\’s been a while'] peopleAtHome = [] for member in itemRegistry.getItem('G_Presence_Family').getAllMembers(): if member.state == OPEN: peopleAtHome.append(member.label) random.shuffle(peopleAtHome) msg = random.choice(greetings) for i in range(len(peopleAtHome)): person = peopleAtHome[i] msg += ' '+person if i+2 == len(peopleAtHome): msg +=' and' elif i+1 == len(peopleAtHome): msg +='.' elif i+2 < len(peopleAtHome): msg +=',' #tts(msg, PRIO['HIGH'], ttsRoom='Kitchen', ttsVol=42, ttsLang='en-GB', ttsVoice='Brian') tts(msg, PRIO['HIGH'], ttsRoom='Kitchen', ttsVol=42, ttsLang='en-IN', ttsVoice='Aditi') #tts(msg, PRIO['HIGH'], ttsRoom='Kitchen', ttsVol=42, ttsLang='en-US', ttsVoice='Matthew') #tts(msg, None, ttsRoom='All', ttsLang='de-DE', ttsVoice='Vicki') #tts(msg) # Also works if you accept the defaults addRule(SayHello())