Rule higher than 40 degrees notification sent

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version:
  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Dear community,

I am struggling with setting up a rule that notifies me if a sensor measures temperature higher than, e.g. 30 degrees Celsius.

I am clear with the notification part but for whatever reason, the rule doesn´t work. And yes, I checked the syntax tutorial and example rules but didn´t find a suitable answer.

Can you please help? I´d like to set the same things for battery level lower than 10% or rain drop higher than 100 etc.

Thanks so much.

Regards,
db

rule "Feueralarm Waschküche"

when 

Item zwave_device_XXXX_node70_temperature changed

then

if (zwave_device_35415c38_node70_temperature.state > 23,0) and
sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")

end

It should be working without “,” as decimal seperator:

if (zwave_device_35415c38_node70_temperature.state > 23) and
sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")

does he need the and at the end of that line? It seems to me that he should remove it

rule "Feueralarm Waschküche"
when 
	Item zwave_device_35415c38_node70_temperature changed
then
	if (zwave_device_35415c38_node70_temperature.state > 23) { 
		sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")
	}
end
1 Like

Stupid me, I missed that as well!
Thanks for stepping in.

Dear both,

thanks a lot - I will try it check, if it works.

Regards,
db

This would result in your rule sending the email everytime the temperature of your device changed and is above 23. if your cellar is normally always under 23 degress, that’s fine. An in your case (fire Alarm), it would be also OK I guess to have your inbox flooded… :wink:
Otherwise, you could add a Little more logic around that to be sure, the email/'message is only send once the temparature is higher than expected. I usually work with a timestamp in those cases - if there’s a better solution?

items:

DateTime	Firealarm_Lastupdate

rule:

rule "Feueralarm Waschküche"
when 
	Item zwave_device_35415c38_node70_temperature changed
then
	// if the device sends a temperature higher tahn 23° and the last firealarm is more than 1h ago (prevent email flooding)
	if (zwave_device_35415c38_node70_temperature.state > 23 && now.minusHours(1).isAfter(Firealarm_Lastupdate)) { 
		sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")
		postUpdate(Firealarm_LastUpdate, new DateTimeType())
	}
end

Hint: be sure to have Firealarm_Lastupdate be initialized on each startup either using persistence or with an valid timestamp - otherwise it will result in an ERROR in the rule

Hi - How would I initialize Firealarm_Lastupdate?

I’m trying something like this and a few other variations but I keep getting errors:
var DateTime Firealarm_LastUpdate = now.minusHours(1)

rule "Temperature Email Alert Test".....

In Thomas’s original code (post 6) Firealarm_Lastupdate is an Item. To initialize it you would need a System started triggered Rule to postUpdate to the Item and give it an initial value.

Though it would be better, as he recommends, to use persistence with restoreOnStartup so the last value gets restored when OH restarts. In that case you would only need the System started Rule that very first run. Or you can initialize the Item using the REST API docs.

Thanks Rich but i’m not sure what to do here. What would the syntax look like for this system start triggered rule? Can I create this item? I’m not really sure here how openhab handles a situation like this where it needs to remember the value of a variable in between running a rule.

You have to create this Item for the code above to work.

Items retain their state between Rule runs. This particular Item is called Design Pattern: Unbound Item (aka Virtual Item).

Global variables retain their state between Rule runs. https://www.openhab.org/docs/configuration/rules-dsl.html#the-syntax

Items configured with persistence and restoreOnStartup strategy retain their values between restarts of openHAB. https://www.openhab.org/docs/configuration/persistence.html#predefined-strategies

Design Pattern: Encoding and Accessing Values in Rules shows a lot of ways to initialize the value of an Item.

ok got it.
Here is what I have now:

Item:
DateTime Temperature_LastAlert “Last Temperature Alert Sent”

Startup Rule:
        rule "System Startup"

        when System started

        then postUpdate(Temperature_LastAlert, new DateTimeType())

    end

after creating the line in the items file and the startup rule i restarted openhab

when the temperature changed at startup from NULL to 62.6 I get the error:

2020-01-03 11:02:46.466 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Temperature Email Alert’: An error occurred during the script execution: Could not invoke method: org.joda.time.base.AbstractInstant.isAfter(long) on instance: 2020-01-03T10:02:46.426-05:00

There is probably a bug in the original code. You have to convert a DateTimeType to a Joda DateTime in order to compare them. See https://www.openhab.org/docs/configuration/rules-dsl.html#datetime-item.

Worked! Thank you!
I changed the dimmer by the ‘when’ for testing.

rule "Temperature Email Alert"

when Item ZWaveNode5NZW31InWallDimmingSwitch_Dimmer changed

then

val Joda_Temperature_LastAlert = new DateTime(Temperature_LastAlert.state.toString)

if (ZWaveNode4_SensorTemperature.state < 63 && now.minusHours(1).isAfter(Joda_Temperature_LastAlert)) { 
		sendMail("ezraadler@gmail.com","**Attic Temperature is Below 45 Degrees**","**Attic Temperature is below 45 degrees")
    logInfo("myTempLog", "Temperature change detected. Current temperature: " + ZWaveNode4_SensorTemperature.state + " degrees")
		postUpdate(Temperature_LastAlert, new DateTimeType())
	}


end