Fan rule

Hello,
I’m trying to make a rule for a bathroom fan and I’ve got probably a mistake in syntax and I can’t figure it out where is the problem.
rule

val maxHumidity = 60
val minHumidity = 40

rule “fanBathroom”

when
Item Humidity changed
then
if(ventilator.state != ON) {
if(Humidity.state as Number > maxHumidity) {
ventilator.sendCommand(ON)
}
}
if(ventilator.state = ON) {
if(Humidity.state as Number < minHumidity) {
ventilator.sendCommand(OFF)
}
}

end

fan

Switch ventilator “ventilator” {qpio=“pin:29 activelow:yes initialValue:high” }

humidity

Number Humidity { channel=“mqtt:topic:58ab282c:Humidity” }

P.S. when the humidity exceeds 60 turns the fan on and when is the humidity below 40 fan shuts down.

Adding logging inside both inner if-statements will help you troubleshoot.
I noticed that the channel for the fan specifies ‘high’ as a value, not ‘ON’.
Also, you may have to specify the humidity limits as Number (not sure about that).

val Number maxHumidity = 60
val Number minHumidity = 40

rule "fanBathroom"
  when
    Item Humidity changed
  then
    logInfo("FanBathroom", "Humidity changed to {}, ventilator is {}", Humidity.state, ventilator.state)
    if (ventilator.state != ON) {
      if (Humidity.state as Number > maxHumidity) {
        logInfo("FanBathroom", "Humidity {} above threshold of {}", Humidity.state, maxHumidity)
        ventilator.sendCommand(ON) 
      }
    }
    if (ventilator.state == ON) {
      if (Humidity.state as Number < minHumidity) {
        logInfo("FanBathroom", "Humidity {} below threshold of {}", Humidity.state, minHumidity)
        ventilator.sendCommand(OFF)
      }
    }
  end

I checked up the log and I have seen that I got an error.
But the meaning of this error is to me unknown

For starters, you are missing the jsonpath transformation. Add it to your configuration :grin:.

What is the name of the rules file you created for this rule? Is it just rules? If so, try to rename it to something like fanbathroom.rules.

Ok, so I renamed file into fan.rules
and I’ve check for the jsonpath transformation and it’s installed(2.4x version)

Do you mean that it was already installed? the log file suggests it is missing: "Transformation service CONFIG. “JSONPATH for pattern $. not found!”. In that case try to uninstall and install it again and reboot in between.

If it is still throwing the NullPointerException, please add logging to see where in the rule it stumbles upon an NPE. And add another logging at the start of the rule to show the values/states of the items.

BTW, do you see mqtt messages coming in in events.log?

The jsonpath was already installed.
Can a problem be in a incoming value under channel configuration?


Remove the “config” and the quotation marks from the transformation pattern.
The state topic looks odd, are you sure that “Humidity” is correct in there?

I think it is a syntax issue in the transform and therefor the humidity item remains null and hence the NPE error.
Why do you prefix the jsonpath with “config” ? And I’m not sure if you should add the quotes around it (I use configuration files instead of Paper UI, so I’m not sure).

EDIT; @opus was a few seconds ahead of me…

1 Like

No

“config” there was a mistake, sorry about that.

What kind of device is it?
Mine is running Tasmota and I use the state topic:
tele/sonoff_TH/SENSOR
With sonoff_TH being the given name for the device

I’m sorry because I can’t give you more information because I’m totally new into the mqtt and this JSON transformation.

Device is NodeMcu based on ESPeasy with DHT22
and mqtt topic for the humidity is

/sensor/dht22/humidity

topic is 100% correct because PaperUi shows it just fine.

If the statetopic is correct with "Humidity " , why is it needed in the transformation pattern again?
Could you post the raw message you are getting.
My assumption would be, if the " humidity " is part of the state topic a transformationPattern might be obsolete.

You mean raw message of the mqtt?

This looks like a simple numeric value, not a json message. You should remove the json transformation.

if(ventilator.state = ON) {

Is wrong. The = should be ==. So double = sign.

3 Likes

I completely overlooked that typo and corrected the post for future viewers. Thanks for catching it!

1 Like

I apologize for late reply haven’t been home. So far I corrected that typo and my rule looks like this

val Number maxHumidity = 90
val Number minHumidity = 70

rule “fanBathroom”
when
Item Humidity changed
then
if (ventilator.state != ON) {
if (Humidity.state as Number > maxHumidity) {
log.info(“on”, “Humidity {} above threshold of {}”, Humidity.state, max$
ventilator.sendCommand(ON)
}
}
if (ventilator.state == ON) {
if (Humidity.state as Number < minHumidity) {
log.info(“off”, “Humidity {} below threshold of {}”, Humidity.state, mi$
ventilator.sendCommand(OFF)
}

and I've checked the log and found a new error

[WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘fan.rules’ has errors, therefore ignoring it: [5,6]: no viable alternative at input ‘“’

is this valid at all? i think you should either change this to logInfo(…) or completely comment out those log.info’s and i guess the rule will magically work :wink:

Deleted the log’s out and the rule still doesn’t work