Exec Binding - Read value from file/bash script


I want read value from file.


Number ac_current_temp "Current [%.1f]" (AC)   { channel="exec:command:ac_get_temp:output" }


Thing exec:command:ac_get_temp [command="/etc/openhab2/samsungac/parse_status_file.sh", interval=60, timeout=5] // run every minute


echo "34"

It doesent works… What is wrong?

The exec binding output is a string


Try changing your item to:

String ac_current_temp "Current [%.1f]" (AC)   { channel="exec:command:ac_get_temp:output" }

Works with:

String ac_current_temp "Current [%s]" (AC)   { channel="exec:command:ac_get_temp:output" }

How I can work on Integer value? I want use if (greater) statment.

You need another item

Number ac_current_temp_number

And convert your String item to a number is a rule:

rule "ac temp to number"
    Item ac_current_temp changed
    ac_current_temp_number.postUpdate((ac_current_temp.state) as Number)
1 Like

Revisiting this one if I may.

My Thing

Thing exec:command:pwrFeedIn 	[command="bash /home/openhabian/scripts/pwrFeedIn.sh", interval=60, transform="REGEX((.*?))"]

My Items

    String pwrFeedInSt	"Feed-In Power : [%s W] "	   <energy>   (PowerCons)   	{channel="exec:command:pwrFeedIn:output"}
    Number pwrFeedIn	"Feed-In Power "

My rule

    rule "powers to number"
        Item pwrFeedInSt changed
        pwrFeedIn.postUpdate((pwrFeedInSt.state) as Number)

Result from log

    10:23:49.189 [INFO ] [smarthome.event.ItemStateChangedEvent] - pwrFeedInSt changed from 1073 to 336
    10:23:49.224 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'powers to number': Could not cast 336 to java.lang.Number; line 224, column 26, length 29


pwrFeedInSt (Type=StringItem, State=94, Label=Feed-In Power :, Category=energy, Groups=[PowerCons])
pwrFeedIn (Type=NumberItem, State=NULL, Label=Feed-In Power , Category=null)

Am I missing something.

I have tried restarting the Influxdb service as these items are read by Influxdb and have tried forcing a change to the .persist file as they are also persisted.

How can I get my number items to update?


That doesn’t seem to have anything to do with getting a string into a number.

You don’t even need to parse the string, postUpdate will actually do that for you if it is purely numeric.


Thanks @rossko57 for the reply.
It’s now working correctly.
That .state.toString command gets me every time. Seems counter intuitive.

Yeh, a state object is a state object. Some parts of rules DSL are smart enough to do necessary conversions from state to more everyday types - others are not.

I think in this case, being a state object, postUpdate tries to use it directly without conversion but of course a “stringy flavoured” state doesn’t fit a Number Item state.

Give it a proper string object, and it knows to perform a conversion.

1 Like