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…
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!
Hi @vzorglub:
Thank you for your very quick answer.
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…
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
@vzorglub:
I changed the channel type from String to Number but I dont’t riceived anymore temperature datas…
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!
// 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…
Thanks for your help!!!
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
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
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…
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!!!