Im not an programming expert but usually I get some simple code together. In this case I dont understand why addition or sum of two Item values throws errors. Any advises from the community?
LOG output
Script execution of rule with UID ‘5642357aaa’ failed: TypeError:
‘org.openhab.core.library.types.DecimalType’ object is not iterable in at line number 6
PV1 and PV2 are not numbers in any sense that Python can understand. They are Java Item Objects. No matter the language, to do math with the State of an Item, you have to pull that state, e.g. PV1.state. So far so good.
But that state is a Java Object too of either DecimalType or QuantityType. The Python sum() function only knows how to deal with Python numbers, not Java Objects. And even in Rules DSL or Java code one needs to do extra to do math with Number Item’s states, particularly if they carry a unit of measurement.
So how are these Items defined? Do they have units? Are the units compatible if so? Whether or not they carry units changes how to do math with them.
OK, that’s a start. But just because it’s a Number doesn’t mean it’s not carrying units. Do these Items log with just a number or a number with units in events.log.
Assuming that there are no units you can call .intValue() or .floatValue() to get the state as a primitive.
Note, it’s shorter and a lot easier to read usually to use the items dict which is a dictionary of all Items and their current states. So
Yeah, I thought I already tried .intvalue() recently. Indeed I remember getting that error…
2021-06-10 21:30:02.655 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘5642357aaa’ failed: TypeError: ‘int’ object is not iterable in at line number 6
After removing sum statement (just to see whats written into log, I see that PV values are unit-less.
Also tried .intFloat() which results in similar error message - as expected. I think I just need a general method to manage such kind of Java Objects in python scripts.
Can someone help me out getting it solved?