How do I get the php file to trigger an item in openhab?
I have wrote a rule below but thought this doesnt achieve anything as it is polling from openhab instead of being a trigger from php?
My goal is to recieve instant notifications to openhab from Netatmo when motion is detected instead of polling.
rule "Extract Camera Notifications from php file"
when
Time cron "0 0/5 * ? * * *" //Every 5 minutes
then
//url complete and 5 seconds time out
var jsonString = sendHttpGetRequest("http://URL/netatmo_webhook.php$
CAMjson.postUpdate(transform("JSONPATH", "$.Body.Data.CAM.Value", jsonString))
end
Hereās the script I currently use to send webhook content to myopenhab :
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
/**
* Webhooks Endpoint Example.
* This script has to be hosted on a webserver in order to make it work.
* This endpoint should first be registered as webhook URI in your app settings on Netatmo Developer Platform (or registered using the API).
* If you don't known how to register a webhook, or simply need ore information please refer to documentation: https://dev.netatmo.com/doc/webhooks)
*/
//Get the post JSON sent by Netatmo servers
$jsonData = file_get_contents("php://input");
//Each time a webhook notification is sent, log it into a text file
if(!is_null($jsonData) && !empty($jsonData))
{
file_put_contents('/tmp/Webhooks_examples.txt', $jsonData. "\n", FILE_APPEND);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://myopenhab.org/rest/items/curlEntry/state');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_USERPWD, 'mycredential@mydomain.org' . ':' . 'mypassword');
$headers = array();
$headers[] = 'Content-Type: text/plain';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
?>
ā¦may not be very nice, Iām far from being a star in php, so search and copy/paste are my best friends
Then youāve got to create a String Item named curlEntry in your OH instance.
And this is the OH2.5, jython version of the entry handling :
@rule("Netatmo | webhook triggered")
@when("Item curlEntry received update")
def netatmo_events(event):
donnee = event.itemState.toString()
data = json.loads(donnee)
if "event_type" in data:
if data["event_type"] == "movement":
...
elif data["event_type"] == "person":
for person in data["persons"]:
if person["is_known"] != True:
....
else:
....
else:
notification(donnee)
elif "push_type" in data:
if data["push_type"] != "topology_changed":
notification(data["message"])
else:
notification(data["message"])
Iāve adapted the php script to send only certain variables over to openhab instead of the whole json. Mainly because I code php and not very familiar with jython etc. Means I donāt need to split it on openhab as a result.
As I donāt use 2.5 with jython Iāve setup a standard rule to trigger actions based off the cURL item state update.
Tested and working quicker but still a delay in motion to actions being taken. Iād say about 30 seconds delay. Whatās your response like?