Set Mail Actions in one place and not in each rule file

OH 2.5.11

For mailactions is there a way to set that in ONLY one rule file and refer to it in other rule files?

So put this in one rule file:
val omailActions = getActions(“mail”,“mail:smtp:111111”)

Then use this throughout all my other rule files:
omailActions.sendMail(“”,“test”, " test " + now.getMonthOfYear + “/” + now.getDayOfMonth + " | " + now.getHourOfDay + “:” + now.getMinuteOfHour)


is correct.
Talking about DSL .rules files you could combine rules of different files into one file and use global variable in the resulting file.

While you may not want to combine files you could try: creating a configuration file where you define settings in one file. That file needs to be read and parsed by other rule files to set configuration/variables.

Using Rules-DSL I define items for the storage of the configuration:

// constant values to be used in various rules, need to be initialized in a system startup rule
String mailTo
String mailConfig

In a system-startup rule the items are populated with the data:

// initialization of constant values, needed in various rules:

For sending mails in various rules it is then something like this:

val message = "this is the content of the mailbody\n\n\nopenHab"
val mailActions = getActions("mail",mailConfig.state.toString)
mailActions.sendMail(mailTo.state.toString, "this is the mail subject", message)

In my case all of this is done in textual definitions and I do not know if it will work similarily in items/rules defined in the GUI.

1 Like

pretty easy…

just use a notification string item and update it whenever you want to send a mail with the content:

String Notification

create e.g. “notifcations.rules” with the email sending (i just use the push notification):

rule "Send Notification when Text changed"
    Item Notification received update
    if(!Notification.state.toString().isEmpty() && (now.getHour() >= 8 && now.getHour() <= 23) )
        sendNotification("", Notification.state.toString())
        // here would be "your" / the code for  email sending.

and from the rules, where you want to send anything, just update the Notification item:

.. or ..
Notification.postUpdate("this is my mail content..")
1 Like

The original post was about the action object, not the content or target address.

Correct; which means im SOL :slight_smile: Thanks rossko!

1 Like

You don’t want to do that with actions though. You’ll end up with a handle on the action on the Thing as it existed at the time the file was loaded. If the mail binding hasn’t come up yet, no Thing and therefore no action. Or if something goes wrong and the Thing goes away and comes back for some reason, you’ll have stale handle on the action and that won’t work.

For actions, you must get the handle on the action in the rule that calls it so you get the action on the latest active Thing.

@milty456, as @rossko57 and I indicate, you can’t do exactly what you want to do. However, if you were working with something other than the action, or open to other approaches there could be some ways that may work.

  • You can create a rule without triggers and at least one action. When you need to send an email, you can instead call that rule (possible in all rules languages (including Blockly) except Rules DSL). You can pass the information needed like email, subject, and message to this new rule. The advantage is if later on you want to move to Telegram or something like that, or want to set up conditions (e.g. don’t email me at night) you can easily do that all in the one place. But calling a rule with arguments is going to be more work in your calling rules than calling the action directly.

  • Rules DSL supports the concept of a “script” which is a block of Rules DSL code you can call. You can’t get a return from it though nor can you pass anything to it except through Item states. But if you put the email, subject and message in a String Item then used the callScript you could call a block of Rules DSL code that pulls the Mail Action and calls it using the states of the Items. Again, this is going to be more works for the callers than just calling the action themselves.

  • In just about every other language supported, you can create library functions you can call. Then all you need to do is import and call your custom function with the needed info as arguments.

One thing to note about Blockly is it will import the action as necessary as part of the use of the block. So you don’t really have to worry about that sort of thing there.