Rule with sequential actions in script block

openHAB version: 2.5.0

Hey guys,

if have tried to implement the following rule to send a picture from my entrance IPCam via telegram every time the front door has been opened:

rule "Front_Door_Opened"
when
Item Entrance_FrontDoor_contact changed from CLOSED to OPEN

then
	sendTelegramPhoto("bot1", Entrance_IPCam_image.state.toFullString, "Front door opened")
end

The above works totally fine and i receive the telegram message with picture.

As the state of the image from the cam is not automatically updated i receive the “old” picture (from the last update) every time, this is clear.

To get an updated image with the rule i tried to include the update in the script block:

rule "Front_Door_Opened"
when
Item Entrance_FrontDoor_contact changed from CLOSED to OPEN

then
	Entrance_IPCam_updateImageNow.sendCommand(ON)
	sendTelegramPhoto("bot1", Entrance_IPCam_image.state.toFullString, "Front door opened")
end

This works, but as the actions in the script block are performed at the same time, i only get the old state (image).
As i found in the openhab documentation this could be possible with implicit variables, but i dont know how.

Any help appreciated!

Greetings
Chris

I don’t see how. What you need to do is wait a little bit of time after the sendCommand(ON) before the sendTelegramPhoto. That has nothing to to do with implicit variables.

You have two choices here. If the wait is less than half a second (500 msec) you can use Thread::sleep(500) reasonably safely. Any longer of a sleep is dangerous though.

If you have to wait longer than half a second, then you will need to create a Timer.

createTimer(now.plusSeconds(1), [ | 
    sendTelegramPhoto("bot1", Entrance_IPCam_image.state.toFullString, "Front door opened") 
])

This will immediately return from the Rule but schedule the code between the to execute at some later time, in this case a second from now.

1 Like

You can tell when your new snapshot is ready, Entrance_IPCam_image will change, and you can trigger a rule off of that.
That will not tell you if the door was just opened, but if that is the only time you update the image then you can send the telegram.

Hey Rich,

the sleep works, but 500ms isn’t always enough so I have to experiment with the timer.

Thanks for the solution
KR
Chris

Unfortunately the camera isn’t only updated when the door contact triggers. But thanks for the hint

Psuedocode

Flag msgWanted = false

When door opened
Start capture
Set msgWanted = true

When capture available
If msgWanted
Send telegram
Set msgWanted false

Proper sequential, no delays

1 Like

Thx rossko, will give it a try!

Hey Rossko,

it works :slight_smile:

var Number numTTrigger = 0
rule "Front_Door_Opened"

when
Item Entrance_FrontDoor_contact changed from CLOSED to OPEN

then
	Entrance_IPCam_updateImageNow.sendCommand(ON)
	numTTrigger = 1

end

rule "Telegram_Send"

when 
	Item Entrance_IPCam_image changed

then
	if (numTTrigger == 1)
	sendTelegramPhoto("bot1", Entrance_IPCam_image.state.toFullString, "Front door opened")
	numTTrigger = 0
end

1 Like

Looks good. Short, sweet, event driven, no timing, and happens as quickly as it can.