I have a problem with LUA and linknx. I'd like to convert a counter value to power usage value. This value should be stored in a GAD which is defined in linknx. From the log you can see that the value is calculated correct, but it is overwritten right after.
I've run out of ideas.
Solution from Herrieman will probably work, here is the explanation.
Internally, the value is stored in a "float" variable in linknx (32bit floating point), but if you set a group address, the value will be sent on the KNX bus in the format for "9.xxx" type which is only 16 bit. In this case, some precision will be lost. Linknx will then receive from the bus the value (16bit) it has just sent and update its internal value accordingly. In this case, the value 30552.29 will be rounded to the nearest 16bit floating point value that is 30535.68.
Another solution would be to change the type of SAMPLE from "9.xxx" to "14.xxx" to use 32bit floating point value.
Jean-François
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have a problem with LUA and linknx. I'd like to convert a counter value to power usage value. This value should be stored in a GAD which is defined in linknx. From the log you can see that the value is calculated correct, but it is overwritten right after.
I've run out of ideas.
Linknx Version
linknx -V
linknx 0.0.1.32
- E-mail gateway enabled (with pthread support)
- LUA scripting support enabled
- Log4cpp logging enabled
Object definition
<object type="9.xxx" id="SAMPLE" gad="9/3/0" init="persist" log="true" flags="cwtuf">Sample</object>
Rule
<rule id="TEST">
<condition type="timer" trigger="true">
<every>10s</every>
</condition>
<actionlist type="if-true">
<action type="script">
function round(num, idp)
local mult = 10^(idp or 0);
return math.floor(num * mult + 0.5) / mult;
end;
value = round(tonumber(14229.8 + tonumber(obj("EG_TECH_ZA2_DA")) / 1000),2);
print ("value = ", value);
set("SAMPLE", value);
</action>
</actionlist>
</rule>
Log
2015-05-17 10:12:57,674 INFO > Rule - Evaluate rule TEST
2015-05-17 10:12:57,674 INFO > Condition - TimerCondition evaluated as '0'
2015-05-17 10:12:57,674 INFO > Rule - Rule TEST evaluated as 0, prev value was 1
2015-05-17 10:12:57,674 DEBUG > Rule - Action list 'false' executed for rule TEST
2015-05-17 10:12:57,674 INFO > PeriodicTask - Rescheduled at 2015-5-17 10:13:07 (1431850387)
2015-05-17 10:12:57,674 INFO > Action - Execute LuaScriptAction
2015-05-17 10:12:57,674 DEBUG > LuaScriptAction - Getting object with id=EG_TECH_ZA2_DA
2015-05-17 10:12:57,674 DEBUG > Object - Object (id=EG_TECH_ZA2_DA): get
2015-05-17 10:12:57,674 DEBUG > LuaScriptAction - Object 'EG_TECH_ZA2_DA' has value '16322488'
2015-05-17 10:12:57,675 DEBUG > LuaScriptAction - Setting object with id=SAMPLE
2015-05-17 10:12:57,675 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,675 INFO > KnxConnection - write(gad=9/3/0, buf, len=4)
2015-05-17 10:12:57,675 DEBUG > KnxConnection - Write request sent
2015-05-17 10:12:57,675 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,675 INFO > Object - New value 30535.68 for object SAMPLE (type: 9.xxx)
2015-05-17 10:12:57,675 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,675 INFO > FilePersistentStorage - Writing '30535.68' for object 'SAMPLE'
2015-05-17 10:12:57,675 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,675 INFO > FilePersistentStorage - Writing log'30535.68' for object 'SAMPLE'
2015-05-17 10:12:57,675 DEBUG > LuaScriptAction - Object 'SAMPLE' set to value '30552.29'
2015-05-17 10:12:57,715 DEBUG > KnxConnection - Write from 0.0.0 to 9/3/0: 5d d3
2015-05-17 10:12:57,715 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,715 INFO > Object - New value 30535.68 for object SAMPLE (type: 9.xxx)
2015-05-17 10:12:57,715 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,715 INFO > FilePersistentStorage - Writing '30535.68' for object 'SAMPLE'
2015-05-17 10:12:57,715 DEBUG > Object - Object (id=SAMPLE): get
2015-05-17 10:12:57,715 INFO > FilePersistentStorage - Writing log'30535.68' for object 'SAMPLE'
Hi,
In the object definition , you can delete the gad part.
Like this:
<object type="9.xxx" id="SAMPLE" init="persist" log="true" flags="cwtuf">Sample</object>
This worked for me.
Greetings
Hi,
Solution from Herrieman will probably work, here is the explanation.
Internally, the value is stored in a "float" variable in linknx (32bit floating point), but if you set a group address, the value will be sent on the KNX bus in the format for "9.xxx" type which is only 16 bit. In this case, some precision will be lost. Linknx will then receive from the bus the value (16bit) it has just sent and update its internal value accordingly. In this case, the value 30552.29 will be rounded to the nearest 16bit floating point value that is 30535.68.
Another solution would be to change the type of SAMPLE from "9.xxx" to "14.xxx" to use 32bit floating point value.
Jean-François
Hi herrieman and Jean-François,
I've changed the type to "14.xxx" and it's working like a charm.
Thank you very much.
Manfred