Set Variable for computed attributes

Andreas Kern4 years ago

Hello community,

it it possible to set and use variable in computed attributes?

When i use following code for computed attributes i get "7" as result:
var tempMax = 7; temp1 or temp2 or temp3 or temp4 >= 7 ? "temperature" : (temp1 or temp2 or temp3 or temp4 <= 0 ? "temperature" : null)

Thank s in advance!

Best regards Andreas

Anton Tananaev4 years ago

I don't think variables are supported, but I don't really understand why you need a variable here.

Andreas Kern4 years ago

Hello Anton,
thank you for your reply.

You are right. In this code a variable does not make sense.

I tested another to create specific alarms. But without editing source code it does not work.

temp1 >= 7 ? "temp1TooWarm" : (temp1+2 >= 7 ? "temp1nearTooWarm" : (temp1-2 <= 0 ? "temp1nearTooCold" : (temp1<= 0 ? "temp1TooCold" : null)))

And if i expand the code for more three sensors a variable would be comfortable if min or max values are changed.
It would be much more comfortable if a variable could be add to a device as an attribute and use this in a code for a computed attribute.
For the temperature example one computed attribute could be used for different devices with different min an max values.

Perhaps you could implement customizable attributes for devices in future codes.

Anton Tananaev4 years ago

You can use device attributes, as far as I remember.

Andreas Kern4 years ago

I thought that only attributes from drop down list are possible to add to a device.
But i was wrong. It is possible just to type in a custom name.
So i added attributes

tempMax with value 7

I just tested the following code for computed attribute

temp1 >= tempMax ? "temperature" : (temp1 <= 0 ? "temperature" : null)
Result should be "temperature"

Temperature 1 from Sensor sends a value of 20. I get no result. If i test the computed attribute i get an error message that tempMax is an unknown variable. Do you have got a tip how to call the device attribute?

Anton Tananaev4 years ago

The way you did it looks correct, but for testing I would just print the attribute value instead of using a complicated expression like this.

Andreas Kern4 years ago

It is not possible for me to print device attributes.
They are also not shown in state list and so can't be taken by computed attributes.

Then i created two custom computed attribute and set a value to it.

Attribute: Temp1Max
Expression: 7
Type: Number 
Attribute: Temp1Min
Expression: 0
Type: Number 

I attached the computed attribute to the device and it appears in status list.

Then i created following computed attribute

Attribute: Alarm
Expression: temp1 >= Temp1Max ? "temperature" : (temp1 <= Temp1Min ? "temperature" : null) 
Type: String
Value of temp1 = 20

When i check the computed attribute with the "question mark button" in editor and select the right device where my custom computed attributes are linked to, the right result "temperature" appears in alert window.

But when a new record is received from device i no alarm is generated.

Anton Tananaev4 years ago

To print attribute just use its name as an expression. That's what I meant.

Andreas Kern4 years ago

I think it does not work.

To print a attribute from a device i did following

add attribute to device

Name: MaxTemp1
Value: 7

Created computed attribute and linked it to device

Attribute: Temp1Max
Expression: MaxTemp1
Type: Number

I also tried Type:String

Nothing in State is shown and when i check it in computed attribute editor with question mark button
Error mesage

org.traccar.handler.ComputedAttributesHandler.computeAttribute@102![0,8]: 'MaxTemp1;' undefined variable MaxTemp1 - Variable (... < ComputedAttributesHandler:102 < AttributeResource:57 < ...)

appears.

Anton Tananaev4 years ago

Try setting processing.computedAttributes.deviceAttributes to true in the config.

Andreas Kern4 years ago

Good morning Anton,

that was the missing piece to complete the puzzle.
Device attribute are handled as String and now can be called up in computed attributes.

Thank you very much for your support and ideas!

Here is a summary how to use device attributes as variable in computed attributes:

  1. add
    <entry key='processing.computedAttributes.deviceAttributes'>true</entry>
    to your config.

  2. open device setting

  3. click on Button "Attributes"
    device_settings

  4. click on "+" to add attribute
    device_attribute_list

  5. type in name (without spaces)and value and confirm
    device_attribute_list

  6. now you can create a computed attribute and call up the value by typing in the name of device attribute. do not forget to link the computed attribute to device.
    Here is an example how to print the value of device attribute in status table.
    computed_attribute_edit_custom

Hello Andreas, Anton,

I was able to add the custom attributes using your little how-to. When I test them, on the Computed Attributes page using the "?" button, they return the correct value, for each device. However, they're not showing on the Status panel of any device.

Is there anything I'm missing?

Anton Tananaev3 years ago

Have you linked them to your devices?

Thanks Anton, I was missing that! now it works great.