[SOLVED] Xiaomi Humidity Sensor Read Values in Rules


(Jan) #1

Hi

Sorry for this stupid question but I’m running nuts…

I have different Xiaomi Sensors attached to Openhab 2.3. I configured them manually in things and items files due to backup reasons.

One is defined as follows
Number:Dimensionless Xi_HT_Schlafzimmer_Humidity “Schlafzimmer” (gHumidity) { channel=“mihome:sensor_ht:xxxx:humidity” }

It shows correct values in my sitemap. BUT in my rule it seems to be not read… I’m wondering whether it is because of Number:Dimensionless

// Rule für Oskar Little im Schlafzimmer
rule “Oskar little ON wenn Feuchtigkeit im Schlafzimmer zu niedrig”
when
Item Xi_HT_Schlafzimmer_Humidity changed
then
if (Xi_HT_Schlafzimmer_Humidity < 50) {
if (MyStr_Switch03_Switch.state == OFF) {
MyStr_Switch03_Switch.sendCommand(ON)
sendBroadcastNotification(“Oskar Little im Schlafzimmer wurde eingeschaltet”)
}
}
else {
if (MyStr_Switch03_Switch.state == ON) {
MyStr_Switch03_Switch.sendCommand(OFF)
}
}
end

I thing the statement if (Xi_HT_Schlafzimmer_Humidity < 50) is not correct somehow but I’m not able to resolve it. if (Xi_HT_Schlafzimmer_Humidity.state < 50) does not help either.

Can anyone help?


(Vincent Regaud) #2

Please use the code fences!

// Rule für Oskar Little im Schlafzimmer
rule "Oskar little ON wenn Feuchtigkeit im Schlafzimmer zu niedrig"
when
    Item Xi_HT_Schlafzimmer_Humidity changed
then
    var humidity = Xi_HT_Schlafzimmer_Humidity.state as Number
    logInfo("Xi_HT_Schlafzimmer_Humidity: ", humidity.toString)
    if (humidity < 50) {
        if (MyStr_Switch03_Switch.state == OFF) {
            MyStr_Switch03_Switch.sendCommand(ON)
            sendBroadcastNotification(“Oskar Little im Schlafzimmer wurde eingeschaltet”)
        }
    }
    else {
        if (MyStr_Switch03_Switch.state == ON) {
            MyStr_Switch03_Switch.sendCommand(OFF)
        }
    }
end

(Jan) #4

thank you a lot for you immediate help… I’m going to try this!

Jan


(Jan) #5

Thank you a lot, works fine!

Jan


(Jan) #6

Unfortunately it does not work completely…

Regardless of the value of Xi_HT_Schlafzimmer_Humidity resp. humidityr2, always the first IF statement is entered, never the second. Therefore the value of humidityr2 must be always below 45… even if the value is shown as 48 % etc…

Therefore I assume, that there must bei a problem with the value conversion. Maybe the value is in percentage, even if the conversion is made as number etc.

Has anyone a clue?

// Rule für Oskar Little im Schlafzimmer
rule "Oskar little ON wenn Feuchtigkeit im Schlafzimmer zu niedrig"
when
    Item Xi_HT_Schlafzimmer_Humidity changed
then
    var humidityr2 = (Xi_HT_Schlafzimmer_Humidity.state as Number)
    logInfo("Xi_HT_Schlafzimmer_Humidity: ", humidityr2.toString)
    if (humidityr2 < 45) {
        sendBroadcastNotification("Feuchtigkeit Schlafzimmer unter 45%; Einschaltsequenz wurde eingeleitet")
        if (MyStr_Switch03_Switch.state == OFF) {
            MyStr_Switch03_Switch.sendCommand(ON)
            sendBroadcastNotification("Oskar little im Schlafzimmer wurde eingeschaltet")
        }
    } 
    if (humidityr2 > 47) {
        sendBroadcastNotification("Feuchtigkeit Schlafzimmer über 47%; Ausschaltsequenz wurde eingeleitet")
        if (MyStr_Switch03_Switch.state == ON) {
            MyStr_Switch03_Switch.sendCommand(OFF)
            sendBroadcastNotification("Oskar little im Schlafzimmer wurde ausgeschaltet")
        }
    } 
end

Thanks a lot

Jan


(Vincent Regaud) #7

Try that:

var humidityr2 = (Xi_HT_Schlafzimmer_Humidity.state as QuantityType<Number>).doubleValue

(Jan) #8

Hi Vincent

Thanks a lot for your Help. I will try your hint with the next rule. I did it like this now

// Rule für Oskar Little im Schlafzimmer
rule "Oskar little ON wenn Feuchtigkeit im Schlafzimmer zu niedrig"
when
    Item Xi_HT_Schlafzimmer_Humidity changed
then
    var humidityr2 = (Xi_HT_Schlafzimmer_Humidity.state as Number)
    logInfo("Xi_HT_Schlafzimmer_Humidity: ", humidityr2.toString)
    if (humidityr2 < 0.45) {
        if (MyStr_Switch03_Switch.state == OFF) {
            MyStr_Switch03_Switch.sendCommand(ON)
            sendBroadcastNotification("Oskar little im Schlafzimmer wurde eingeschaltet")
        }
    } 
    if (humidityr2 > 0.48) {
        if (MyStr_Switch03_Switch.state == ON) {
            MyStr_Switch03_Switch.sendCommand(OFF)
            sendBroadcastNotification("Oskar little im Schlafzimmer wurde ausgeschaltet")
        }
    } 
end

I just divided the limit values by 100 (as for percent) and it seems that it works now.

Jan