Log non existing item in rule condition

I just spent 45 minutes to find out, why a rule was not firing using ruleDSL. From the logs there was no error while parsing the rule “model”. So I thought everything should be ok and was checking at other places. In the end it turned out, that I had a typo in the item name in the rule condition :(.

My question here is: would it be possible to log some kind of error if an item, referenced in the confition of a rule, does not exist? That would be awesome!

I’m using OH3.1 with openhabian and having the rule in a text file.

Some stuff we learn the hard way :wink:

This should be a warning, not an error, you might come along and add the Item afterwards. Advanced users sometimes use dynamic Items.

You might log an enhancement request against DSL rules on Github.
But to be honest I don’t think there is much developer interest in new features for ‘legacy’ files-based DSL, you cannot make this mistake using GUI entered rules.

Yeah, fair enough :smiley:.Let me try if someone has interest in this, thanks!

I’m not so sure how easy that would be to implement.

  • What happens if the Item existed when the rule was loaded but later was removed?
  • What happens if the Item didn’t exist when the rule was loaded but later added?
  • What happens if the Item existed when the rule was loaded, but later was removed and then recreated?
  • Does it only cover Item triggers or all references to an Item?

In OH 2 all of the above could have occurred on every restart of OH because the order and timing of files being loaded related to each other was not defined. In OH 3 it’s less likely because there is more control over the boot process now. But they could occur after OH has started as one manually manipulates the config.

Having said all that, I believe that the openHAB Extension for VSCode will in fact tell you as a warning or an error if you try to use an Item that doesn’t exist.

Fair points. But I’m not saying to fail the parsing of the rule or to not registering it, but just to log a WARN as a hint that the item, on rule parsing, is currently unknown to the system. So that a poor guy like me isn’t pretending everything in the rule is fine, but actually isn’t :).

I guess it’s just me (and maybe my setup), but the VSCode extension is kind of flaky. If I hover over existing items, it sometimes works (shows state), sometimes not. The same goes for auto-completion. For some items it works for some not. Maybe when they were just recently created (?). At least for me it does not show any warn/error if I use an unknown item in the condition.

In the case of rule trigger, like

rule "blaarch"
   Item blaargh changed

VSC does not notice imaginary Items.
Within the body of a rule, it does.

Seems like that could be added to the extension then. It seems reasonable to do so to me.

1 Like

My observation is that VSC won’t pick up on new items until it’s restarted. I assume that it checks the available items once when it loads. So if I create/edit an item, then I just close/open VSC and it’s all good.

Keep in mind that there are lots of things that would cause a rule to not work without throwing a warning/error. For example, my bad habit is forgetting to attach .state to items in IF statements, which means that the rule just ignores the condition. There’s only so much that the system can account for human error, and I lean toward “Russ needs to learn to not make mistakes” over “someone needs to program something to prevent Russ’s mistakes”. But I’m a glutton for punishment. :wink:

Rather than logging a warning/error, I’d suggest that the VSC extension indicate a problem in the workspace when it can’t find the item. That would serve as a reminder to restart VSC if you’ve just created that item (or add the item if you haven’t already done so).

@Confectrician, would this be possible (and worth doing)?

Theoretically this should be possible.

Anyway i am not doing deeper changes in the language parts, since i am simply not that experienced with handling language related parts.