Skip to content

Instantly share code, notes, and snippets.

@richturner
Created February 11, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richturner/178cd9689ceabf1bd7abcccb0e3d8d19 to your computer and use it in GitHub Desktop.
Save richturner/178cd9689ceabf1bd7abcccb0e3d8d19 to your computer and use it in GitHub Desktop.
An example Groovy rule for OpenRemote 3.0 for periodically generating sensor values
package demo.rules
import org.openremote.manager.rules.RulesBuilder
import org.openremote.model.asset.*
import org.openremote.model.attribute.*
import org.openremote.model.value.*
import org.openremote.model.rules.*
import java.util.logging.*
Logger LOG = binding.LOG
RulesBuilder rules = binding.rules
Assets assets = binding.assets
String UPDATE_FREQUENCY = "5s" // Note the actual frequency will be longer than this due to scheduling of rule engine firing
rules.add()
.name("Virtual sensor")
.when(
{facts ->
return !facts.matchFirst(String, {it == "VirtualSensor"}).isPresent()
})
.then(
{facts ->
facts.putTemporary(UPDATE_FREQUENCY, "VirtualSensor")
// Push a sensor value (get asset ID from the URL in the manager e.g. /manager#asset:2LBNHqoAUt2TEwb5vDv41l and attribute name by editing the asset and copy paste the attribute name (note in asset view mode a display name is shown for the attribute not the actual attribute name e.g. Microphone Level vs. microphoneLevel)
// The value type sent must match the attribute's value type
// String attributes -> Values.create("value")
// Boolean attributes -> Values.create(true)
// Number attributes -> Values.create(10)
// Parsing arbitrary JSON -> Values.parse("[1, 2, 3, 4]").get()
assets.dispatch(new AttributeEvent("2LBNHqoAUt2TEwb5vDv41l", "microphoneLevel", Values.create(50)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment