I would like to create a rule but don’t know how the code should look like. I already created many rules, but don’t know how to achieve this:
If any item of the group “Lights” is ON for e.g. 2 hours (and was not switched off during that time) then do something, e.g. turn it off.
The challenges here are: “an item of a group” in combination with “and was not switched off for too long” and “turn this specific light off, not simply all”.
As a first step I would suggest to persist the state of all group members, everyChange should suffice.
Next step would be to do a cron job like:
rule "switch off light after 2 hours"
when
Time cron "0 * * * * ?" //once a minute
then
var lightsOn = MyGroup?.members.filter(s | s.state != OFF) //if only switches are members
if(!lightsOn.isEmpty) //are there any lights on?
lightsOn.forEach[o |
if(!o.changedSince(now.minusHours(2)))
o.sendCommand(OFF)
]
end
I did not test this, but in theory it should work at least, openHAB Designer throws no errors.
The rule gets a list lightsOn of all lights which are ON (within MyGroup).
For each member of lightsOn, it does a test if this item did not change since 2 hours. If so, the item is switched OFF.
As every switch returns only ON, OFF (or null/uninitialized) and every dimmer returns 0 to 100 (or null/uninitialized),you will have to build two groups, one for all switches and one for all dimmers (and of course two rules…)
For testing the rule, you could change minusHours(2) to minusSeconds(30) and change trigger e.g. to cron "0/15 * * * * ?"
I guess, the error is about now and minusHours, that would be because openHAB Designer does not know about joda.time.
The error should vanish with this import at the very beginning of rule file:
import org.joda.time.DateTime
However, this is not needed by openHAB, because this is standard import since ~ OH 1.7(?)
Hi,
I’m affraid that’s not the problem. It says “There is no context to infer the closure’s argument types from. Consider typing the arguments or put the closures into a typed context.” for the first error and “Type mismatch: cannot convert from Object to Item” for the two other errors.
If I change o.sendCommand(OFF) to sendCommand(o, OFF), the error is gone…
But as I said, it works like you wrote and you can see attached:
Don’t forget to add { after the second if clause and } before the ] because sendBroadcastNotification would be executed regardless if o did or did not change.
Hello @Udo_Hartmann,
I used the rule you posted and had some problems. Initially the lastupdated would not change. I have as default persitence mapDB so I went in the persist file and added everyUpdate as well as everyChange was there. I now see the correct time the lights were turned on. The changedSince condition seems to not work.
Here’s my rule
rule "switch off light after 2 hours"
when
Time cron "0 * * * * ?" //once a minute
then
var lightsOn = gAutoTurnOff.members.filter(s | s.state != OFF && s.state != NULL) //if only switches are members of gAutoTurnOff
if(!lightsOn.isEmpty){ //are there any lights on?
lightsOn.forEach[o |
if(!o.changedSince(now.minusMinutes(2))){
logInfo("auto-lights-off.rules","Item was updated since "+o.lastUpdate+ " and changed "+ o.changedSince(now.minusMinutes(2)) + " and updated "+ o.updatedSince(now.minusMinutes(2)))
}
]
}
end
and this is what I get from the logs
2018-06-19 15:21:56.233 [ome.event.ItemCommandEvent] - Item 'Living_Room_Lamp' received command ON
2018-06-19 15:21:56.259 [vent.ItemStateChangedEvent] - Living_Room_Lamp changed from OFF to ON
………………..
==> /var/log/openhab2/openhab.log <==
2018-06-19 15:22:00.100 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:23:00.116 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:24:00.099 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:25:00.107 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:26:00.117 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:27:00.123 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:28:00.127 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:29:00.110 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
2018-06-19 15:30:00.129 [INFO ] [e.model.script.auto-lights-off.rules] - Item was updated since 2018-06-19T15:21:56.255+03:00 and changed false and updated true
Yes, that’s correct.
mapdb does only provide the actual state of an item (with timestamp of last update), but the rule will need historical data as well, so please either use rrd4j or another persistence service, like jdbc (in combination with MySQL, MariaDB, PostgreSQL, SQLite or whatever other database) .