String to Number - Chart display of bluetooth sensor value

Hello all,
i currently do have a strange issue which i can not solve.
I am reading sensor values of a Xiaomi bluetooth temperature and humidity sensor via exec binding.
This string i am converting into a number via a rule.
The number should then be displayed in a chart.
However the chart is showing very high values from time to time, even if i setup the rule to limit the values …

Things:

Thing exec:command:humidor_temperature "Humidor - Temperatur" @ "Buero" [command="sudo bash /home/openhabian/xiaomi_sensor_uni.sh 'A4:C1:38:D6:68:AA' 'temperature'", transform="REGEX((.*?))", interval=300, timeout=80, autorun=true]

Items:

String Humidor_Temperature "Humidor Temperature [%s C]" <temperature> { channel="exec:command:humidor_temperature:output"}

Number Humidor_Temperature_temp  "Humidor [%.1f C]" <temperature>  

Number Humidor_Temperature5_num  "Humidor [%.1f C]" <temperature> (gTemperatur)

Rule:

  rule "String to Number"

    when

        Item Humidor_Temperature changed

    then

        Humidor_Temperature_temp.postUpdate(Humidor_Temperature.state.toString)

        if (Humidor_Temperature_temp.state < 40 && Humidor_Temperature_temp.state > 0) {

            Humidor_Temperature5_num.state = Humidor_Temperature_temp.state;

            logInfo("info","Humidor Sensor innerhalb des bereichs") 

        } else {

            logInfo("info","Humidor Sensor ausserhalb des bereichs") 

        }

end

Sitemap:

Text label="Raumklima" icon="temperature"{

                Default     item=Buero_Temperature_num

                Default     item=Humidor_Temperature5_num

                Default     item=Kinderzimmer_Temperature_num

                Chart       item=gTemperatur              icon="temperature"  refresh=300 period=W legend=true


            }

Dont know where the problem is and why there are such high values displayed.
Can someone help?

Thank you!

Edit:
One additional remark: Sometimes i am getting error messages from my exec script when the bluetooth device was not found. In the log this looks like this:

2020-10-05 09:43:27.996 [vent.ItemStateChangedEvent] - Humidor_Temperature changed from 20.59 to connect error: Function not implemented (38)

/home/openhabian/xiaomi_sensor_uni.sh: line 7: a: command not found

connect error: Function not implemented (38)

/home/openhabian/xiaomi_sensor_uni.sh: line 7: a: command not found

==> /var/log/openhab2/openhab.log <==

2020-10-05 09:43:28.006 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert 'connect error: Function not implemented (38)

/home/openhabian/xiaomi_sensor_uni.sh: line 7: a: command not found

connect error: Function not implemented (38)

/home/openhabian/xiaomi_sensor_uni.sh: line 7: a: command not found' to a state type which item 'Humidor_Temperature_temp' accepts: [DecimalType, QuantityType, UnDefType].

2020-10-05 09:43:28.020 [INFO ] [.eclipse.smarthome.model.script.info] - Humidor Sensor innerhalb des bereichs

When the exec script receives correct values it looks like this:

2020-10-05 09:54:48.882 [vent.ItemStateChangedEvent] - Humidor_Temperature changed from 20.59 to 20.58

2020-10-05 09:54:48.899 [vent.ItemStateChangedEvent] - Humidor_Temperature_temp changed from 20.59 to 20.58

2020-10-05 09:54:48.913 [vent.ItemStateChangedEvent] - Humidor_Temperature5_num changed from 20.59 to 20.58

==> /var/log/openhab2/openhab.log <==

2020-10-05 09:54:48.913 [INFO ] [.eclipse.smarthome.model.script.info] - Humidor Sensor innerhalb des bereichs

Here’s a timing problem, that might cause effects you had not expected.
postUpdate is asynchronous, that is to say that the update gets put on openHABs event bus and passed around to any bindings and other rules that are interested. Eventually the actual Item gets updated.

The rule does not stop and wait for that to happen. It only takes a few milliseconds, but before then your rule has moved on and the Item state that you look at in the if() is more than likely the “old” state.
Maybe it’ll get updated in time before the next line that uses the Item state - but maybe it won’t.

It’s all horribly unpredictable, so don’t try to immediately use the state of an Item that you’ve only just updated. Items are not ordinary variables.
Luckily you don’t need to do that, because you already know what you updated it to.

Separately from all that, it’s probably not a good idea to make your rule have errors by trying to force your script’s occasional error text into a number Item.
If you check to see if it is numeric first, you get to choose what to do about it - ignore it and keep an old value, or keep an old value for a while, or set an UNDEF value, or ring a bell or whatever you want

Hello rossko57,
thank you for the quick answer!
I do understand the problem.
What would be your suggestion how to solve it.
Dont understand this sentence to be honest:

It’s all horribly unpredictable, so don’t try to immediately use the state of an Item that you’ve only just updated. Items are not ordinary variables.
Luckily you don’t need to do that, because you already know what you updated it to.

Do you mean to directly check it in the exec python script?

No, I’ve no idea what your script is doing. I’m talking OH rules.

var XX = some calculation
myItem.postUpdate(XX)

fred = myItem.state
//  Don't do that - you'll get the old state
// because the postUpdate has probably not completed yet

fred = XX
// do this, you already know what the state will be

In your case, you’d use your equivalent of XX in the if() and so on.