How to store output from exec binding as Numbers?

exec
openhab2
binding
Tags: #<Tag:0x00007f1e5c06d4a8> #<Tag:0x00007f1e5c06d1b0> #<Tag:0x00007f1e5c06cf58>
(Aaron K.) #1

I have defined the following Things:

Thing exec:command:apc_status   [command="/sbin/apcaccess -u -p STATUS", interval=10, timeout=5, autorun=false]
Thing exec:command:apc_timeleft [command="/sbin/apcaccess -u -p TIMELEFT", interval=10, timeout=5, autorun=false]
Thing exec:command:apc_loadpct  [command="/sbin/apcaccess -u -p LOADPCT", interval=10, timeout=5, autorun=false]
Thing exec:command:apc_linev    [command="/sbin/apcaccess -u -p LINEV", interval=10, timeout=5, autorun=false]

apc_timeleft, apc_loadpct, and apc_linev return numbers and I’d like to assign them to items of type Number.

The following does not work:

Number APC_Timeleft { channel="exec:command:apc_timeleft:output" }
Number APC_Loadpct { channel="exec:command:apc_loadpct:output" }
Number APC_Linev { channel="exec:command:apc_linev:output" }

I only see values when I define the items as Strings.

Do I need to set up rules to do the typecasting? Or is there an easier approach?

0 Likes

(Hallo Ween) #2

Maybe you can make a string item which gets the exec-Data.

Or you can read out the exec-Data not with an item. You can also make it inside a rule with cron-job.

I make nearly the same with my power-meter. I made tests with all ways of reading out the data. item, thing and rule.

Now i use the thing to do this. But the other ways were working also.

Here is my rule. My power meter sends the data as string to an item.

rule "Alle Stromzähler auslesen - neu"
	when
			Item PowerMeter_new received update // String Item linked to exec:command channel that runs above script
	then
			//logInfo("Powermeter-new", "TEST " + PowerMeter_new)
			var lines = PowerMeter_new.state.toString.split('\n')
			logInfo("Powermeter-new", "alles ok - lines --> " + lines.size('\n'))
			if (lines.size('\n') == 10) {
					val counterStr = lines.get(0).split('#').get(1)
					val supplyStr = lines.get(1).split('#').get(1)
					val consumptionStr = lines.get(4).split('#').get(1)
					val solarcounterStr = lines.get(5).split('#').get(1)
					val solar1Str = lines.get(6).split('#').get(1)
					val solarpowerStr = lines.get(9).split('#').get(1)
					stromzaehler0_bezug.sendCommand(counterStr)
					stromzaehler0_einspeisung.postUpdate(supplyStr)
					stromzaehler0_leistung.postUpdate(consumptionStr)
					stromzaehler1_leistung.postUpdate(solarpowerStr)
					stromzaehler1_leistung1.postUpdate("-" + solarpowerStr)
					stromzaehler1_erzeugung.sendCommand(solarcounterStr)
					stromzaehler1_ungenutzt.postUpdate(solar1Str)
					var hausverbrauch_aktuell = (Double::parseDouble(consumptionStr)) + (Double::parseDouble(solarpowerStr))
					if (hausverbrauch_aktuell < 0) {
							logInfo("Powermeter-new", "Fehler - Verbrauch negativ --> " + hausverbrauch_aktuell + " kW")
							stromzaehler_haus_leistung.postUpdate(0)
					}
					else {
							stromzaehler_haus_leistung.postUpdate(hausverbrauch_aktuell)
					}
			}
			else if ...
...
...
...
end

I get a string with 10 lines, so i did some splitting first to get each value. But you only get a single line with one value. So you don´t need the line with " var lines…" the if and the split-command.

0 Likes

(Aaron K.) #3

Thanks, that’s helpful!

I’ll use a rule-based approach then. Just wanted to make sure that I don’t miss anything obvious that would have made my life easier :slight_smile:

0 Likes