Groovy rule based on values of 2 assets

Hello,

I’m new to OpenRemote platform but it seems very powerful and seems to fit my needs regarding some project. Currently I’m facing problem with Groovy rule which should modify asset X custom attribute “atr3” (Number) as caclulation (let’s say sum for a simplicity) based on values “atr1” and “atr2” of this asset and “atr4” of asset Y (atr3=atr1+atr2+atr4). I’ve managed to change value of attribute “atr3” to defined constant everytime “atr1” is being updated but having problem how to get and use values of “atr1”, “atr2” (from asset X) and “atr4” (from asset Y) for a full calculation formula. I have no experience in Groovy/Java language and little help would be very appreciated.

Regards,

Krzysztof

Hi!

Have you tried this with the Flow Rules feature in the Manager UI?
We use this ourselves to make simple calculations, so it might be a good fit.

.
However, if this does not fit your needs, Groovy can provide flexibility :slight_smile:
You can look at the examples we made here and here, or look at the official docs:

.
Let me know if you need help :wink:

Hi, thank you for answer :slight_smile:

Unfortunately simple Flow Rules are not suitable for calculations I need to make. I must use Groovy.
To precise my question: how can I get current value of attribute “atr2” from asset “X” and “atr4” from asset “Y” (I would like to reference to assets by their id and to attributes by their name) in “then” section of Groovy rule? What objects and methods should I use?

I’ve checked examples you suggested but unfortunately can’t clearly undestand how to use them in my (much simplier - I think) case.

In another thread I found someting I’m thinking of:

def co = assets.matchFirstAssetState(new AssetQuery().ids(“2zhjdjqTWSONZYMcuLA71o”)).getAttributeValue(“co_pred”)

modified to my case (variable name,asset Id, asset attribute name):

def tmtValue = assets.matchFirstAssetState(new AssetQuery().ids(“5cm7aGF2j8FGlMNFDfkaDL”)).getAttributeValue(“Temperature”)

but it doesn’t work:

Error executing rules of: RulesetDeployment{id=1100, name=‘DR01-TMT’, version=18, status=DEPLOYED} – Error executing action of rule ‘DR01-TMT’: No signature of method: org.openremote.manager.rules.facade.AssetsFacade.matchFirstAssetState() is applicable for argument types: (org.openremote.model.query.AssetQuery) values: [AssetQuery{select=null, ids=[5cm7aGF2j8FGlMNFDfkaDL], name=null, parent=null, path=null, realm=null, userId=‘null’, type=null, attribute=null, orderBy=null, recursive=false}]

Regards,

Krzysztof