[SOLVED] Automatic watering rule and if statement

The last rule that you have shown has nothing about Item Rule_Trigger.
Perhaps you are triggering from change - have you anything to change the Item to OFF? It can only change to ON once, no matter how many ON commands you send.

Maybe you meant to trigger on command instead.

This is the test rule:

rule “Bewatering plantenbakken achter”
when
Item Rule_Trigger changed

then
var Number w = SensorPlantenbak_Sensor_Humidity.state as Number
if (w < 15) {
WaterkraanAchter_Valve_commands_DurationWatering.sendCommand(60)
WaterkraanAchter_Valve_commands_OpenValveWithDuration.sendCommand(ON)
} else if (w < 30) {
WaterkraanAchter_Valve_commands_DurationWatering.sendCommand(30)
WaterkraanAchter_Valve_commands_OpenValveWithDuration.sendCommand(ON)
} else if (w < 50) {
WaterkraanAchter_Valve_commands_DurationWatering.sendCommand(15)
WaterkraanAchter_Valve_commands_OpenValveWithDuration.sendCommand(ON)
}
end

So it response to the item Rule_Trigger what is a switch that I flip from off to on, normally the rule runs every time it changes from state ON to OFF or OFF to ON. But not with this rule for some reason… It also worked before when I had the IF state wrong and all statement run. This happened every time I used the trigger.

May we see your events.log showing this Item changing? The command event you showed us before will not trigger this rule.

Has your edited rule actually loaded? (and so replaced earlier versions)
You need to keep an eye on your openhab.log when editing rules.

How do you know the rule is not triggered? It may have an error - see your openhab.log.
Or it is possible to execute your rule with a value of w like 60, and nothing will happen to be seen in your log.

I will post the logging of the event.log further on.
When I edit rules I keep the logviewer side-by-side to the if the rule refreshes, so I know the rule is loaded properly.

If I trigger the item and keep the logging next to it, I’ll see the item get triggered but the water isn’t showing or showing errors.

So what is w value?
Hint - find out.

logInfo("test", "w " + w.toString)

Place before your if() after you have created w

Thanks, that is a nice trick I didn’t know about. This is the result of the first run after a reboot of OH, the rule runs after the trigger:

2020-07-24 20:23:04.728 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command ON

2020-07-24 20:23:04.742 [vent.ItemStateChangedEvent] - Rule_Trigger changed from NULL to ON

==> /var/log/openhab2/openhab.log <==

2020-07-24 20:23:05.375 [INFO ] [.eclipse.smarthome.model.script.test] - w 28.0

==> /var/log/openhab2/events.log <==

2020-07-24 20:23:05.410 [ome.event.ItemCommandEvent] - Item ‘WaterkraanAchter_Valve_commands_DurationWatering’ received command 30

2020-07-24 20:23:05.429 [ome.event.ItemCommandEvent] - Item ‘WaterkraanAchter_Valve_commands_OpenValveWithDuration’ received command ON

2020-07-24 20:23:05.432 [nt.ItemStatePredictedEvent] - WaterkraanAchter_Valve_commands_DurationWatering predicted to become 30

2020-07-24 20:23:05.450 [nt.ItemStatePredictedEvent] - WaterkraanAchter_Valve_commands_OpenValveWithDuration predicted to become ON

2020-07-24 20:23:05.464 [vent.ItemStateChangedEvent] - WaterkraanAchter_Valve_commands_DurationWatering changed from 60.0 min to 30.0 min

2020-07-24 20:23:05.472 [vent.ItemStateChangedEvent] - WaterkraanAchter_Valve_commands_OpenValveWithDuration changed from NULL to ON

And this is the result when I fire it the second time:

2020-07-24 20:26:34.362 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command ON

2020-07-24 20:26:35.361 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 38.1 to 38.6

2020-07-24 20:26:37.400 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 38.6 to 38.1

All logging further on doesn’t concern the watering rule.
So it seems that it can read the value the first time but after that is seems impossible till a reboot.

Actually the log tells you why the rule isn’t executing.
The first time you flip the switch, you get

2020-07-24 20:23:04.728 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command ON
2020-07-24 20:23:04.742 [vent.ItemStateChangedEvent] - Rule_Trigger changed from NULL to ON

The second time (since the trigger is already on) you only get

2020-07-24 20:26:34.362 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command ON

but you rule is only executed when the item changes its state:

when
Item Rule_Trigger changed

then

So you might want to change the trigger to

when
Item Rule_Trigger received command

then

That should help. See also https://www.openhab.org/docs/configuration/rules-dsl.html#event-based-triggers to read what kind of triggers are available

1 Like

Ah now I see, that’s a bit dumb :stuck_out_tongue:
Thank you all, everything seem to work now as I wanted, I also integrated the rain exception, it’s a bit dry now but I will check if it’s working properly when there is a lot of rain again :slight_smile:

You might ponder why that didn’t happen. What made you think the switch flipped from on to off, when it clearly didn’t. Maybe a misunderstanding of whatever UI you use.

Well, the item is a simple fake ON/OFF switch, I created a switch in Habpanel to easily use the switch button.
It worked before but now there is something strange with it, it keeps in the state (ON or OFF) that it already is and just set the switch to the same state again, like you can see here were I multiple times pushed the switch button:

2020-07-25 09:03:15.321 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command OFF

2020-07-25 09:03:15.868 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command OFF

2020-07-25 09:03:15.992 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 37.6 to 38.6

2020-07-25 09:03:16.443 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command OFF

2020-07-25 09:03:16.949 [ome.event.ItemCommandEvent] - Item ‘Rule_Trigger’ received command OFF

It keeps getting the command to go off, so the trigger that I had that was looking for the event where it changed from ON to OFF or OFF to ON, will only happens one time after a reboot. Don’t know why yet…

Rephrase as, the UI keeps sending OFF
To toggle a switch i.e. send OFF if the last thing was ON, the UI would look at the current state to invert it.
I’ve no idea about HABpanel configuring or where that can go wrong.

Yea it always worked like that because I use this trigger for months and it always worked. I’ve nothing special configured in the HABpanel for the switch, just a plain ON/OFF switch with the trigger item selected.

Will try to remove both the item as the switch in HABpanel and create them again, maybe that will solve the problem.

You might check if you have accidentally disabled autoupdate on your Item.

Ah I will try that as well, I just saw that I didn’t made a choice at all for auto update, lets see if it makes any difference.

Will try it when I’m back home again :slight_smile:

You should not need to, “no selection” has autoupdate enabled by default.

Ghostly links to an Item can also stop item state responding to commands.

Hmmm ok, that’s strange then.
How can I check if there are ghostly links to an item? I created the item directly in the item section from the UI, not made by an channel.

Maybe worth a look. Sometimes people get caught when creating a “new” Item with a name like TestSwitch, and it picks up rubbish left over from some other experiment a year ago involving a long forgotten TestSwitch.

Zombie link checking -

Bear in mind we’e going down this side issue rabbithole only because your test Item doesn’t seem to be autoupdating as expected. Autoupdate explanation -

It’s all rather off-topic as you have your rule working … but I wouldn’t like mystery happenings in my own system :wink:

Your right, I also rather don’t have mysterious behavior of my system. I’m glad that’s only simple test item doing nothing most of the time.

Thanks fo the threats, will take a look at it tomorrow and see if there might be a solution for it in the article’s.

Will post the outcome overhere :slight_smile:

I kept trying several scenarios but the items seems just right, I was able to replicate the issue but I thinks its more an browser issue.

I’m using Safari on a MacBook Pro, and the problem seems to start when I open a tab with my HabPanel, open some other tabs, after an hour or so, the trigger switch doesn’t seem to work like it used to. When I reload the page, it’s works again like it used to. Tried it several times and it kept happening when I did what I described before.

Your events.log tells you the truth. Either it receives commands from your UI, or it doesn’t.