[SOLVED] Rule ' ': Could not cast value to float

Hello.
I riceive with mqtt temperature datas as string Typ. (GenericMQTTThing).
Now I would like to calcuate with a rule the min and max value of the temperature but I have problems to find the error… :fearful: :rage:
My code:

var float Room2TempTodayMin = 0
//var float GenericMQTTThing_Room2Temperature_float
//var float Room2TempTodayMax = 0
var DateTime Room2TempTodayMin_Time
var DateTime Room2TempTodayMax_Time

rule “Min/Max temp”
when
Item GenericMQTTThing_Room2Temperature changed
then

//calculate max temp

//GenericMQTTThing_Room2Temperature_float.postUpdate(Float.parseFloat(GenericMQTTThing_Room2Temperature.state)) //conversion string to float???
//GenericMQTTThing_Room2Temperature.state as Number???

if ( GenericMQTTThing_Room2Temperature.state as float > Room2TempTodayMax.state as float) {
Room2TempTodayMax.postUpdate(GenericMQTTThing_Room2Temperature.state.toString) //updating the item Room2TempTodayMax
Room2TempTodayMax_Time.postUpdate(new DateTimeType) //updating the item Room2TempTodayMax_Time
logInfo(“Calculate temp Max YES!”)
} else
{
logInfo(“Calculate temp Max NO!”)
}
end

// rule “6pm Min Temp Reset”
// when
// Time cron “0 0 18 * * ?” //6pm
// then
// Room2TempTodayMin.postUpdate(GenericMQTTThing_Room2Temperature.state.toString)
// Room2TempTodayMin_Time.postUpdate(new DateTime Type)
// end

// rule “6pm Max Temp Reset”
// when
// Time cron “0 0 18 * * ?” //6pm
// then
// Room2TempTodayMax.postUpdate(GenericMQTTThing_Room2Temperature.state.toString)
// Room2TempTodayMax_Time.postUpdate(new DateTime Type)
// end

I get this error:

Rule ‘Min/Max temp’: Could not cast 0.75 to float

I think it is a problem with the string - number conversion but I’m not able to find the correct syntax.
Any help is very appreciated.
Thanks a lot!

Why don’t you change the channel type to Number and all your problems will disappear ?

1 Like

Hi @vzorglub:
Thank you for your very quick answer. :sunglasses: :nerd_face:
I thought the same and so I have tried to change string to number under Configuration/Things/GenericMQTTThing/Channels/Linked items.
But the rule doesn’t worked and the value on the basic ui wasn’t displayed.
So I thought that only string values were displayes on the basic ui and I have oder problems where I don’t know the solution.
I will try to change it again and do a stop and start of openhab… :pensive:

Maybe something like this could be adapted to your rule:

rule “Temperature Livingroom Sensor”
when
Item Temperature_LR received update
then
if (Temperature_LR.state != NULL){
val roundedNumber = (Math::round((Temperature_LR.state as DecimalType).floatValue()* 10)).floatValue() /10
postUpdate(Temperature_LR_rounded,roundedNumber)
}
end

Hi @H102:
Thank you for your input.

@vzorglub:
I changed the channel type from String to Number but I dont’t riceived anymore temperature datas… :woozy_face:
Did I something wrong?
When I change the channel type openhab convert the incomming datas?
I think that my esp8266 with my mqtt sketch send string datas and not number datas, witch I have to convert (string to number) before I have to program a rule.
Is my train of thougt right?
I tried to convert the string temperature data to float data with this command but it doesn’t work…uff

GenericMQTTThing_Room2Temperature_float.postUpdate(Float.parseFloat(GenericMQTTThing_Room2Temperature.state)) //conversion string to float???

Has someone an advice for my problem?
Thank you for your helpfulness!

What does it sends exactly?
Do you have an MQTT utility like MQTT explorer?

MQTT sends Strings and Binary data only
Numbers are sent as strings. OpenHAB does the conversion automagically.

@vzorglub:
With my mqtt sketch i send different datas. For example the temperature like this:

client.publish(“room_1/temperature”, String(t).c_str());

The variable h is from typ float:

// Read temperature as Celsius (the default)
float t = dht.readTemperature();

Yes, I have MQTT Explorer.

Now I deleted all my channel and created it new as typ number. The value will get out correctly on my basic ui. (First positive step)

My rule have still an error:

020-01-29 21:33:18.644 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Min/Max temp’: ‘state’ is not a member of ‘float’; line 23, column 48, length 23

My code:

var float Room1TempTodayMin = 0.0
var float Room1TempTodayMax = 0.0
 
rule "Min/Max temp"
 when
     Item GenMQTTThing_Room1_temp changed
 then 
 
 //calculate max temp
 
 if ( GenMQTTThing_Room1_temp.state as Number > Room1TempTodayMax.state as Number) {
     Room1TempTodayMax.postUpdate(GenMQTTThing_Room1_temp.state)     //updating the item Room1TempTodayMax
     Room1TempTodayMax_Time.postUpdate(new DateTimeType)     //updating the item Room1TempTodayMax_Time
     logInfo("Calculate temp Max YES!")
 }   else
 {
      logInfo("Calculate temp Max NO!")
 }
 end

I havn’t an idea how can I do differently… :worried:
Thanks for your help!!!

SO, I’ll ask my question again:

15.2?
15?
XV?
15.45c?

What?

I think the error occurs in this line:
Room1TempTodayMax.postUpdate(GenMQTTThing_Room1_temp.state)
I’d add a as Number before closing the bracket.

1 Like

Good morning!

@vzorglub:
It sends a float-typ like ‘15.20’.

@opus :
Thank you for your input. I will try it at evening.

Show me your thing and number channel, please

@vzorglub:
Thank you for your helpfulness and patience.

Please show me the channel screen, please

Now, I hope that I have understand what do you mean, sorry.

And when you configure this (assuming that the topic is correct) it doesn’t work?
But the same topic works as a Text channel?

It should work

Please show the logs when the value is updated via MQTT. For the Number channel set up please

The datas wich I riceive were displayed correctly on my basic ui.
In my rule I have problems. I get this error:

2020-01-30 19:16:10.722 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Min/Max temp’: ‘state’ is not a member of ‘java.lang.Number’; line 23, column 48, length 23

My rule code:

var Number Room1TempTodayMin = 0.0
var Number Room1TempTodayMax = 0.0
var DateTime Room1TempTodayMin_Time
var DateTime Room1TempTodayMax_Time

rule "Min/Max temp"

when

    Item GenMQTTThing_Room1_temp changed

then 

//calculate max temp

if ( GenMQTTThing_Room1_temp.state as Number > Room1TempTodayMax.state as Number) {

    Room1TempTodayMax.postUpdate(GenMQTTThing_Room1_temp.state as Number)     //updating the item Room1TempTodayMax

    Room1TempTodayMax_Time.postUpdate(new DateTimeType)     //updating the item Room1TempTodayMax_Time

    logInfo("Calculate temp Max YES!")

}   else

{

     logInfo("Calculate temp Max NO!")

}

end

My sitemap (excerpt):

Frame label="Device 1"   {

                Text item=GenMQTTThing_Room1_temp label="Raum 1 Temperatur[%.1f °C]" icon="temperature"

                Text item=GenMQTTThing_Room1_hum    label="Raum 1 Feuchtigkeit[%.1f %%]" icon="humidity"

                Text item=GenMQTTThing_Room1_rssi    label="Raum 1 Wifi Quality[%d rssi]" icon="qualityofservice"  

                Text item=MQTT_Update1

                Switch item=Device1_Status  

                Text item=Room1TempTodayMin 

                Text item=Room1TempTodayMax 

                Text item=Room1TempTodayMin_Time 

                Text item=Room1TempTodayMin_Time

My items (excerpt):

Number      Room1TempTodayMin       "Room1TempTodayMin [%.1f °C]"  <heating>
Number      Room1TempTodayMax       "Room1TempTodayMax  [%.1f °C]"  <heating>
DateTime      Room1TempTodayMin_Time       "Room1TempTodayMin_Time [%1$tH:%1$tM]"  <heating>
DateTime     Room1TempTodayMax_Time      "Room1TempTodayMax_Time [%1$tH:%1$tM]"  <heating>

log excerpt:

2020-01-30 19:17:00.715 [vent.ItemStateChangedEvent] - GenMQTTThing_Room1_temp changed >from 18.70 to 18.60

2020-01-30 19:17:10.718 [vent.ItemStateChangedEvent] - GenMQTTThing_Room1_temp changed >from 18.60 to 18.70

2020-01-30 19:17:30.715 [vent.ItemStateChangedEvent] - GenMQTTThing_Room1_temp changed >from 18.70 to 18.80

2020-01-30 19:18:10.729 [vent.ItemStateChangedEvent] - GenMQTTThing_Room1_temp changed >from 18.80 to 18.90

Ok:

The rule:

var Number Room1TempTodayMin = 0.0
var Number Room1TempTodayMax = 0.0

rule "Min/Max temp"
when
    Item GenMQTTThing_Room1_temp changed
then 
//calculate max temp
if ( GenMQTTThing_Room1_temp.state as Number > Room1TempTodayMax.state as Number) {
    Room1TempTodayMax.postUpdate(GenMQTTThing_Room1_temp.state as Number)     //updating the item Room1TempTodayMax
    Room1TempTodayMax_Time.postUpdate(new DateTimeType)     //updating the item Room1TempTodayMax_Time
    logInfo("Calculate temp Max YES!")
} else {
     logInfo("Calculate temp Max NO!")
}

end

Room1TempTodayMin is a Variable, not an Item. So you can’t use .state on it…

Do this:

if ( GenMQTTThing_Room1_temp.state as Number > Room1TempTodayMax) {
    Room1TempTodayMax = GenMQTTThing_Room1_temp.state as Number     //updating the item Room1TempTodayMax
    Room1TempTodayMax_Time.postUpdate(new DateTimeType)     //updating the item Room1TempTodayMax_Time

I think do you have made a good base :sunglasses:, what I did false…
In my rule I have the variable

var Number Room1TempTodayMin = 0.0
var Number Room1TempTodayMax = 0.0

but I have the same name in my item.

Number      Room1TempTodayMin       "Room1TempTodayMin [%.1f °C]"  <heating>
Number      Room1TempTodayMax       "Room1TempTodayMax  [%.1f °C]"  <heating>

Them same with the time.
How can I connect the variable in the rule with the correct item?

If you have Items to hold max and min … what are the variables supposed to be for?
And your time “things”, are those Items or variables?
You must not muddle these up in rules, they are something quite different.

It is difficult to guess what you are trying to do because of the duplicate names.

@rossko57:
Yes, thats true. I have muddled the two things… :fearful:
Thank you for the tip. @vzorglub too.
My idea is to calculate the min and max temperature value with a rule (and in future automatically with the time cron) and show the calculates datas with the timestamp from min and max temperature on my basic ui.

I’m a beginner and so I have some problems.
Thank you so much for our help!!!