Openhab 2 Lastest Motiondetector logger

rules
openhab2
Tags: #<Tag:0x00007fe057c5fcd0> #<Tag:0x00007fe057c5fb90>

(wolf_from_the_north) #1

Hello Guys,

longtime no see :slight_smile:
I wanted to create a item for my sitemap that displays the name of the latest motion sensor activated.
till today i fail at this.
i have read multiple examples but they don’t seem to be working on my installation.

i have 3 motion detectors from fibaro working perfectly.
i have 1 group gMotionsensor and the items linked to this.
i have 1 string item [%s] in my items file

looking at some examples i tried this.
example below is with a item but tried the group as wel without effect.
i do not get any errors.
can someone help ?

rule "Process Intrusion Updates"
when
	Item gMotionAlarms changed from OFF to ON
then
    val motionLatest = gMotionAlarms.members.sortBy[lastUpdate].last
	val doorwindowLatest = DoorsWindows.members.sortBy[lastUpdate].last
    val latest = if(motionLatest.lastUpdate.after(doorwindowsLatest.lastUpdate)) motionLatest else doorwindowLatest

    val name = latest.name.split("_")
    LastMotionDetection.postUpdate("" + name.get(1) + " " + name.get(2) + " " + name.get(3) + " Intrusion Alert!, " + latest.state.toString)
	
end

items:

group gMotionAlarms

switch Z_MotionSensor_Bureau_M_Alarm
switch Z_MotionSensor_Livingroom_M_Alarm
switch Z_MotionSensor_Hallway_M_Alarm

string LastMotionDetection “Last motion detectecd on [%s]”

sitemap:

text item=LastMotionDetection

Persistence has been set up with rdd4j to persist the group gMotionAlarms on everychange, restoreonstartup


(Rich Koshak) #2
  1. Please use code fences when posting code or logs: How to use code fences

  2. Please post your Items and Group definitions

  3. Your rule only triggers on Z_MotionSensor_Bureau_M_Alarm which appears to be only one of the three sensors.

  4. If any members of the groups have not been saved to persistence then you will get errors because those items will return null for lastUpdate and you cannot sort null objects.


(wolf_from_the_north) #3

Hello rlkoshak,

I updated the post as you requested.
I saw this code in a previous topic used in openhab 1.8
it seems that when i take a look at the log the group is not recieving any update.
i use the fibaro motion sensors and i want to update a string value based on the name of the sensor so i know which sensor has been triggered last.
so in my items file i made a string item [%s] and in my sitemap a text item that refers to the string item.
the string item stays empty and checking the logs i don’t find any update done.
I do see that the motionsensor goes from off to on.


(Rich Koshak) #4

You need to give the Group a type. Otherwise OH will no longer attempt to calculate a state for it and therefore it never changes nor receives updates to trigger rules with.

Group:Switch:OR(ON,OFF) gMotionAlarms

Note, case matters. You must capitalize Group, Switch, and String when defining Items.

Add a logInfo as the first line of your rule. That will tell you whether the Rule is being triggered or not once you fix the Group problems.


(wolf_from_the_north) #5

hello,

i will try this tonight when i’m home and see if its working.
thank you for the input !

kindest regards


(wolf_from_the_north) #6

Updated my rule and items like sugested and checked the log.
i’m recieving updates to my groups so that is excellent.
the rule however gives me following error

Rule ‘Process Intrusion Updates’: ‘after’ is not a member of ‘null’; line 10, column 21, length 59

adjusted rule:

rule "Process Intrusion Updates"
when
	Item gMotionAlarms changed from OFF to ON or
	Item DoorsWindows changed from OFF to ON
then
	Thread::sleep(300)
	logInfo("MotionUpdate.rules", "Checking Last Motion Detection !")
	val motionLatest = gMotionAlarms.members.sortBy[lastUpdate].last
	val doorwindowLatest = DoorsWindows.members.sortBy[lastUpdate].last
    val latest = if(motionLatest.lastUpdate.after(doorwindowsLatest.lastUpdate)) motionLatest else doorwindowLatest
	val name = latest.name.split("_")
    LastMotionDetection.postUpdate("" + name.get(1) + " " + name.get(2) + " Intrusion Alert!, " + latest.state.toString)	
end

(Rich Koshak) #7

motionLatest is null. Are ALL members of gMotionAlarms initialized (i.e. not NULL) and are all of them successfully being saved to the database?

You can filter out he null ones using

val motionLatest = gMotionAlarms.members.filter[a | a.lastUpdate != null].sortBy[lastupdate].last

(wolf_from_the_north) #8

if i apply that syntax i get following error.

22:08:45.990 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘Process Intrusion Updates’: The name ‘lastupdate’ cannot be resolved to an item or type; line 9, column 83, length 10

how can i check if my persistence is working ?
i installed rrd4j and made a rrd4j.persist file with following info.

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
	everyMinute:"0 * * * * ?"
    everyHour : "0 0 * * * ?"
    everyDay  : "0 0 0 * * ?"

    // if no strategy is specified for an item entry below, the default list will be used
    default = everyChange, everyMinute, restoreOnStartup
}

/* 
 * Each line in this section defines for which item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * item (excl. the group item itself).
 */

Items {
    // persist all items once a day and on every change and restore them from the db at startup
	BatteryLevels* : strategy = everyMinute, restoreOnStartup
	Temperatures* : strategy = everyMinute, restoreOnStartup
	gMotionAlarms* : strategy = everyChange, restoreOnStartup
	// additionally, persist all temperature and weather values every hour
    
}

(wolf_from_the_north) #9

i checked for persistence and i do see some *.rdd files made in openHAB/userdata/persistence/rrd4j so i assume its doing its thing as the files are recently updated a minute ago.

kindest regards


(wolf_from_the_north) #10

checked the logs and it told me the value for null should be == null and not =null.
i changed that but now i’m getting error.

Rule ‘Process Intrusion Updates’: The name ‘lastupdate’ cannot be resolved to an item or type; line 9, column 84, length 10


(Rich Koshak) #11

lastUpdate

When you see an error like this:

Look at the line and column it is complaining about, in this case line 9 and column 83.

Read the error, what is it saying is wrong? In this case it says it doesn’t know the name ‘lastupdate’.

Look around, everywhere else it is used in this rule it is lastUpdate.

Problem solved.


(wolf_from_the_north) #12

Updated the rule with capital U :slight_smile:
i will test tommorrow if it works now.

thnx for the assist


(wolf_from_the_north) #13

mmm back to same error from before now.

Rule ‘Process Intrusion Updates’: ‘after’ is not a member of ‘org.joda.time.DateTime’; line 11, column 21, length 59

current rule

rule "Process Intrusion Updates"
when
	Item gMotionAlarms changed from OFF to ON or
	Item DoorsWindows changed from OFF to ON
then
	Thread::sleep(300)
	logInfo("MotionUpdate.rules", "Checking Last Motion Detection !")
	//val motionLatest = gMotionAlarms.members.sortBy[lastUpdate].last
	val motionLatest = gMotionAlarms.members.filter[a | a.lastUpdate !== null].sortBy[lastUpdate].last
	val doorwindowLatest = DoorsWindows.members.sortBy[lastUpdate].last
    val latest = if(motionLatest.lastUpdate.after(doorwindowsLatest.lastUpdate)) motionLatest else doorwindowLatest
	val name = latest.name.split("_")
    LastMotionDetection.postUpdate("" + name.get(1) + " " + name.get(2) + " Intrusion Alert!, " + latest.state.toString)	
end

(Rich Koshak) #14

I highly recommend using VSCode to edit rules. Properly configured it will identify problems and potentially how to fix it as you type.

isAfter not after


(wolf_from_the_north) #15

solved !

rule "Process Intrusion Updates"
when
	Item gMotionAlarms changed from OFF to ON or
	Item DoorsWindows changed from OFF to ON
then
	Thread::sleep(300)
	logInfo("MotionUpdate.rules", "Checking Last Motion Detection !")
	//val motionLatest = gMotionAlarms.members.sortBy[lastUpdate].last
	val motionLatest = gMotionAlarms.members.filter[a | a.lastUpdate !== null].sortBy[lastUpdate].last
	val doorwindowLatest = DoorsWindows.members.sortBy[lastUpdate].last
    val latest = if(motionLatest.lastUpdate.isAfter(doorwindowLatest.lastUpdate)) motionLatest else doorwindowLatest
	val name = latest.name.split("_")
    LastMotionDetection.postUpdate("" + name.get(1) + " " + name.get(2) + " Intrusion Alert!, " + latest.state.toString)	
end

(wolf_from_the_north) #16

ok the rule is now without errors but,
when i check the result it only takes the doorwindow and not the motionsensors.
i checked the logs but i don’t find anything strange , the motionsensors are updating the group.
any idea ?

2018-01-19 07:50:20.301 [vent.ItemStateChangedEvent] - LastMotionDetection changed from NULL to DoorWindowsSensor Garage Intrusion Alert!, CLOSED
2018-01-19 07:50:50.915 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from ON to OFF
2018-01-19 07:50:50.920 [GroupItemStateChangedEvent] - gMotionAlarms changed from ON to OFF through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:50:50.932 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from ON to OFF
2018-01-19 07:51:02.665 [vent.ItemStateChangedEvent] - Elevation changed from -7.85 to -7.13
2018-01-19 07:51:02.669 [vent.ItemStateChangedEvent] - Azimuth changed from 112.90 to 113.83
2018-01-19 07:51:43.495 [vent.ItemStateChangedEvent] - Z_SmartPlug_Bureau_PowerUsage changed from 76.27 to 76.28
2018-01-19 07:51:43.534 [vent.ItemStateChangedEvent] - Z_SmartPlug_Bureau_Power changed from 7.3 to 52.6
2018-01-19 07:52:20.427 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from OFF to ON
2018-01-19 07:52:20.435 [GroupItemStateChangedEvent] - gMotionAlarms changed from OFF to ON through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:52:20.446 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from OFF to ON
2018-01-19 07:52:20.785 [vent.ItemStateChangedEvent] - LastMotionDetection changed from DoorWindowsSensor Garage Intrusion Alert!, CLOSED to DoorWindowsSensor Garage Intrusion!, CLOSED
2018-01-19 07:52:50.590 [GroupItemStateChangedEvent] - gMotionAlarms changed from ON to OFF through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:52:50.595 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from ON to OFF
2018-01-19 07:52:50.608 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from ON to OFF
2018-01-19 07:53:33.499 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from OFF to ON
2018-01-19 07:53:33.505 [GroupItemStateChangedEvent] - gMotionAlarms changed from OFF to ON through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:53:33.521 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from OFF to ON
2018-01-19 07:54:03.987 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from ON to OFF
2018-01-19 07:54:03.991 [GroupItemStateChangedEvent] - gMotionAlarms changed from ON to OFF through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:54:04.005 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from ON to OFF
2018-01-19 07:54:44.384 [vent.ItemStateChangedEvent] - Z_SmokeSensor_Kitchen_Temp changed from 25.9 to 25.5
2018-01-19 07:56:02.002 [vent.ItemStateChangedEvent] - Z_SmartPlug_Bureau_Power changed from 52.6 to 36.2
2018-01-19 07:56:02.388 [vent.ItemStateChangedEvent] - Z_Relay_Bureau_Light changed from OFF to ON
2018-01-19 07:56:02.671 [vent.ItemStateChangedEvent] - Azimuth changed from 113.83 to 114.76
2018-01-19 07:56:02.675 [vent.ItemStateChangedEvent] - Elevation changed from -7.13 to -6.41
2018-01-19 07:56:15.996 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from OFF to ON
2018-01-19 07:56:16.002 [GroupItemStateChangedEvent] - gMotionAlarms changed from OFF to ON through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 07:56:16.015 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from OFF to ON

(wolf_from_the_north) #17

could persistence have something todo with it ?


(Rich Koshak) #18

What do you mean? motionLatest is null or motionLatest is always after doorwindowLatest?

Add logging to see what is being returned by lastUpdate on both.

All the Items in gMotionAlarms and DoorsWindows must be saved in persistence. And your persistence must be set up to store changes, not save every minute or something like that.


(wolf_from_the_north) #19

hello can you help me to add logging ?
as far as i searched i saw that the DoorsWindows was not persisted yet.
i added it now and checked the persistence file to log for every change of those groups.
for the motion sensors it was allready like that.
checking openhab log i did see the motionsensors still updating the group but nothing was hapening.
only the last detection from my garagedoor was there so in other words , the string item updated like you can see in the logs i posted but it never mentions anything for the motionsensors.
so in my opinion it defines DoorsWindows always as latest, i added that group now to persistence and will check now if this solves the incident.


(wolf_from_the_north) #20

hello,

just checked and added logging by mking a loginfo that statest the latest value of the string object.
even if the motionsensors go off it still states its the Doorswindows sensor.

rule

rule "Process Intrusion Updates"
when
	Item gMotionAlarms changed from OFF to ON or
	Item DoorsWindows changed from OFF to ON
then
	Thread::sleep(300)
	logInfo("MotionUpdate.rules", "Checking Last Motion Detection !")
	//val motionLatest = gMotionAlarms.members.sortBy[lastUpdate].last
	val motionLatest = gMotionAlarms.members.filter[a | a.lastUpdate !== null].sortBy[lastUpdate].last
	val doorwindowLatest = DoorsWindows.members.sortBy[lastUpdate].last
    val latest = if(motionLatest.lastUpdate.isAfter(doorwindowLatest.lastUpdate)) motionLatest else doorwindowLatest
	val name = latest.name.split("_")
    LastMotionDetection.postUpdate("" + name.get(1) + " " + name.get(2) + " Intrusion!, " + latest.state.toString)
	logInfo("MotionUpdate.rules", LastMotionDetection.state.toString)	
end

logs:

2018-01-19 18:10:14.388 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'MotionUpdate.rules'
2018-01-19 18:10:14.454 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'MotionUpdate.rules'
2018-01-19 18:10:40.678 [INFO ] [home.model.script.MotionUpdate.rules] - Checking Last Motion Detection !
2018-01-19 18:10:40.687 [INFO ] [home.model.script.MotionUpdate.rules] - DoorWindowsSensor Garage Intrusion!, CLOSED

2018-01-19 18:06:05.271 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Lux changed from 112 to 113
2018-01-19 18:10:40.332 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from OFF to ON
2018-01-19 18:10:40.337 [GroupItemStateChangedEvent] - gMotionAlarms changed from OFF to ON through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 18:10:40.352 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from OFF to ON
2018-01-19 18:11:03.356 [vent.ItemStateChangedEvent] - Azimuth changed from 248.15 to 249.08
2018-01-19 18:11:03.360 [vent.ItemStateChangedEvent] - Elevation changed from -8.56 to -9.29
2018-01-19 18:11:11.376 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_M_Alarm changed from ON to OFF
2018-01-19 18:11:11.381 [GroupItemStateChangedEvent] - gMotionAlarms changed from ON to OFF through Z_MotionSensor_Bureau_M_Alarm
2018-01-19 18:11:11.395 [vent.ItemStateChangedEvent] - Z_MotionSensor_Bureau_Tamper changed from ON to OFF