Mail action

Hi all! I want to make if item goes offline oh send me mail and i do it

Summary

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

// Global Variables^M

rule "Test Mail"
when
Item Sk1_power_reachable changed to Offline or
Item Sk2_power_reachable changed to Offline or
Item s001_power_reachable changed to Offline or
Item s002_power_reachable changed to Offline or
Item s003_power_reachable changed to Offline or
Item s004_power_reachable changed to Offline or
Item s005_power_reachable changed to Offline or
Item s006_power_reachable changed to Offline or
Item s007_power_reachable changed to Offline or
Item s008_power_reachable changed to Offline or
Item s009_power_reachable changed to Offline or
Item s010_power_reachable changed to Offline or
Item s011_power_reachable changed to Offline or
Item s012_power_reachable changed to Offline or
Item s013_power_reachable changed to Offline or
Item s014_power_reachable changed to Offline or
Item s015_power_reachable changed to Offline or
Item s016_power_reachable changed to Offline or
Item s017_power_reachable changed to Offline or
Item s018_power_reachable changed to Offline
then
// send an email
sendMail(“mail”, “Item go offline!”, “”)
end

i want that oh send me name of item whiсh go offline. I try add + OfflineThings.state + but it not work

Summary

sendMail(“mail”, “Item go offline!”, "Have detected " + OfflineThings.state + “T hings offline.”)

2017-11-24 16:32:30.559 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'test2.rules’
2017-11-24 16:32:30.637 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘test2.rules’ is either empty or cannot be parsed correctly!
2017-11-24 16:32:31.289 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘test2.rules’, using it anyway:
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
2017-11-24 16:32:31.325 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'test2.rules’
2017-11-24 16:33:09.923 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘test2.rules’, using it anyway:
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
2017-11-24 16:33:10.065 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test2.rules’
2017-11-24 16:33:18.058 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘test2.rules’, using it anyway:
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
2017-11-24 16:33:18.355 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test2.rules’
2017-11-24 16:35:26.898 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Test Mail’: An error occurred during the script execution: The name ‘OfflineThings’ cannot be resolved to an item or type.

Anybody can help with rule?)

Looking into it

Hi,
First of all, try to use the code brackets. It makes everything more readable.
image

I’m assuming you use OpenHAB 2. In that case this part:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

is not needed anymore. (That’s the error message “The use of wildcard imports is deprecated.”).

Also, can you share your .items file? It seems that the item “OfflineThings” does not exist.

This post was flagged by the community and is temporarily hidden.

I respect your preference for spoilers and linking to sendspace. But the majority here prefers code brackets. I need to register on sendspace to see your rules. Sorry, I don’t want to do that. Just past it here using the code brackets.

1 Like

items




/* SONOFF */


Switch s001_power "Sonoff 001 Power" { mqtt=">[broker:cmnd/s001/power:command:*:default], <[broker:stat/s001/POWER:state:default]" }
String s001_power_reachable "Sonoff 001 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s001/LWT:state:default]" }
Number s001_power_rssi "Sonoff 001 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s001/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Switch s002_power "Sonoff 002 Power" { mqtt=">[broker:cmnd/s002/power:command:*:default], <[broker:stat/s002/POWER:state:default]" }
String s002_power_reachable "Sonoff 002 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s002/LWT:state:default]" }
Number s002_power_rssi "Sonoff 002 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s002/STATE:state:JSONPATH($.Wifi.RSSI)]" }


/* MSK */

Switch s003_power "Sonoff 003 Power" { mqtt=">[broker:cmnd/s003/power:command:*:default], <[broker:stat/s003/POWER:state:default]" }
String s003_power_reachable "Sonoff 003 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s003/LWT:state:default]" }
Number s003_power_rssi "Sonoff 003 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s003/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s004_power "Sonoff 004 Power" { mqtt=">[broker:cmnd/s004/power:command:*:default], <[broker:stat/s004/POWER:state:default]" }
String s004_power_reachable "Sonoff 004 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s004/LWT:state:default]" }
Number s004_power_rssi "Sonoff 004 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s004/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s005_power "Sonoff 005 Power" { mqtt=">[broker:cmnd/s005/power:command:*:default], <[broker:stat/s005/POWER:state:default]" }
String s005_power_reachable "Sonoff 005 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s005/LWT:state:default]" }
Number s005_power_rssi "Sonoff 005 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s005/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s006_power "Sonoff 006 Power" { mqtt=">[broker:cmnd/s006/power:command:*:default], <[broker:stat/s006/POWER:state:default]" }
String s006_power_reachable "Sonoff 006 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s006/LWT:state:default]" }
Number s006_power_rssi "Sonoff 006 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s006/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s007_power "Sonoff 007 Power" { mqtt=">[broker:cmnd/s007/power:command:*:default], <[broker:stat/s007/POWER:state:default]" }
String s007_power_reachable "Sonoff 007 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s007/LWT:state:default]" }
Number s007_power_rssi "Sonoff 007 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s007/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s008_power "Sonoff 008 Power" { mqtt=">[broker:cmnd/s008/power:command:*:default], <[broker:stat/s008/POWER:state:default]" }
String s008_power_reachable "Sonoff 008 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s008/LWT:state:default]" }
Number s008_power_rssi "Sonoff 008 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s008/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s009_power "Sonoff 009 Power" { mqtt=">[broker:cmnd/s009/power:command:*:default], <[broker:stat/s009/POWER:state:default]" }
String s009_power_reachable "Sonoff 009 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s009/LWT:state:default]" }
Number s009_power_rssi "Sonoff 009 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s009/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s010_power "Sonoff 010 Power" { mqtt=">[broker:cmnd/s010/power:command:*:default], <[broker:stat/s010/POWER:state:default]" }
String s010_power_reachable "Sonoff 010 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s010/LWT:state:default]" }
Number s010_power_rssi "Sonoff 010 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s010/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s011_power "Sonoff 011 Power" { mqtt=">[broker:cmnd/s011/power:command:*:default], <[broker:stat/s011/POWER:state:default]" }
String s011_power_reachable "Sonoff 011 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s011/LWT:state:default]" }
Number s011_power_rssi "Sonoff 011 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s011/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s012_power "Sonoff 012 Power" { mqtt=">[broker:cmnd/s012/power:command:*:default], <[broker:stat/s012/POWER:state:default]" }
String s012_power_reachable "Sonoff 012 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s012/LWT:state:default]" }
Number s012_power_rssi "Sonoff 012 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s012/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s013_power "Sonoff 013 Power" { mqtt=">[broker:cmnd/s013/power:command:*:default], <[broker:stat/s013/POWER:state:default]" }
String s013_power_reachable "Sonoff 013 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s013/LWT:state:default]" }
Number s013_power_rssi "Sonoff 013 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s013/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s014_power "Sonoff 014 Power" { mqtt=">[broker:cmnd/s014/power:command:*:default], <[broker:stat/s014/POWER:state:default]" }
String s014_power_reachable "Sonoff 014 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s014/LWT:state:default]" }
Number s014_power_rssi "Sonoff 014 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s014/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Switch s015_power "Sonoff 015 Power" { mqtt=">[broker:cmnd/s015/power:command:*:default], <[broker:stat/s015/POWER:state:default]" }
String s015_power_reachable "Sonoff 015 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s015/LWT:state:default]" }
Number s015_power_rssi "Sonoff 015 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s015/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s016_power "Sonoff 016 Power" { mqtt=">[broker:cmnd/s016/power:command:*:default], <[broker:stat/s016/POWER:state:default]" }
String s016_power_reachable "Sonoff 016 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s016/LWT:state:default]" }
Number s016_power_rssi "Sonoff 016 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s016/STATE:state:JSONPATH($.Wifi.RSSI)]" }


Switch s017_power "Sonoff 017 Power" { mqtt=">[broker:cmnd/s017/power:command:*:default], <[broker:stat/s017/POWER:state:default]" }
String s017_power_reachable "Sonoff 017 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s017/LWT:state:default]" }
Number s017_power_rssi "Sonoff 017 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s017/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Switch s018_power "Sonoff 018 Power" { mqtt=">[broker:cmnd/s018/power:command:*:default], <[broker:stat/s018/POWER:state:default]" }
String s018_power_reachable "Sonoff 018 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/s018/LWT:state:default]" }
Number s018_power_rssi "Sonoff 018 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/s018/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Switch Sk1_power "Sonoff Sk1 Power" { mqtt=">[broker:cmnd/Sk1/power:command:*:default], <[broker:stat/Sk1/POWER:state:default]" }
String Sk1_power_reachable "Sonoff Sk1 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/Sk1/LWT:state:default]" }
Number Sk1_power_rssi "Sonoff Sk1 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/Sk1/STATE:state:JSONPATH($.Wifi.RSSI)]" }
DateTime Sk1_LastUpdate     "Last seen [%1$td/%1$tm/%1$ty %1$tH:%1$tM]" <clock>

Switch Sk2_power "Sonoff Sk2 Power" { mqtt=">[broker:cmnd/Sk2/power:command:*:default], <[broker:stat/Sk2/POWER:state:default]" }
String Sk2_power_reachable "Sonoff Sk2 Power: reachable [%s]" (gReachable) { mqtt="<[broker:tele/Sk2/LWT:state:default]" }
Number Sk2_power_rssi "Sonoff Sk2 Power: RSSI [%d %%]" (gRSSI) { mqtt="<[broker:tele/Sk2/STATE:state:JSONPATH($.Wifi.RSSI)]" }


rules

// This rule file is autogenerated by HABmin.
// Any changes made manually to this file will be overwritten next time HABmin rules are saved.

// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

// Global Variables^M

rule "Test Mail"
when
	Item Sk1_power_reachable changed to Offline or
	Item Sk2_power_reachable changed to Offline or
	Item s001_power_reachable changed to Offline or
	Item s002_power_reachable changed to Offline or
	Item s003_power_reachable changed to Offline or
	Item s004_power_reachable changed to Offline or
	Item s005_power_reachable changed to Offline or
	Item s006_power_reachable changed to Offline or
	Item s007_power_reachable changed to Offline or
	Item s008_power_reachable changed to Offline or
	Item s009_power_reachable changed to Offline or
	Item s010_power_reachable changed to Offline or
	Item s011_power_reachable changed to Offline or
	Item s012_power_reachable changed to Offline or
	Item s013_power_reachable changed to Offline or
	Item s014_power_reachable changed to Offline or
	Item s015_power_reachable changed to Offline or
	Item s016_power_reachable changed to Offline or
	Item s017_power_reachable changed to Offline or
	Item s018_power_reachable changed to Offline
then
    // send an email
    sendMail("mail", "Item go offline!", "")
end

Thank for fast reply

Hi,
Very strange, your error says:

Rule ‘Test Mail’: An error occurred during the script execution: The name ‘OfflineThings’ cannot be resolved to an item or type.

But your rule doesn’t mention OfflineThings.

Also, your wildcards (import org.openhab.core.library.types.*) are still there.

Are you sure you use sendmail in the right way? See documentation.
I would expect something like:

sendMail("youremailadres@something.com", "Item goes offline!", "Something in the body of the mail")

I think this is also an interesting read for you:
Design Pattern: Working with Groups in Rules.
In summary:

  • Put all your items (Item Sk1_power_reachable, Item s009_power_reachable) in a group (e.g. gPowerStuff);
  • Change your trigger to “when gPowerStuff changed to…”. This makes it much easier;
  • You can refer to your triggered item as “gPowerStuff.members.sortBy[lastUpdate].last.state.toString”. Again, see the Design Pattern for details.

With this solution you need to create temporary items to store the state to compare it. Maybe there is something better but this works pretty good for me.
Create an rule.items file.

rule "Send Mail Offline"

when
    
Time cron "0 0/1 * 1/1 * ? *"
    
then
    /*
    * itemList is list of all your items you want mails from. 
    * example put into (s018_power_reachable s017_power_reachable) and so on
    * you will need to create for each item one extra item to store the state 
    * s017_power_reachable_old s018_power_reachable_old
    * Thoose _old items go into the itemList_old (s018_power_reachable_old  s017_power_reachable_old)
    * Thoose 2 lists must be in the EXACT same order. If not you will store Value ON from Item1 into maybe Item33
    */
    val itemList = newArrayList()
    val itemList_old = newArrayList()
    
    val StringBuilder msg = new StringBuilder
    
    
    for (var i = 0; i < itemList.size(); i++)
	{
	    var item = itemList.get(i)
	    var item_old = itemList_old.get(i)
		
	    if (item.state == OFF && item_old.state == ON)
		{
		    msg.append("Dev :" + item.name() + "is offline")
		    sendMail(your@mail.de, "Subject", msg.toString)
		    logInfo("Mail Info", "[INFO] Mail send")
		}
		item_old.setState(item.state)	    
	}
end

If you need help understanding this rule. Just leave a message.

Thanks for rule, i understand it

Hope it works as you expect it.

I will read!Thanks

Did it work?