Rule for several items instead of one rule for each

  • Platform information:
    • openHAB version: 2.5.4

I am adding DateTime stamps to several items so I can see the last time it was updated. The window or door was last updated on XX.

I’ve made a very simple rule but I am sure there is a much smarter way to do this than to make one rule per item.

rule "Back door openeing detected"
when Item BackDoor_DoorStatus changed
then
BackDoorDate.postUpdate(new DateTimeType)
logInfo("file", "Backdoor date updated")
end

Should I be able to make a rule that runs when a member of the group changed, then pick out which member of that group changed so I can update the correct DateTime item? Without a long series of if statements one for each possible item?
Any advice would be welcome.

There are several answers to your question:

  1. Yes, you can run rules on group members. A good place to start is here:
  1. You can set multiple when statements for a rule to trigger the same rule with different items. See the use of the implicit variable triggeringItem here:
  1. Your best bet, in this case is probably to just do this without rules at all. Item profiles can set timestamps for you automatically:

I think this does pretty similar:

rule "Temperature min/max updates"
when
    Member of g_temperature received update
then
    logInfo("Min_Max Rules update","Posting an min and max updates to " + triggeringItem.name)
    val minUpdateItem = g_temperature_min_max.members.findFirst[ lu | lu.name == triggeringItem.name + "_min" ]
    minUpdateItem.postUpdate(triggeringItem.minimumSince(now.withTimeAtStartOfDay).state)
    val maxUpdateItem = g_temperature_min_max.members.findFirst[ lu | lu.name == triggeringItem.name + "_max" ]
    maxUpdateItem.postUpdate(triggeringItem.maximumSince(now.withTimeAtStartOfDay).state)
end

Any of the items in g_temperature receive an update then I set a value.

Ok the profiles are intriguing, I made an attempt to set this up via an items file but just could not find an example I could follow online.

I ended up using paper UI to make an entry for the profile and it is working great. Created a DateTime item in the items file which I could format the way I wanted and then linked in paper UI.
Thanks for the tip.

DateTime vFan_LastChange { channel="mqtt:topic:thermostat:fan" [profile="timestamp-change"] }

When the indicated Channel receives a message this Item is updated with the current DateTime.

1 Like

Cool!
I couldn’t find this in the documentation. Found a description of only two profiles: FollowProfile, OffsetProfile
What other profiles do you know?

There’s loads: https://www.openhab.org/docs/configuration/items.html#profiles

1 Like

I’m sorry, lost sight of, I was here.

1 Like

I did it through a rule, something like this:

rule "LastChange"
when
    Member of gLastChange changed
then
    sendCommand(triggeringItem.name + "_Change", now.toString)
end

Now I tried it through the profile, as you showed.
Everything looks nice and simple, but as it turned out in practice, it works correctly only until the OpenHAB is rebooted.
After a reboot, items receive the change time and update time as the server reboot time, and not the real change time, as the rule worked.
Thanks, but I’ll stick with the rules, unless someone pokes me in what I’m doing wrong.
Thanks anyway!

As your Item seems to be MQTT linked …
When openHAB reboots and the MQTT binding starts up, it signs on to your MQTT broker for a topic.
Exactly what happens then depends on how the publisher is configured, for example it might have set “retain”, in which case your openHAB Item will get updated with a historic value. That’s a change, whether you think it real or not.

DateTime    Room_Climate_Temp_Change             "Change [%1$tH:%1$tM %1$td.%1$tm]"     <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature" [profile="timestamp-change"]}
DateTime    Room_Climate_Temp_Update             "Update [%1$tH:%1$tM %1$td.%1$tm]"    <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature" [profile="timestamp-update"]}

Okay, so what does mihome binding do at startup time?

items:

Number      Room_Climate_Temp                    "Температура в комнате [%.1f °C]"        <temperature>    (gHistory)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature"}
Number      Room_Climate_Humi                    "Влажность в комнате [%.0f %%]"          <humidity>       (gHistory,gLastUpdate)                                 {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:humidity"}
Number      Room_Climate_Press                   "Давление [%.0f кПа]"                    <pressure>       (gHistory,gLastUpdate)                                 {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:pressure"}
Number      Room_Climate_Battery                 "Климат в комнате [%d%%]"                <battery>        (gHistory,gMinLevelBattery)                            {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:batteryLevel"}
Switch      Room_Climate_BatteryLow              "Климат в комнате [%s]"                  <lowbattery>     (gHistory,gBatteryLow)                                 {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:lowBattery"}

DateTime    Room_Climate_Temp_Update             "Обновлено [%1$tH:%1$tM %1$td.%1$tm]"    <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature" [profile="timestamp-update"]}
DateTime    Room_Climate_Temp_Change             "Изменено [%1$tH:%1$tM %1$td.%1$tm]"     <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature" [profile="timestamp-change"]}
DateTime    Room_Climate_Humi_Update             "Обновлено [%1$tH:%1$tM %1$td.%1$tm]"    <time>           (gRestore)
DateTime    Room_Climate_Humi_Change             "Изменено [%1$tH:%1$tM %1$td.%1$tm]"     <time>           (gRestore)
DateTime    Room_Climate_Press_Update            "Обновлено [%1$tH:%1$tM %1$td.%1$tm]"    <time>           (gRestore)
DateTime    Room_Climate_Press_Change            "Изменено [%1$tH:%1$tM %1$td.%1$tm]"     <time>           (gRestore)
DateTime    Room_Climate_Battery_Update          "Обновлено [%1$tH:%1$tM %1$td.%1$tm]"    <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:batteryLevel" [profile="timestamp-update"]}
DateTime    Room_Climate_Battery_Change          "Изменено [%1$tH:%1$tM %1$td.%1$tm]"     <time>           (gRestore)                                             {channel="mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:batteryLevel" [profile="timestamp-change"]}

log:

2020-08-20 11:22:08.373 [DEBUG] [internal.handler.XiaomiBridgeHandler] - Adding item listener for device 158d0001d36b65
2020-08-20 11:22:08.379 [TRACE] [internal.handler.XiaomiBridgeHandler] - Received message {"cmd":"get_id_list_ack","sid":"286c07fa22a3","token":"ZLNCOTQltYV8rhrr","data":"[\"158d0001e56c1c\",\"158d0001b7b5c1\",\"158d0001b7acc7\",\"158d0001b91d19\",\"158d00014cec71\",\"158d00015afb4b\",\"158d000159c0f2\",\"158d0001d36b65\",\"158d000149d797\",\"158d0001832cf4\",\"158d00010c14aa\",\"158d000319bfaf\",\"158d000302c566\",\"158d00023915a3\",\"158d00020598b3\",\"158d0001291464\"]"}
2020-08-20 11:22:08.403 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Device 158d0001d36b65 on channel mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:humidity received command REFRESH
2020-08-20 11:22:08.403 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Device 158d0001d36b65 on channel mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:temperature received command REFRESH
2020-08-20 11:22:08.403 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Device 158d0001d36b65 on channel mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:pressure received command REFRESH
2020-08-20 11:22:08.412 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Device 158d0001d36b65 on channel mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:batteryLevel received command REFRESH
2020-08-20 11:22:08.412 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Device 158d0001d36b65 on channel mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65:lowBattery received command REFRESH
2020-08-20 11:22:08.446 [DEBUG] [internal.handler.XiaomiBridgeHandler] - Send to bridge mihome:bridge:286c07fa22a3: {"cmd": "read", "sid": "158d0001d36b65"}
2020-08-20 11:22:08.450 [TRACE] [.mihome.internal.socket.XiaomiSocket] - Sending message: {"cmd": "read", "sid": "158d0001d36b65"} to /192.168.203.140:9898
2020-08-20 11:22:08.710 [TRACE] [internal.handler.XiaomiBridgeHandler] - Received message {"cmd":"read_ack","model":"weather.v1","sid":"158d0001d36b65","short_id":60013,"data":"{\"voltage\":2865,\"temperature\":\"2170\",\"humidity\":\"6216\",\"pressure\":\"96066\"}"}
2020-08-20 11:22:08.712 [TRACE] [internal.handler.XiaomiBridgeHandler] - Updated "last time seen" for device 158d0001d36b65
2020-08-20 11:22:08.726 [DEBUG] [internal.handler.XiaomiBridgeHandler] - Device 158d0001d36b65 honored read request
2020-08-20 11:22:08.726 [DEBUG] [rnal.handler.XiaomiDeviceBaseHandler] - Item got update: {"cmd":"read_ack","model":"weather.v1","sid":"158d0001d36b65","short_id":60013,"data":"{\"voltage\":2865,\"temperature\":\"2170\",\"humidity\":\"6216\",\"pressure\":\"96066\"}"}

events:

2020-08-20 11:22:06.410 [hingStatusInfoChangedEvent] - 'mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65' changed from UNINITIALIZED to UNINITIALIZED (BRIDGE_UNINITIALIZED)
2020-08-20 11:22:07.462 [hingStatusInfoChangedEvent] - 'mihome:bridge:286c07fa22a3' changed from INITIALIZING to ONLINE
2020-08-20 11:22:07.505 [hingStatusInfoChangedEvent] - 'mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65' changed from UNINITIALIZED (BRIDGE_UNINITIALIZED) to INITIALIZING
2020-08-20 11:22:08.381 [hingStatusInfoChangedEvent] - 'mihome:gateway:286c07fa22a3:286c07fa22a3' changed from INITIALIZING to ONLINE
2020-08-20 11:22:08.384 [hingStatusInfoChangedEvent] - 'mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65' changed from INITIALIZING to OFFLINE
2020-08-20 11:22:08.727 [hingStatusInfoChangedEvent] - 'mihome:sensor_weather_v1:286c07fa22a3:158d0001d36b65' changed from OFFLINE to ONLINE
2020-08-20 11:22:08.729 [vent.ItemStateChangedEvent] - Room_Climate_Humi changed from NULL to 62.16
2020-08-20 11:22:08.733 [vent.ItemStateChangedEvent] - Room_Climate_Temp_Change changed from NULL to 2020-08-20T11:22:08.730+0800
2020-08-20 11:22:08.738 [vent.ItemStateChangedEvent] - Room_Climate_Temp changed from NULL to 21.7
2020-08-20 11:22:08.742 [vent.ItemStateChangedEvent] - Room_Climate_Temp_Update changed from NULL to 2020-08-20T11:22:08.739+0800
2020-08-20 11:22:08.746 [vent.ItemStateChangedEvent] - Room_Climate_Battery changed from NULL to 41
2020-08-20 11:22:08.749 [vent.ItemStateChangedEvent] - Room_Climate_Battery_Update changed from NULL to 2020-08-20T11:22:08.746+0800
2020-08-20 11:22:08.753 [vent.ItemStateChangedEvent] - Room_Climate_Battery_Change changed from NULL to 2020-08-20T11:22:08.749+0800
2020-08-20 11:22:08.761 [vent.ItemStateChangedEvent] - Room_Climate_BatteryLow changed from NULL to OFF
2020-08-20 11:22:08.762 [vent.ItemStateChangedEvent] - Room_Climate_Press changed from NULL to 96.066
2020-08-20 11:22:14.777 [vent.ItemStateChangedEvent] - Room_Climate_Press_Change changed from NULL to 2020-05-15T09:50:06.000+0800
2020-08-20 11:22:15.021 [vent.ItemStateChangedEvent] - Room_Climate_Humi_Change changed from NULL to 2020-05-15T09:50:16.000+0800
2020-08-20 11:22:15.057 [vent.ItemStateChangedEvent] - Room_Climate_Humi_Update changed from NULL to 2020-08-20T10:56:06.000+0800
2020-08-20 11:22:15.143 [vent.ItemStateChangedEvent] - Room_Climate_Press_Update changed from NULL to 2020-08-20T10:56:06.000+0800

Okay, all looks as expected. Your Items get updated by the binding at reboot time with data fetched from the device, and the timestamp is updated to match. Or rather, both timestamps, where you have one for update and one for change.

But that process is having a fight with your restore-on-startup process. There’s a race about whether persistence restore or binding refresh acts first. You wouldn’t normally need restore where the bindng does updates at reboot time.

What is you are trying to do? You certainly can exercise more subtle controls by using rules instead of the profile auto-stamp, but it’s not often needed.

I want the data to be correct at all times, not just between server reboots. For example, the state of the batteries of the sensors, I want to know when the value of the battery charge changed exactly, and when it just updated, thereby I know that the sensor is alive. When was the last time the leakage sensor, etc.
And now it turns out that all data is reset on reboot. This is not the correct operation of the documented feature. Perhaps at least it would be nice to make a clarification in the documentation. I am not expressing any displeasure, but tried to find out if I understood correctly how it works.

Can you clarify what feature(s) you are on about? There are a number involved here.

My English is very bad, in a different way.
timestamp-update and timestamp-change
track events on the channel, not the items.
Thank you, my question is closed.