# this is a single line comment
'''
this is
a multiline
comment
'''
itemRegistry.getItem("My_Item")
or using the alias to itemRegistry...
ir.getItem("My_Item")
ir.getItem("My_Item").state
or...
items["My_Item"]
or (using the core
package)...
items.My_Item
ir.getItem(event.itemName)
event.itemName
event.itemState
event.oldItemState
event.itemCommand
Send a command to an item (more options):
events.sendCommand("Test_SwitchItem", "ON")
Send an update to an item (more options):
events.postUpdate("Test_SwitchItem", "ON")
def testFunction(event)
if event.oldItemState == UnDefType.NULL:
return
# do stuff
from org.joda.time import DateTime
start = DateTime.now()
Persistence extensions (others not listed are similar):
from core.actions import PersistenceExtensions
PersistenceExtensions.previousState(ir.getItem("Weather_SolarRadiation"), True).state
from org.joda.time import DateTime
PersistenceExtensions.changedSince(ir.getItem("Weather_SolarRadiation"), DateTime.now().minusHours(1))
PersistenceExtensions.maximumSince(ir.getItem("Weather_SolarRadiation"), DateTime.now().minusHours(1)).state
Use other Core & Cloud Actions:
from org.eclipse.smarthome.model.script.actions.Exec import executeCommandLine
executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s --connect-timeout 3 --max-time 3 http://some.host.name",5000)
from org.eclipse.smarthome.model.script.actions.HTTP import sendHttpPutRequest
sendHttpPutRequest("someURL.com, "application/json", '{"this": "that"}')
from core.actions import Audio
Audio.playSound("doorbell.mp3")# using the default audiosink
Audi.playSound("my:audio:sink", "doorbell.mp3")# specifying an audiosink
Audio.playStream("http://myAudioServer/myAudioFile.mp3")# using the default audiosink
Audio.playStream("my:audio:sink", "http://myAudioServer/myAudioFile.mp3")# specifying an audiosink
from core.actions import NotificationAction
NotificationAction.sendNotification("[email protected]","This is the message")
NotificationAction.sendBroadcastNotification("This is the message")
NotificationAction.sendLogNotification("This is the message")
from core.actions import Mail
Mail.sendMail("[email protected]","This is the message")
from core.actions import Transformation
Transformation.transform("JSONPATH", "$.test", test)
from core.actions import Voice
Voice.say("This will be said")
from core.actions import ThingAction
ThingAction.getThingStatusInfo("zwave:device:c5155aa4:node5")
See the timer_example.py
in the Script Examples for examples of using both Jython and the createTimer
Action.
from core.actions import Telegram
Telegram.sendTelegram("MyBot", "Test")
from core.actions import Mail
Mail.sendMail("[email protected]", "This is the subject", "This is the message")
from org.slf4j import Logger, LoggerFactory
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
log.debug("Test debug log")
log.info("Test info log")
log.warn("Test warn log")
log.error("Test error log")
or using the log module, which logs to org.eclipse.smarthome.automation.jsr223.jython...
from core.log import logging, LOG_PREFIX
log = logging.getLogger(LOG_PREFIX + ".TEST")
log.debug("This is a test log")
items["String_Item"] == StringType("test string")
items["Switch_Item"] == OnOffType.ON
items["Number_Item"] > DecimalType(5)
items["Contact_Item"] == OpenClosedType.OPEN
items["Some_Item"] != UnDefType.NULL
event.itemState <= DecimalType(event.oldItemState.intValue() + 60)
event.itemState <= DecimalType(event.oldItemState.doubleValue() + 60)
event.itemState <= DecimalType(event.oldItemState.floatValue() + 60)
int(str(items["Number_Item1"])) + int(str(items["Number_Item2"])) > 5
items["Number_Item1"].intValue() + items["Number_Item2"].intValue() > 5
float(str(items["Number_Item"])) + 5.5555 > 55.555
items["Number_Item"].floatValue() + 5.5555 > 55.555
from time import sleep
sleep(5)# the unit is seconds, so use 0.5 for 500 milliseconds
ir.getItem("gTest").members
ir.getItem("gTest").allMembers
for item in ir.getItem("gTest").members:
#do stuff
listOfMembers = filter(lambda item: item.state == OnOffType.ON, ir.getItem("gTest").members)
filter(lambda item: item.state == OnOffType.ON, ir.getItem("gTest").members)[0]
filter(lambda item: item.state == OnOffType.OFF, ir.getItem("gTest").members)[0:5]
sortedBatteryLevel = sorted(battery for battery in ir.getItem("gBattery").getMembers() if battery.state < DecimalType(5), key = lambda battery: battery.state)
map(lambda lowBattery: "{}: {}".format(lowBattery.label, str(lowBattery.state) + "%"), ir.getItem("gBattery").members)
# the state.add(state) is a method of QuantityType
reduce(lambda sum, x: sum.add(x), map(lambda rain: rain.state, ir.getItem("gRainWeeklyForecast").members))
lowBatteryMessage = "Warning! Low battery alert:\n\n{}".format(",\n".join(map(lambda lowBattery: "{}: {}".format(lowBattery.label,str(lowBattery.state) + "%"), sorted(battery for battery in ir.getItem("gBattery").getMembers() if battery.state < DecimalType(5), key = lambda battery: battery.state))))
https://community.openhab.org/t/jsr223-jython-using-item-metadata-in-rules/53868
from org.slf4j import Logger, LoggerFactory
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
log.debug("JSR223: Test dir(object)=[{}]".format(dir(object)))
scriptExtension.importPreset("RuleSupport")
ruleUID = filter(lambda rule: rule.name == "This is the name of my rule", rules.getAll())[0].UID
from core import osgi
ruleEngine = osgi.get_service("org.eclipse.smarthome.automation.RuleManager")
ruleEngine.setEnabled(ruleUID, True)# enable rule
ruleEngine.setEnabled(ruleUID, False)# disable rule
from core import osgi
ruleEngine = osgi.get_service("org.eclipse.smarthome.automation.RuleManager")
considerConditions = False# consider the rule's Conditions
ruleEngine.runNow(ruleFunction.UID)# without inputs
ruleEngine.runNow(ruleFunction.UID, considerConditions, {'name': 'EXAMPLE'})# with inputs