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

Hi Krzysztof,

Here’s how to retrieve and update attribute values:

        // Don't forget to add 'rule state' configuration item to the attributes of interest 

        // Get attribute value:
        def inputValue1 = facts.matchFirstAssetState(
                new AssetQuery()
                        .ids("inputAssetId1")
                        .attributeName("inputAttribute1")
        ).flatMap { it.value }.orElse(null)

        // Update attribute value:
        assets.dispatch("outputAssetId1", "outputAttribute1", "outputValue1")
2 Likes

Hello Igor,

Thank you, I’ve managed to get attributes yesterday, I was struggling because lack of “rule state” item on attribute (attribute was not found by AssetQuery()) - great that you pointed it! :slight_smile:

Regards,

Krzysztof

1 Like

Hi Igor,
I already knew about your proposal to get a value for an attribute, but now I would need to get a list of the latest values of an attribute. Let’s say, for example, that this attribute stores daily MAPE values, and I want to get the average of the last month to decide on further action, how could I do it?
I’m trying to learn a bit about Groovy but it seems that ‘universal’ Groovy is not applicable to Openremote, could it be?
Thanks in advance

Hi,

Not sure what you mean by ‘universal’ Groovy, there is a limited sandbox for the groovy engine but it doesn’t do much and all groovy language functionality should be available.

There is the historicFacade exposed to rules which is an instance of the HistoricFacade class and this has a method called getValueDatapoints which takes an AssetDatapointQuery allowing retrieval of average value(s) using the AssetDatapointIntervalQuery, you’ll need to look at the code to know the data schema.