[SOLVED] Auto ventilation, rules error

I´m trying to get a rule to work. But I´m getting an error which I don´t seem to understand why or how to correct it.

This is the rules:

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then

    if(gHumidityBathRoom < 60) {
     logInfo("info", "either bathrooms humidity is above 60%")
	nilan_vent.sendCommand(4)
    }

    else if(gHumidityBathRoom > 60) { 
     logInfo("info", "either bathrooms humidity is below 60%")
	// Return to previous state.. how?
    }

end

The error I get is:

2018-09-23 22:05:04.205 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Auto ventilate bathrooms': Unknown variable or command '<'; line 6, column 8, length 22

First…
I have created a group for the humitidy in out two bathrooms. This is called gHumidityBathRoom and it works fine according to this:

2018-09-23 21:58:49.236 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 39.10 to 39.20 through lille_bad_fugt

The ventilation system is a Nilan Comfort 300LR. It has 4 step for the ventilation speed. (1 to 4 where 4 is the fastest).
The rule is suppose to set our ventilationsystem to step 4 when ever the humidity goes beyound 60% in anyone of the bathrooms, (thats why I have created a group).

Any idea why I get this error?

Second…
When I have figured out how to set the ventilationsystem back to previous state when humidity reach below 60%, this feature will be added as well. But untill now I have now idea how to. I do have persistens for the ventilationspeed. But I have no idea how to use it.

Hints would be apreciated :slight_smile:

It would need to be

if(gHumidityBathRoom .state < 60)

assuming that item is of Number type.

I haven’t verified but you could try

nilan_vent.sendCommand(nilant_vent.previousState().state)
1 Like
var Number previousVentilation = 0

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    val humidity = gHumidityBathRoom.state as Number
    if(humidity > 60) {
        logInfo("info", "either bathrooms humidity is above 60%")
        previousVentilation = nilan_vent.state as Number
        nilan_vent.sendCommand(4)
    } else if(humidity < 60) { 
        logInfo("info", "either bathrooms humidity is below 60%")
	// Return to previous state.. how?
        // Like this...
        nilan_vent.sendCommand(previousVentilation)
    }
end
2 Likes

Ahh thanks @mstormi and @vzorglub
Ofcouse I was missing the var/val for humidity…
(also mixed up the >< :slight_smile: I see)…

Regarding the return to previous state… is it really that simple?.. Thats awesome!!
I´ll give it a try…

EDIT…
Just saved the rule, and got this error:

2018-09-23 23:02:23.881 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Auto ventilate bathrooms': Script interpreter couldn't be obtain

Ignore the previous EDIT… It seems like the error happened due to copy/past from the web (bad idea anyway).

Just tested the rule and it works… Or at least the ventilation went to 4 (had to change the level though)… Just waiting for it to return to previous state…

And it does return to previous state as well…
This is just awesome… speciallly because I have struggle my head in how to return to previous state…

Thank you very much!!

I have re opened this thread, as I was getting a problem with the previous state. It stopped returning to previous state, when humidity reach below 60. And I have no idea why…
I made a single change inserting line 9. And the rules now look like this:

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    // Exit the rule when there is nothing to do
    if(gHumidityBathRoom.state < 40) return;
    if(nilan_vent.state == 4) return;

        logInfo("info", "Humidity below 60% or Nilan is already on step 4")

    val humidity = gHumidityBathRoom.state as Number
    if(humidity > 60) {

        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = nilan_vent.state as Number
        nilan_vent.sendCommand(4)
    } else if(humidity < 60) { 

        logInfo("info", "Humidity below 60% return to previous ventilation state")
        nilan_vent.sendCommand(previousVentilation) 	// Return to previous Nilan state
    }
end

I just edited line 8 setting the statement to < 40, just for test purpose. Normally it ´s 60.

Now three questions:

  1. How come this wont return to previous state, when humidity has reach below 60% ?
  2. When adding this new line 9, I now get this in my logfile:
2018-10-10 23:29:05.038 [GroupItemStateChangedEvent] - gHumidityBathRoom changed from 48.90 to 49.00 through stort_bad_fugt
2018-10-10 23:29:05.783 [INFO ] [.eclipse.smarthome.model.script.info] - Humidity below 60% or Nilan is already on step 4
2018-10-10 23:29:05.791 [INFO ] [.eclipse.smarthome.model.script.info] - Humidity below 60% return to previous ventilation state
2018-10-10 23:29:05.806 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 1
  1. This show is do send command 1 (previous state). How come it doesn´t work in “live”. A I have changed is adding line 9, and for testing the 40 in line 8.
    I looked through the logfiles today, and when humidity reach above 60, it sends command 4 to the nilan_vent. But when humidity goes below 60, it doesnt send command 1 to nilan_vent. But in this test, it does, as the log above shows…

Because you change the do nothing test to 40 then the rule proceeded to the <60 test
If you put back your do nothing test to <60 then the rule will exit and never go back to previous state.
I don’t think you need

if(gHumidityBathRoom.state < 40) return; //or 60

At all

BUT you need to change one of the tests to >= or <= so that the rule does something when humidity is exactly 60 because at the moment it does nothing.

I changed the <40 to trigger the rule, (insted of manipulation the humidity sensor to go beyond 60).

This is just to stop the rule from doing anything, when the humidity is low. I know I dont need it, but then the rule will send statements to the Nilan ventilation all the time, which is unnecessary in my opinion.

Ahh, ofcouse. That may be the reason I was looking for… Never thought of that. Will give it a try when I get home… It sound obvious!

Before your test you had:

if(gHumidityBathRoom.state < 60) return;

The rule will exit and NEVER proceed to the part where you restore previous ventilation state.

We can deal with that:

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 > 60) {
        logInfo("info", "Humidity above 60% set ventilation to step 4")
        previousVentilation = nilan_vent.state as Number
        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 != nilan_vent.state as Number) {
        nilan_vent.sendCommand(ventCommand)
    }

end

I would like to give my opinion to this case:

I would recommend to use a hysteresis on this value. If you are around the value of 60, your ventilation will be between stage 4 and stage 0 all the time…

I would suggest to set up an Hysteresis of about double the “normal” noise of the sensor. So if your sensor is just about 5 %r.h. on noise, then I would suggest to let the vantilation on at values higher than 60 and stop (resp. set to the old value) at 50.

From this perspective you would not be kept within toggling the ventilation…

The solution from @vzorglub seems very good with the above bespoken hysteresis…

Even at start-up it is save, because if you start with an humidity of between 50 and 60, nothing happens until the lower or upper value is reached…

Only one remark: if you use previousVentilation as variable (like above), it is not possible to save this state over a new start of openhab. If you use an Item instead, it is possible to save the value and being updated after restart. So even if you have o restart openhab while on Speed-up venting, everything is going allright after the new start…

BR,
Andreas

Why? If the himidity is above 60, I would assume it continue the rest of the rule??

Anyway…
These are my items:

Number lille_bad_fugt 			"Lille Bad Fugtighed [%.0f %%]" 					<Humidity> 	(Fugtighed,gHumidityBathRoom) 		 		{ihc="13702695"}
Number stort_bad_fugt 			"Stort Bad Fugtighed [%.0f %%]" 					<Humidity> 	(Fugtighed,gHumidityBathRoom) 				 	 		{ihc="13699623"}
Group:Number:MAX			gHumidityBathRoom	"Højeste Fugtighed i badeværelserne [%.0f %%]"				<humidity>
Number nilan_vent			"Bruger ventilation valg [Level]" 			<fan>				(heat_nilan) {modbus="nilan2:2"}

This is the rule.

var Number previousVentilation = 1

rule "Auto ventilate bathrooms"
when
    Member of gHumidityBathRoom changed
then
    // Exit the rule when there is nothing to do
    if(gHumidityBathRoom.state < 60) return;

        logInfo("info", "Humidity is:", gHumidityBathRoom.state.toString) 

    val humidity = gHumidityBathRoom.state as Number
    if(humidity > 61) {

        logInfo("info", "Humidity above 61% set ventilation to step 4")
        previousVentilation = nilan_vent.state as Number
        nilan_vent.sendCommand(4)
    } else if(humidity < 61) { 

        logInfo("info", "Humidity below 61% return to previous ventilation state")
        nilan_vent.sendCommand(previousVentilation) 	// Return to previous Nilan state
    }
end

I havnt changed anything yet.
This rule is triggering fine, when humidity reach above 60. It sets the Nilan ventilation to step 4, when humidity reach above 61% But it never return to previous state (step 1) when humidity reach below 61. So it just continues at step 4.

Thanks for your opinion Andreas… Original I thought there was a need for a hysteresis. But since this is triggered by two humidity sensores from our bathrooms, they will only trigger when someone is having a bath. The humidity reach 75-85% very fast. Normal humidity is about 45-50% in the bathrooms.
So I thought that it would make no sense using a hysteresis, as it would not stay around 60% humidity for long.
Our Nilan ventilation has four speeds, (1, 2, 3 and 4), where 4 is the fastest. It do have 0 as well, but we never use 0, cause it will stop the ventilation. And thats not a good idea.
I have thought of making a rule which control the speed in intervals. But my knowledge isn´t good enough. And second, the most humidity happens when someone is taking a bath.
I dont know if it makes any good sense :slight_smile:

Hmm, this might come in hand. But it hasnt been a problem yet, even though I restart quite often, (due to testing lots of stuff).

Yes it will do the part where Humidity is above 60 but never do the else if because you already rejected under 60 values.
Your next code is pretty much the same. Changing to 61 will not make it work if the Humidity changes from 61 to 59 for example. I suggest you use the last code I sent.

Thanks Vicent. Your rule seem to work just fine.

Your problem is the first if-clause in your rule…

When this one takes controll, everything else is not working anymore - even the settings to switch the ventilation back.

Therefore: use the rule from @vzorglub and adapt the values, and I will recommend again to use a hysteresis of 5 %r.h. to prevent the system go on and off, even if it is only for a few minutes…

BR,
Andreas

Yeah I see that now…
I´m using Vincets rule, and have changed the upper level to 65 and lower level to 60.

CLOSED again…

1 Like

Have to open again…
I thought it was working fine, cause yesterday it went back to step 1. But today it doesn´t.

This is the log…

2018-10-14 12:35:52.304 [INFO ] [.eclipse.smarthome.model.script.info] - Humidity is: 49.70
2018-10-14 12:35:52.313 [INFO ] [.eclipse.smarthome.model.script.info] - Nilan is already on step 4

With humidity below 60, it should have brought the ventilation back to step 1…Something wrong somewhere.

If I manually set ventilation to step 1, then this show in the log for the next humidity reading:

2018-10-14 12:41:09.268 [ome.event.ItemCommandEvent] - Item 'nilan_vent' received command 1
2018-10-14 12:41:17.042 [INFO ] [.eclipse.smarthome.model.script.info] - Humidity is: 49.70
2018-10-14 12:41:17.061 [INFO ] [.eclipse.smarthome.model.script.info] - Humidity below 60% return to previous ventilation state

On which state is your previousVentilation state?
When the previous ventilation state is on 4, the rule works :wink:

Just in case: log the previousVentilation state in your rule…

BR,
Andreas