OH3 ReferenceError: "logInfo" is not defined

Hi!
My rules script.

var BathRoomWater = itemRegistry.getItem('bathroom_water_temperature_p').getState()
var datenow = new Date()
var hoursnow = parseInt(datenow.getHours(),10)

if (BathRoomWater < 25 && hoursnow < 24 && 9 < hoursnow ) {
  events.sendCommand('bathroom_boiler_p', 'ON')
  logInfo("hoursnow", hoursnow);
}

if (BathRoomWater > 45) {
  events.sendCommand('bathroom_boiler_p', 'OFF')
  logInfo("hoursnow", hoursnow);
}

In log

Script execution of rule with UID '5e8dfb1b8d' failed: ReferenceError: "logInfo" is not defined in <eval> at line number 12

?

I guess the reason is the semicolon at the end of the ‘logInfo’ lines, that should not be there. I did some other improvements to your rule as well. Try:

var OnOffType newState

if (bathroom_water_temperature_p.state < 25) {
  if (now.getHour() < 24 && now.getHour() > 9) {
    newState = ON
  }
} else if (bathroom_water_temperature_p.state > 45) {
  newState = OFF
}

bathroom_boiler_p.sendCommand(newState)
logInfo("myrule", "hoursnow: " + now.getHour())
Script execution of rule with UID 'aaa326cc5f' failed: <eval>:1:14 Expected ; but found newState
var OnOffType newState
              ^ in <eval> at line number 1 at column number 14

Can you post your complete .rules file?

triggers:
  - id: "1"
    configuration:
      itemName: bathroom_water_temperature_p
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: |-
        var OnOffType newState

        if (bathroom_water_temperature_p.state < 25) {
          if (now.getHour() < 24 && now.getHour() > 9) {
            newState = "ON"
          }
        } else if (bathroom_water_temperature_p.state > 45) {
          newState = "OFF"
        }

        bathroom_boiler_p.sendCommand(newState)
        logInfo("myrule", "hoursnow: " + hoursnow)
    type: script.ScriptAction

Try changing the following line:

type: application/javascript

in

type: application/vnd.openhab.dsl.rule
11:44:33.481 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'aaa326cc5f' failed: var OnOffType  ___ newState

if (bathroom_water_temperature_p.state < 25) {
  if (now.getHour() < 24 && now.getHour() > 9) {
    newState = "ON"
  }
} else if (bathroom_water_temperature_p.state > 45) {
  newState = "OFF"
}

bathroom_boiler_p.sendCommand(newState)
logInfo("myrule", "hoursnow: " + hoursnow)
   1. The method or field hoursnow is undefined; line 12, column 293, length 8
   2. Duplicate local variable newState; line 1, column 14, length 8
   3. Type mismatch: cannot convert from String to OnOffType; line 5, column 135, length 4
   4. Type mismatch: cannot convert from String to OnOffType; line 8, column 211, length 5

Hmm I don’t have any experience using UI, all my configurations are in text files. My example works fine in a .rules file. You could try that, or wait until someone with more UI experience has an alternative.

Last try:

triggers:
  - id: "1"
    configuration:
      itemName: bathroom_water_temperature_p
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        var OnOffType newState

        if (bathroom_water_temperature_p.state < 25) {
          if (now.getHour() < 24 && now.getHour() > 9) {
            newState = ON
          }
        } else if (bathroom_water_temperature_p.state > 45) {
          newState = OFF
        }

        bathroom_boiler_p.sendCommand(newState)
        logInfo("myrule", "hoursnow: " + now.getHour())
    type: script.ScriptAction

Working!
If I use rules file. Not UI
Thanks a lot!

p.s. UI bad

Nothing wrong with the UI, you just cannot bits of javascript and bits of DSL in one rule and expect it to work. It won’t work in a file, either.

3 Likes

I’m having the same error.
And with all that is said in this thread, it’s still unclear to me on how to instruct OH3 to log a string containing the value of a parameter using dsl rules.
I’m trying to rework my config using the UI as much as possible as I read that’s the recommendation but so far, I’m having trouble finding decent info on how to do that.

So what would be the correct line of code in dsl to log the state of an item switch named Gar_sch_Licht?
I tried all kind of things: the last one is taken from here: Docs » Logging on github where they talk about LogAction. But if I select the Rules DSL tab, I get to see only this as the code which I know doesn’t work.

logInfo(“EXAMPLE”, “Example info log [{}]”, 5 + 5)
logWarn(“EXAMPLE”, “Example warning log [{}]”, 5 + 5)
logError(“EXAMPLE”, “Example error log [{}]”, 5 + 5)
logDebug(“EXAMPLE”, “Example debug log [{}]”, 5 + 5)

Any help is very much appreciated.

Thanks,

Steven.

Please post your complete rule. This is about context, not magic syntax.

ok
been searching for days, the moment I post this message the logging starts working
 :anguished:
I don’t know what has changed but at least I’ve made progress again. :smiley:

That is: the error changed, so it’s maybe no longer the best thread to keep adding to but I’ve a feeling that there might still be a mixup between javascript & DSL in play so here goes:

WHEN

When → a trigger channel fires
Channel → dimmer_switch_event (Dimmer Switch Event)
Event →

THEN
Execute a given script (dsl):

logInfo("DimmerTest", "receivedEvent from remote = {}", ChannelTriggeredEvent.getEvent().toString)

I’d like to continue working with that event data in a switch - case rule after having it logged


The error now reads:

Script execution of rule with UID ‘d804b360b2’ failed: logInfo(“DimmerTest”, “receivedEvent from remote = null”, ___ ChannelTriggeredEvent.getEvent().toString)
The method or field ChannelTriggeredEvent is undefined; line 1, column 56, length 21

so now not logInfo but ChannelTriggeredEvent is undefined.

Thank you,

Well, yes. That’s not a recognized DSL keyword.

For a channel trigger you are probably interested in the receivedEvent implicit variable.

well, that was easy enough :blush:
You can’t believe how relieved I am right now
 :star_struck:

Thank you!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.