Computed attributes represent another possibility to quickly and effortlessly prepare data already during the process of processing them into the CitySys system. It is a very efficient tool that allows you to asynchronously calculate a value from static or measured fields.
Typical examples of use are e.g. electricity consumption in local currency, where we calculate real financial costs through tariff prices and consumed energy.
It is similar, for example, when calculating OEE – Quality, where we can express the value of the parameter as a percentage through the ratio of qualitatively identical manufactured pieces / number of total manufactured pieces * 100.
You can certainly find other useful examples yourself, and then we will briefly show how to define it in the Entity Type. It is important to remember the basic facts:
- it is applicable for Attributes with a parameter “type” : “computed”
- only Attributes with a parameter can be included in the field calculation “type” : “static” or “type” : “telemetry”
- the values are calculated asynchronously, but they are always added up, so the calculation can sometimes take more than 1-2 seconds
- it is also possible to count with the attributes of other entities, but the condition is that the Entities are in the same logical line (at the same or higher level).
If you use a context Attribute from the current Entity instance and the result of the expression depends on its value and needs to be evaluated every time the value changes, you must add a parameter computed_attributes with a reference to the name of the computed attribute to the definition of the dependent attribute.
The following example describes the calculation of costs in local currency for the Electricity Meter Entity that measures the Total Energy Attribute. The attributes in the “computed_attributes” section determine which Computed fields are to be calculated if a value is entered in the “total_energy” field.
Let’s look at the definitions of the Attributes of the selected fields contained in computed_attributes.
... "ee_dane_var_EUR": { "type": "computed", "unit": "€", "trans": { "en": "Taxes - var.part", "sk": "Dane - var.časť", "de": "Steuern - var. Teil", "cz": "Daně - var. část" }, "evaluation": { "context": { "mega": 1000000 }, "entity_ids": { "tarify": 864 }, "expression": "(entity.attributes['total energy'] / mega) * tarify.attributes['ee_dane_var']" }, "value_type": "number", "value_default": 0 }, },
... "total_energy": { "type": "telemetry", "unit": "Wh", "trans": { "en": "Total Energy", "sk": "Celková Energia", "de": "Gesamtenergie", "cz": "Celková Energie" }, "value_type": "number", "value_default": 0, "computed_attributes": { "ee_dane_var_EUR": {}, "ee_total_power_kW": {}, "ee_total_power_EUR": {}, "ee_odchylka_var_EUR": {}, "ee_poplatky_tps_EUR": {}, "ee_poplatky_tss_EUR": {}, "ee_obchodnik_var_EUR": {}, "ee_komodita_dokup_EUR": {}, "ee_total_power_target_kW": {}, "ee_distribucia_el_njf_EUR": {}, "ee_distribucia_var_vn_EUR": {}, "ee_distribucia_straty_vn_EUR": {} } ...
The following example shows the definition of an Attribute with a conversion to kW, also an Attribute contained in computed_attributes.
... "ee_total_power_kW": { "type": "computed", "unit": "kW", "trans": { "en": "Total Power in kW", "sk": "Celkový Výkon v kW" }, "evaluation": { "context": { "kilo": 1000 }, "expression": "entity.attributes['total_power'] / kilo" }, "value_type": "number", "value_default": 0 }, ...
If necessary, it is also possible to define local variables via the “context” parameter (such as the kilo variable). The calculation formula itself is defined using the “expression” parameter.
... "context": { "kilo": 1000 }, "expression": "entity.attributes['total_power'] / kilo" ...
If it is necessary to include in the calculation an Attribute of an entity other than the one defined, it is possible to use the Parameter “entity_ids“, where you define the ID of the selected Entity. (see following image.)
... "entity_ids": { "tarify": 864 }, ...
In case you need to process more entities, the “entity_ids” field can be defined as an object with multiple values. (See the following picture)
... "entity_ids": { "entities": [ 890, 891, 892, 893, 894, 904, 905 ] }, ...
Then the order of the objects in the “entities” definition is taken into account in the “expression” parameter in the patterns.
... "expression": "(entities[0].attributes['total power'] + entities[1].attributes['total power'] + entities[2].attributes['total power'] + entities[3].attributes['total power'] + entities[4].attributes['total power'] + entities[5].attributes['total power'] + entities[6].attributes['total power'] + entities[7].attributes['total power']) / kilo" ...