The name 'newState' cannot be resolved to an item or type

  • Platform information:
    • Hardware: x64
    • OS: Ubuntu 18.04
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_241-b07)
    • openHAB version: 2.5.6-2

Hi everyone,

I tried quickly writing a new rule, but it’s turned out to not be so quick.

I’ve removed the guts of the rule to just focus on the actual issue. For some reason, it does’t appear to be setting the newState variable.

The items:

Rollershutter   GarageDoor
Switch  Remote_GarageDoor_1_0

The rule:

rule "Garage Door Movement Rule"
when
    Item Remote_GarageDoor_1_0 received command or
    Item GarageDoor received command
then
    logInfo("Garage", newState)
end

The event log
2020-07-12 16:33:01.948 [ome.event.ItemCommandEvent] - Item ‘GarageDoor’ received command UP

The openhab log
2020-07-12 16:33:01.949 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Garage Door Movement Rule’: The name ‘newState’ cannot be resolved to an item or type; line 35, column 23, length 8

Can anyone see what I’ve done wrong?

The newState implicit variable is only available when the rule is triggered by a state change or update (not command). For your rule, you’d want to use receivedCommand instead of newState, or change the trigger.

1 Like

Thanks for the reply Scott.

I’ve now updated the rule to what’s below. It doesn’t throw an error anymore but the value is null.

2020-07-12 19:12:41.263 [INFO ] [clipse.smarthome.model.script.Garage] - null

rule "Garage Door Movement Rule"
when
    Item Remote_GarageDoor_1_0 received command or
    Item GarageDoor received command
then
    logInfo("Garage", receivedCommand)
end

All good.

I’ve got it working now using the following.

rule "Garage Door Movement Rule"
when
    Item Remote_GarageDoor_1_0 changed or
    Item GarageDoor changed
then
    logInfo("Garage", receivedCommand)
end

Whatever you think is going on, that rule would not work. There is no receivedCommand to display.

Make sure you use unique rule “names”, it is not obvious that this is important (or they overwrite each other).
Check in your openhab.log after editing xxx.rules files, to see if the file refreshed successfully (otherwise old rule versions persist)

I’ve discovered the need for unique names the hard way in the past. It’s definitely unique. I’ve also greped the rules folder. There’s no other rules containing the word garage.

I have been checking the openhab log. Upon saving the file it says, “Refreshing model 'garage.rules”.

I can’t explain why it works or why “receivedCommand” did not, but it does.

Your last posted version of the rule is telling us that receivedCommand does work without a command rule trigger, which is what I was boggling at.