openhab-vscode/snippets/openhabrules.json

256 lines
9.9 KiB
JSON

{
"rule": {
"prefix": "rule",
"body": [
"rule \"${1:rule name}\"",
"when",
"\t${2}",
"then",
"\t${3}",
"end"
],
"description": "Add a quick openHAB textual rule"
},
"channel": {
"prefix": "chan",
"body": "Channel \"${1:example:channel:id}\" triggered ${2}",
"description": "Channel \"example:channel:id\" triggered - place in 'when' section"
},
"item changed": {
"prefix": "it",
"body": "Item ${1:Some_Item} changed ${2}",
"description": "Item Some_Item changed - place in 'when' section"
},
"item received command": {
"prefix": "irc",
"body": "Item ${1:Some_Item} received command ${2}",
"description": "Item Some_Item received command - place in 'when' section"
},
"item received update": {
"prefix": "iru",
"body": "Item ${1:Some_Item} received update ${2}",
"description": "Item Some_Item received update - place in 'when' section"
},
"system started": {
"prefix": "ss",
"body": "System started",
"description": "System started - place in 'when' section"
},
"system shuts down": {
"prefix": "ssd",
"body": "System shuts down",
"description": "System shuts down - place in 'when' section"
},
"time": {
"prefix": "cron",
"body": "Time cron \"${1:cron_expression}\"",
"description": "Time cron expression - place in 'when' section"
},
"sendCommand": {
"prefix": "sc",
"body": "sendCommand(${1:Item}, ${2:COMMAND})",
"description": "Send command to an item"
},
"postUpdate": {
"prefix": "pu",
"body": "postUpdate(${1:Item}, ${2:COMMAND})",
"description": "Post update to an item"
},
"if": {
"prefix": "if",
"body": "if( ${1} ) {\n\t${2}\n}",
"description": "Simple if statement"
},
"if else": {
"prefix": "ife",
"body": "if( ${1} ) {\n\t${2}\n} else {\n\t${3}\n}\n",
"description": "Simple if else statement"
},
"simple lambda": {
"prefix": "lam",
"body": [
"import org.eclipse.xtext.xbase.lib.Functions",
"",
"/**",
" * Details: https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888",
" */",
"val Functions\\$Function1<GenericItem, String> ${1:lambdaName} = [ s |",
"\t// logInfo(\"lambda\", s.state.toString)",
"\ttrue",
"]",
"// Usage within a rule: ${1:lambdaName}.apply(${2:Some_Item})"
],
"description": "Reusable Functions: A simple lambda."
},
"logDebug": {
"prefix": "logDebug",
"body": "logDebug(\"${1:loggerName}\", \"${2:message}\")",
"description": "logDebug(String loggerName, String format, Object... args)"
},
"logInfo": {
"prefix": "logInfo",
"body": "logInfo(\"${1:loggerName}\", \"${2:message}\")",
"description": "logInfo(String loggerName, String format, Object... args)"
},
"logWarn": {
"prefix": "logWarn",
"body": "logWarn(\"${1:loggerName}\", \"${2:message}\")",
"description": "logWarn(String loggerName, String format, Object... args)"
},
"logError": {
"prefix": "logError",
"body": "logError(\"${1:loggerName}\", \"${2:message}\")",
"description": "logError(String loggerName, String format, Object... args)"
},
"persist": {
"prefix": "persist",
"body": "${1:Item}.persist",
"description": "<item>.persist - Persists the current state"
},
"lastUpdate": {
"prefix": "lastUpdate",
"body": "${1:Item}.lastUpdate",
"description": "<item>.lastUpdate - Query for the last update timestamp of a given item."
},
"historicState": {
"prefix": "historicState",
"body": "${1:Item}.historicState(${2:AbstractInstant})",
"description": "<item>.historicState(AbstractInstant) - Retrieves the historic item at a certain point in time"
},
"changedSince": {
"prefix": "changedSince",
"body": "${1:Item}.changedSince(${2:AbstractInstant})",
"description": "<item>.changedSince(AbstractInstant) - Checks if the state of the item has (ever) changed since a certain point in time"
},
"updatedSince": {
"prefix": "updatedSince",
"body": "${1:Item}.updatedSince(${2:AbstractInstant})",
"description": "<item>.updatedSince(AbstractInstant) - Checks if the state of the item has been updated since a certain point in time"
},
"maximumSince": {
"prefix": "maximumSince",
"body": "${1:Item}.maximumSince(${2:AbstractInstant})",
"description": "<item>.maximumSince(AbstractInstant) - Gets the Item with the maximum value (state) since a certain point in time"
},
"minimumSince": {
"prefix": "minimumSince",
"body": "${1:Item}.minimumSince(${2:AbstractInstant})",
"description": "<item>.minimumSince(AbstractInstant) - Gets the Item with the minimum value (state) since a certain point in time"
},
"averageSince": {
"prefix": "averageSince",
"body": "${1:Item}.averageSince(${2:AbstractInstant})",
"description": "<item>.averageSince(AbstractInstant) - Gets the average value of the state of a given item since a certain point in time."
},
"deltaSince": {
"prefix": "deltaSince",
"body": "${1:Item}.deltaSince(${2:AbstractInstant})",
"description": "<item>.deltaSince(AbstractInstant) - Gets the difference value of the state of a given item since a certain point in time."
},
"previousState": {
"prefix": "previousState",
"body": "${1:Item}.previousState()",
"description": "<item>.previousState() - Retrieves the previous item (returns HistoricItem).\n<item>.previousState(true) - Retrieves the previous item, skips items with equal state values and searches the first item with state not equal the current state (returns HistoricItem)."
},
"sumSince": {
"prefix": "sumSince",
"body": "${1:Item}.sumSince(${2:AbstractInstant})",
"description": "<item>.sumSince(AbstractInstant) - Retrieves the sum of the previous states since a certain point in time. (OpenHab 1.8)"
},
"concurrency guard": {
"prefix": "concurrency",
"body": [
"import java.util.concurrent.locks.ReentrantLock",
"",
"var java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock()",
"",
"rule ConcurrentCode",
"when",
"\tItem ${1:Item} received update",
"then",
"\tlock.lock()",
"\ttry {",
"\t\t// do stuff (e.g. create and start a timer ...)",
"\t} finally{",
"\t\tlock.unlock()",
"\t}",
"end"
],
"description": "If a rule triggers on UI events it may be necessary to guard against concurrency."
},
"design pattern: time of day": {
"prefix": "dp time",
"body": [
"val logName = \"weather\"",
"",
"/**",
" * Details: https://community.openhab.org/t/design-pattern-time-of-day/15407",
" */",
"rule \"Calculate time of day state\"",
"when",
"\tSystem started or",
"\tChannel 'astro:sun:home:rise#event' triggered START or",
"\tChannel 'astro:sun:home:set#event' triggered START or",
"\tChannel 'astro:sun:minus90:set#event' triggered START or",
"\tTime cron \"0 0 6,23,0 * * ? *\" // there is currently a bug where only one cron is triggered per rule so I've combined all three into one",
"then",
"\tThread::sleep(1000) // make sure we are a tad past midnight to give Astro a chance to recalculate DateTimes for today",
"",
"\tval long morning_start = now.withTimeAtStartOfDay.plusHours(6).millis",
"\tval long day_start = (vSunrise_Time.state as DateTimeType).calendar.timeInMillis",
"\tval long afternoon_start = (vEvening_Time.state as DateTimeType).calendar.timeInMillis",
"\tval long evening_start = (vSunset_Time.state as DateTimeType).calendar.timeInMillis",
"\tval long night_start = now.withTimeAtStartOfDay.plusHours(23).millis",
"\tval long bed_start = now.withTimeAtStartOfDay.millis",
"",
"\tvar curr = \"UNKNOWN\"",
"",
"\tswitch now {",
"\t\t\t\tcase now.isAfter(morning_start) && now.isBefore(day_start): curr = \"MORNING\"",
"\t\t\t\tcase now.isAfter(day_start) && now.isBefore(afternoon_start): curr = \"DAY\"",
"\t\t\t\tcase now.isAfter(afternoon_start) && now.isBefore(evening_start): curr = \"AFTERNOON\"",
"\t\t\t\tcase now.isAfter(evening_start) && now.isBefore(night_start): curr = \"EVENING\"",
"\t\t\t\tcase now.isAfter(night_start): curr = \"NIGHT\"",
"\t\t\t\tcase now.isAfter(bed_start) && now.isBefore(morning_start): curr = \"BED\"",
"\t}",
"",
"\tif(vTimeOfDay.state.toString != curr) {",
"\t\tlogInfo(logName, \"Current time of day is now \" + curr)",
"\t\tvTimeOfDay.sendCommand(curr)",
"\t}",
"",
"end",
""
],
"description": "Design Pattern: Time of Day - openHAB 2 Astro 2.0 Example by Rich Koshak"
}
}