Hello all,
I hope a comment on this pattern is a good place to put my question - if it is not, please let me know. I will open up a new thread then.
My goal
I want to send out notifications via telegram from rules
My approach
- proxy string
TNotificationViaTelegram
for the message to be sent - any rule may set it, e.g.
TNotificationViaTelegram.sendCommand(tMsg)
- a central rule which reacts to changes in this item:
rule "Send Notification over Telegram"
//notify via Telegram
when
Item TNotificationViaTelegram received update
then
if (SNotificationViaTelegram.state != ON) {
return;
}
val String msg = TNotificationViaTelegram.state.toString
sendTelegram("HomeInfo", msg)
end
Problem
In the Telegram message stream, I sometimes miss a message and instead get the same message twice in quick succession.
I am pretty certain, that this is what is going wrong:
- rule A updates
TNotificationViaTelegram
- rule B updates
TNotificationViaTelegram
(in quick succession - this e.g. happens by rules which react to presence) - rule
Send Notification over Telegram
gets triggered by the rule A update, but the actual sending happens, rule B has already changed the content - ergo the rule B message gets sent - rule
Send Notification over Telegram
gets triggered by the rule B update, the rule B message gets sent
Solution approaches
Note: these are the ones I can think of, I have not tried out any of them yet - still theorizing
-
Add a Thread::sleep after every instance of
TNotificationViaTelegram.sendCommand(tMsg)
- Unsexy approach, as it is not centralized and I have to think of it whenever I sent a message
- I don’t think it will work 100%, as when a certain event (presence) triggers two rules, these two rules might still update
TNotificationViaTelegram
at nearly the same point of time. This would be less likely to happen, if I use randomized sleep-times, which makes working with this approach even more complicated.
-
Approach as suggested in the “Simple Example”
- If I understand correctly, this would not solve my problem (or would it??).
I believe the approach makes a rule a singleton. But my problem is not the rule running in parallel, but thatTNotificationViaTelegram
is changed while the rule is running.
- If I understand correctly, this would not solve my problem (or would it??).
-
Approach as suggested in the “Complex Example using Queues”
- In essence, a FIFO queue is what would solve my problem. The way the example is set up though, the queue does not span across mutiple rules-files - correct?
So what is your view on this? Can anyone give me some pointers how to get this done?
Maybe there is a simple mechanism which I am use but don’t know or fail to recognize?
By the way: @rlkoshak - I have been using OpenHab for ca. 2 years now and your posts in this forum have been tremendously helpful for my understanding. I am amazed how often you find the time to take part in a discussion and help out with your knowledge and views.
Thank you very much for that!