[SOLVED] Auto ventilation, rules error

Can you post your rule as it is now, please?
Thanks

And I believe this is the problem. But that makes the humidity kinda obsolete controlling the step of the ventilation, I would say… The idea was to force the ventilation back to step 1, when humidity is low, obvious.
I didn´t have time last night, but I´ll insert a log to show what previous state is. But I´m almost 100% certain, it´s 4.

The rule is exactly like the one you made. Only difference is, that I changed the values to 65. I doubt that would make any changes in the outcome.

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    // 1. Exit the rule when there is nothing to do
    if (nilan_vent.state == 4) {
        logInfo("info", "Nilan is already on step 4")
        return;
    }

    // 2. Calculate the states
    val humidity = gHumidityBathRoom.state as Number
    var ventCommand = 0

    if(humidity > 65) {
        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = nilan_vent.state as Number
        ventCommand = 4
    } else if(humidity < 65) { 
        logInfo("info", "Humidity below 60% return to previous ventilation state")
        ventCommand = previousVentilation // Restore ventilation to previous state
    }

    // 3. Do the command only if state is different
    if (ventCommand != nilan_vent.state as Number) {
        nilan_vent.sendCommand(ventCommand)
    }

end

I believe this part is the problem:

    // 1. Exit the rule when there is nothing to do
    if (nilan_vent.state == 4) {
        logInfo("info", "Nilan is already on step 4")
        return;
    }

If, for some reason, previous state never returned to 1, it would stay at 4 and never meassure the humidity.
Thats why I, in my original rule, meassured the humidity insted, (ie if humidity is < 60 then return; ). But that will also require a check to make sure, that the ventilation state is 1 as well. If it´s not, (for some reason I still cant figure), and the humidity is below 60, then set ventilation to 1.

My slight theory, (not confirmed yet):
Nilan got its own automatic ventilation speed control, based on humidity as well…
This is a stupid way it handles it, which is why I hoped I could force openhab to take over the controle. I can´t disable Nilan´s own automatic control.
BUT! as far as I understand, Nilan does NOT make use of the state of the ventilation. It uses internal automatic to control the ventilation directly.
If I manually change the state, the manual influence will overrule the internal automatic control. Meaning:
If Nilan, due to high humidity, automatic change the speed of the ventilation til 100%. The ventilation state, which I´m using in openhab, doesnt change. So the previous state would be the same (ie step 1), even though Nilan has set the ventilation to 100%. If I manually change the step to 1, 2, 3 or 4, this will overrule the automatic control of Nilan, and the ventilation will return to whatever speed I have chosen.
Based on this, I don´t think it´s Nilan internal speed control which is messing up with the openhab rule Vincent made. But I´ll keep a close eye on this, and, as suggested, insert a loginfo to log the previous state.

I hope this make sense… It´s a bit difficult to explain.

Ok, let’s change it a bit:
So we remove the first part
Introduce a new val currentState = nilan_vent.state as Number
And we check if the vent state is not already 4 as well as the humidity >= 65

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    
    // 2. Calculate the states
    val humidity = gHumidityBathRoom.state as Number
    val currentState = nilan_vent.state as Number
    var ventCommand = 0

    if(humidity >= 65 && currentState != 4) {
        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = currentState
        ventCommand = 4
    } else if(humidity < 65) { 
        logInfo("info", "Humidity below 60% return to previous ventilation state")
        ventCommand = previousVentilation // Restore ventilation to previous state
    }

    // 3. Do the command only if state is different
    if (ventCommand !=currentState) {
        nilan_vent.sendCommand(ventCommand)
    }

end

Will give it a try tonight, Vincent.

Hmm… there is a problem with the rule, Vincent.
This is a grafana screendump from the humidity in one of our bathrooms.

At aprox 8:30 this morning, then ventilation went to step 4, due to the high humidity above 65, just as it´s suppose to.
But when humidity came below 65, then the ventilation stopped (step 0 = no ventilation).
At aprox 9:30, then ventilation didn´t go to step 4, though the humidity went beyond 65 again.
Now, it´s still at step 0. When I try to force it to step 1, it goes back to step 0 after a few seconds.

I can not reach my logfiles from work through myopenhab. I will study them when I get home tonight. But I have another screendump showing the ventilation speed, ( which btw looks a bit strange, cause it looks like it never reached 100% (step 4) at 8:30. It looks like step 2 (50%). But I know it did, cause at step 4, I can hear the ventilation, which I did)
Anyway… Is still give a slight idea, that something is not working as suppose to:

Yes, I will need to see the logs. After all this it should work…

Yes, I know… In the mean time, I have a question to regarding the rule…
This line, what exactly does it do?

    var ventCommand = 0

Declares the variable ventCommand

Further down the rule the variable will either be set to 4 or to previousVentilation
UNLESS!!! currentState == 4 then it will stay at 0 even if the humidity is > 65

Of course, let me think about this

Change the line to:

var ventCommand = currentState
1 Like

Will do when I get home… I did suspect the line could become the problem, specially because it should never go to step 0, and this will set the ventilation to 0% (stop the fans). This is not good for a ventilation system like this.

I have changed the rule with the line you send earlyer today.

Meanwhile, here is part of the log from this morning, where the humidity went above 65 and the rule triggered first time. For sure something was wrong, and this continued untill the humidity went below 65.

I guess (hope) that the new change will solve this.

2018-10-16 08:27:01.629 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 61.80 to 65.70 through stort_bad_fugt
2018-10-16 08:27:01.674 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 4
2018-10-16 08:27:01.788 [vent.ItemStateChangedEvent] - nilan_vent changed from 1 to 4

2018-10-16 08:27:06.964 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 65.70 to 68.60 through stort_bad_fugt
2018-10-16 08:27:06.998 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 0
2018-10-16 08:27:07.019 [vent.ItemStateChangedEvent] - nilan_vent changed from 4 to 0

2018-10-16 08:27:12.208 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 68.60 to 71.30 through stort_bad_fugt
2018-10-16 08:27:12.230 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 4
2018-10-16 08:27:12.237 [vent.ItemStateChangedEvent] - nilan_vent changed from 0 to 4

2018-10-16 08:27:17.651 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 71.30 to 71.00 through stort_bad_fugt
2018-10-16 08:27:17.685 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 0
2018-10-16 08:27:17.699 [vent.ItemStateChangedEvent] - nilan_vent changed from 4 to 0

2018-10-16 08:27:22.991 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 71.00 to 66.60 through stort_bad_fugt
2018-10-16 08:27:23.009 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 4
2018-10-16 08:27:23.017 [vent.ItemStateChangedEvent] - nilan_vent changed from 0 to 4

I just tested the rule with the new change… Nilan vent to step 4, when the humidity got above 51 (I have manually set the value to 51 for both oppe and lower values, to test it). And when humidity reach below 51, Nilan went back to step 1 (previous state). So this seems to be working… I´ll keep an eye on it.

@vzorglub, the rule seem to work pretty well thank you. But I have a small request for some help in a small change.

Would it be possible to read the actual state of the ventilation (numbers 0,1,2,3,4) and return to actual previous state when humidity goes below its setting?

The reason is:
Sometime I have a need to control the speed manually (the ventilation speed). But if I set it to 3, (for some specific reason, like having alot of guests etc or just need to boost the ventilation), a few seconds after, the rule will change it to step 1, due to the humidity beeing low.
This should be optimized.

I can think of two solutions.

  1. Use a switch to disable the rule. (not an optimized solution).
  2. Somehow read the previousVentilation step, and return to this state, ie 0,1,2,3 or 4…

I believe it require that the ‘previousVentilation’ should be defined as val based on its actual state or something simular But I cant figure how to define it and use it in the rule.

This is the rule:

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    
    // 2. Calculate the states
    val humidity = gHumidityBathRoom.state as Number
    val currentState = nilan_vent.state as Number
    var ventCommand = currentState
logInfo("Højeste_Fugt", gHumidityBathRoom.toString)

    if(humidity >= 60 && currentState != 4) {
        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = currentState
        ventCommand = 4
    } else if(humidity < 60) { 
        logInfo("info", "Humidity below 60% return to previous ventilation state")
        ventCommand = previousVentilation // Restore ventilation to previous state
    }

    // 3. Do the command only if state is different
    if (ventCommand !=currentState) {
        nilan_vent.sendCommand(ventCommand)
    }

end

In the same rule file:

rule "Ventilation changed"
when
    item nilan_vent changed
then
    val currentState = nilan_vent.state as Number
    if (currentState != 4) previousVentilation = currentState
end

Hmm before or after? Or does it not matter?

Doesn’t matter

When I insert this into the rule file, I get this error:

2018-10-24 00:42:53.386 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'autoventilatebathroom2.rules' has errors, therefore ignoring it: [32,5]: no viable alternative at input 'nilan_vent'

This is the rule file:

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    
    // 2. Calculate the states
    val humidity = gHumidityBathRoom.state as Number
    val currentState = nilan_vent.state as Number
    var ventCommand = currentState
logInfo("Højeste_Fugt", gHumidityBathRoom.toString)

    if(humidity >= 60 && currentState != 4) {
        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = currentState
        ventCommand = 4
    } else if(humidity < 60) { 
        logInfo("info", "Humidity below 60% return to previous ventilation state")
        ventCommand = previousVentilation // Restore ventilation to previous state
    }

    // 3. Do the command only if state is different
    if (ventCommand !=currentState) {
        nilan_vent.sendCommand(ventCommand)
    }

end

rule "Ventilation changed"
when
    nilan_vent changed
then
    val currentState = nilan_vent.state as Number
    if (currentState != 4) previousVentilation = currentState
end

Sorry:

rule "Ventilation changed"
when
    item nilan_vent changed
then
    val currentState = nilan_vent.state as Number
    if (currentState != 4) previousVentilation = currentState
end

Ahh ofocuse… I should have spotted that myself… I was so focused of the not so obvious :frowning:

Seems to be working. Thanks alot Vincent.