Hi there,
as explained in the quoted thread, I’m using a SML Reader for reading the power meter eHZ. for that I’m using a short c-program, which reads the Output and returns a string with the values.
I’d like to have the logic in a OH2 rule, which executes the script and updates the items accordingly. Therefore I’m trying to set up rule, which
- executes the program
- reads the string
- splits it and reads the values
- updates the items
The string, which is read out Looks like this:
1-0:1.8.0*255#19008.303#
1-0:1.8.1*255#19008.303#
1-0:16.7.0*255#0.491#
first line is the readout of the counter, I’m not sure about the difference to the second line, which is exactly the same value. the third line is the current power consumption.
I have an EDL21 Reader, but I assume, there are others, which will return other values, so for my rule I thought, we need an delimiter for the payload (in my case “#”), which splits the string. I also thought of an Hash-Table connecting the ID of the splitted string with an OH2-item. My problem is, I’m not so deep into XTEND, and also am the Jon Snow of Java (e.g. I know nothing). So my rule at presend looks like this:
Number Sensoren_Leist_Strom "Stromverbrauch [%.1f kwh]" (SensorenTemp)
Number Sensoren_Status_Strom "Stromzählerstand [%.1f kwh]" (SensorenTemp)
rule "Stromzähler"
when
Time cron "0 */1 * * * ?" // cycle every minute
then
// configuration per SML reader
var String smlreader = "/usr/src/sml_server" // path to the sml_server program
var String delimiter = "#" // delimiter for the values
val meter_map = newLinkedHashMap(1 -> 'Sensoren_Status_Strom', 5 -> 'Sensoren_Leist_Strom') // "ID of value" -> "OH2 item"
var Number i = 0
// getting the payload of the meter
// var String meter_payload = executeCommandLine(smlreader, 5000) // deactivated, use next string for tests:
var String meter_payload = "1-0:1.8.0*255#19008.303#
1-0:1.8.1*255#19008.303#
1-0:16.7.0*255#0.491#
"
// splitting the payload
val meter_segments = meter_payload.split(delimiter)
while (i < meter_segments.size()){
if (meter_map.get(i) != NULL) {
sendCommand(meter_map.get(i), meter_segments.get(i))
}
i = i +1
}
end
Ich erhalte aber immer diese Meldung:
2017-01-22 17:09:00.063 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Stromzähler: An error occured during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ArrayExtensions.get(T[],int) on instance: null
This refers to meter_map.get(i), because with “0” there’s no entry in the HashMap. I don’t know, how to implement the excepter catcher!
Thanks for help!