Exec Binding - Read value from file/bash script

Hi

I want read value from file.

Items:

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

Things:

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

parse_status_file.sh

#!/bin/bash
echo "34"

It doesent works… What is wrong?

The exec binding output is a string

https://docs.openhab.org/addons/bindings/exec/readme.html

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"
when
    Item ac_current_temp changed
then
    ac_current_temp_number.postUpdate((ac_current_temp.state) as Number)
end
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"
    when
        Item pwrFeedInSt changed
    then
        pwrFeedIn.postUpdate((pwrFeedInSt.state) as Number)
    end

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

And

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.
TIA

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?

Anyone?

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.

pwrFeedIn.postUpdate(pwrFeedInSt.state.toString)

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

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