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.
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.
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.
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