IPCamera snap send to email [SOLVED]

I am trying to imlement simple rule: After pressing the ring button, make a snapshot from camera and send it to email. However, it does not work.

I have created an IPCamera binding (I dont think it does matter in my script).
the in browser downloads the webcapture.jpg with valid photo.

rule "zvoncek-fotka"
    Channel "mihome:sensor_switch:286c0785f092:158d0001243994:button" triggered DOUBLE_PRESSED
    logInfo("zvoncek-fotka.rules", "Double pressed")
    val url = ""
    val outputFile = "/home/openhabian/webcam/webcapture.jpg"
    var cmd = "curl -m 10 -o " + outputFile + " " + url
    sendMail("m@gmail.com", "Noon", "Noon", "file:///home/openhabian/webcam/webcapture.jpg")

this is what i got from tail -f:

2021-03-22 21:36:30.672 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mihome:sensor_switch:286c0785f092:158d0001243994:button triggered DOUBLE_PRESSED
==> /var/log/openhab/openhab.log <==
2021-03-22 21:36:30.826 [INFO ] [ore.model.script.zvoncek-fotka.rules] - Double pressed
2021-03-22 21:36:30.862 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[curl -m 10 -o /home/openhabian/webcam/webcapture.jpg]'
java.io.IOException: Cannot run program "curl -m 10 -o /home/openhabian/webcam/webcapture.jpg": error=2, Adresár alebo súbor neexistuje
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1128) ~[?:?]
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1071) ~[?:?]
	at org.openhab.core.io.net.exec.ExecUtil.executeCommandLine(ExecUtil.java:59) [bundleFile:?]
	at org.openhab.core.model.script.actions.Exec.executeCommandLine(Exec.java:40) [bundleFile:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1176) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1151) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1137) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1082) [bundleFile:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:992) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:955) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:236) [bundleFile:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:216) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:459) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:240) [bundleFile:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:216) [bundleFile:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:202) [bundleFile:?]
	at org.openhab.core.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:80) [bundleFile:?]
	at org.openhab.core.model.script.runtime.internal.engine.DSLScriptEngine.eval(DSLScriptEngine.java:125) [bundleFile:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) [bundleFile:?]
	at java.util.Optional.ifPresent(Optional.java:183) [?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1179) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:987) [bundleFile:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:89) [bundleFile:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.io.IOException: error=2, Adresár alebo súbor neexistuje
	at java.lang.ProcessImpl.forkAndExec(Native Method) ~[?:?]
	at java.lang.ProcessImpl.<init>(ProcessImpl.java:340) ~[?:?]
	at java.lang.ProcessImpl.start(ProcessImpl.java:271) ~[?:?]
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1107) ~[?:?]
	... 35 more
2021-03-22 21:36:33.871 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'zvoncek-fotka-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.binding.mail.internal.action.SendMailActions.sendMail(org.openhab.core.thing.binding.ThingActions,java.lang.String,java.lang.String,java.lang.String) on instance: null in zvoncek-fotka

What I am doing wrong?

how the system knows to execute the command with su rules? why the curl -m 10 -o /home/openhabian/webcam/webcapture.jpg file or dir does not exists, when I see it - downloaded it manually?


It doesn’t and it isn’t. You didn’t have su or sudo as part of the command.

Because that’s the command it tried to run. That’s the string you built as your command.

However, as was announced in the OH 3 release notes and is documented at Actions | openHAB, executeCommandLine has changed in OH 3.

First of all, you should add a Duration as the first argument which will cause it to wait for the curl to complete. That way you don’t need the sleep and the email can be sent as soon as possible.

Then the command and it’s arguments are separate arguments to executeCommandLine. Do not concatenate everything into one long String like you are doing.

executeCommandLine(Duration.ofSeconds(3), "curl", "-m", "10",  "-o", outputFile, url)

Thanks Rich,
I have tried to integrate your command,
executeCommandLine(Duration.ofSeconds(3), "curl", "-m", "10", "-o", outputFile, url)
but now i got this:

2021-03-23 20:10:24.889 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'zvoncek-fotka-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.binding.mail.internal.action.SendMailActions.sendMail(org.openhab.core.thing.binding.ThingActions,java.lang.String,java.lang.String,java.lang.String) on instance: null in zvoncek-fotka

when I run curl -m 10 -o /home/openhabian/webcam/webcapture.jpg manually, it says Access denied,
when I run the same with sudo, it saves the webcapture.jpg, but the command continues:

openhabian@openHABianDevice:~/webcam $ sudo curl -m 10 -o /home/openhabian/webcam/webcapture.jpg
[2] 30998
[3] 30999
[4] 31000
[3]   Hotovo                  channel=1
openhabian@openHABianDevice:~/webcam $   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    28    0    28    0     0    280      0 --:--:-- --:--:-- --:--:--   280

Something is bad, but I am not able to understand what…

The error clearly states the problem is with the sendMail action. It is likely that you’ve not moved to the Mail binding as I don’t see where the mail action is imported. The Mail Action is no longer supported.

You probably need to give permission for the user running the curl command to write to the destination folder.

That’s the normal output from curl. There is an option to suppress that output.

Great, now I see that the webcapture.jpg is creating!

This is my Mail binding:

UID: mail:smtp:ba467f4d3e
label: SMTP Server
thingTypeUID: mail:smtp
  hostname: smtp.gmail.com
  security: SSL
  password: passwd
  sender: aaa@gmail.com
  port: "587"
  username: aaa@gmail.com

Should be correct, I hope?

The link in the sendMail command is correct. What else could go wrong then?

The call too the sendMail command issued not. Review the binding docs again. You have to pull in the action using the Thing ID. That doesn’t appear in your rule anywhere.

1 Like

Thanks, finally it works. I have added 1 line of code, removed the port from Item definition, and switched off google security. And before I changed the owner/rights to access to the outputFile folder.

Now the Rule looks like this:

rule "zvoncek-fotka"
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered DOUBLE_PRESSED or
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered SHORT_PRESSED or
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered LONG_PRESSED
    logInfo("zvoncek-fotka.rules", "Someone pressed the Terasa Ring button")
    val url = ""
    val outputFile = "/home/openhabian/webcam/webcapture.jpg"
    executeCommandLine(Duration.ofSeconds(3), "curl", "-m", "10",  "-o", outputFile, url)
    val mailActions = getActions("mail","mail:smtp:ba467f4d3e") //Thing - smtp server identifier
    mailActions.sendMail("email@gmail.com", "Zvoncek Terasa", "Aha, kto stlacil zvoncek na terase", "file:///" + outputFile)

When you get sick of all the emails, check out replacing it with:

Pushover: Can put a GIF that moves up on your mobile screen that you don’t need to unlock the phone to see and makes a different noise to an incoming email. Auto keeps the last X messages so no need to do clean up. Works excellent with the ipCamera binding that can create the GIF for you and it allows you to rewind footage from before the doorbell was pushed so you see the person walk up to the door.

Telegram: Allows you to do something similar to the above but supports MP4 moving video which is smoother and has audio which I find best for my front door as you get to hear the surprised person when they hear the door bell sound inside the house before they push the button.

1 Like

Thanks for the tip, seems to be better through Telegram :slight_smile:
Is the RPi4B capable to do operations with mp4?

here I am receiving the gif through telegram :smiley:

rule "zvoncekTerasa-gif-telegram.vytvor_gif"
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered DOUBLE_PRESSED or
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered SHORT_PRESSED or
    Channel "mihome:sensor_switch:286c0785f092:158d000129909b:button" triggered LONG_PRESSED or
    Channel "mihome:sensor_switch:286c0785f092:158d0001243994:button" triggered SHORT_PRESSED
    //Start creating the GIF
    getActions("ipcamera", "ipcamera:onvif:34aaa0d217").recordGIF("ipcamera",5)
    //Cast a doorbell sound using the Chromecast binding.

rule "zvoncekTerasa-gif-telegram.posli_gif_na_telegram"
    Item Doorbell_recordingGif changed to 0
    val telegramAction = getActions("telegram","telegram:telegramBot:e13f59dd1f")
    telegramAction.sendTelegramAnimation("file:///etc/openhab/html/camera1/ipcamera.gif", "Niekto zvonĂ­ na terase")

I prefer pushover except if you want audio or to offer buttons that can perform actions then telegram is better. Pushover is very handy how it will show you the gif moving whilst your phone is still locked. I sit my phone in front when I work and I don’t need to touch the phone to see.

They both are good and better then email.

Yes a pi can do h264 as the camera is already in that format it actually is easier then Creating the gif.

1 Like

I changed it to MP4. The message comes faster, as there is probably no convertion needed. The message attachment size is the same, cca 1MB/5s.
The previous gif has sending it as ipcamera.gif.mp4 and therefore i was not able to play it in external player. MP4 solved it too.

I already got the Telegram messaging, I will probably use it for later interactions.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.