Script not running?

Thank you all very much for your help. I now managed to get it working in dsl:

val mailActions = getActions("mail","mail:smtp:sender_smtp")
val msg = new StringBuilder
var triggertDevices = battery_levels.members
triggertDevices.forEach [ i |
  if (i.state !== NULL) {
    if ((i.state as Number)  <= 75)
      {
        msg.append(i.name + ': ' + i.state.toString + '%\n')
        logInfo("low_bat-send_mail.rules","Lavt batteriniveau på " + i.name + ": " + i.state.toString + "%")
      }
    }
  ]

if (msg != "") {
  val success = mailActions.sendMail("mail@xxxxxx.dk", "Batteri advarsel - openHAB: -" , msg.toString)
}

and in Blocky

The Ruby script though does absolutely nothing - and no errors in the logs

Ahhh and now I also managed to get the Ruby script running:

low_battery_items = battery_levels.members.filter(&:state?).select { |i| i.state < 75 }
if low_battery_items.any?
  message = low_battery_items.map { |i| "#{i}: #{(i.state).round}%" }.join("\n")
  message = "Batteri advarsel - openHAB:\n#{message}"
  logger.info(message)
  things["mail:smtp:sender_smtp"].send_mail("mail@xxxxxx.dk", "test", message)
end

What was wrong?

In the Ruby script the < 0.75 did not trigger - it must be <75 :slight_smile: and then a subject string in the mail was missing.

d’oh, of course, there would be no errors / no logs etc!

Well now you know how to do it in all 4 languages :slight_smile: (see the code generated by blockly in javascript)

1 Like

@Mherwege, @jimtng, @Oliver2, @Larsen, because this keeps comming up (this is the second time I’ve encountered this problem today) I’ve written a tutorial on how to convert a file based rule to a managed rule. All this content is in the docs but having a tutorial on the forum to link to can be handy.

2 Likes