Create a trigger via http

Hi All,

I have a Synology running surveilance station that can run an action rule when motion is detected. This rule can be a Http GET or POST (REST command). I can’t figure out how to create a switch that listens to this command and thus can be controlled from the synology. I would like to have an automation rule started when a command is received from the Synology. Therfore i was thinking of a switch to be able to trigger on any change.
Can anyone help me?

Take a look at:

Hi Fabian,
Thanks for answering. I saw that one but could not figure out how to use it as trigger.
Do I miss something?

I think I was wrong, and your answer was correct. But I can’t figure out how a HTTP Binary Sensor should look like in the Configuration.yaml file. Could you give an example?

No need to setup a binary sensor in the configuration.yaml. The HTTP request is parsed and the sensor created.

1 Like

Do you know a hass sensor that can react on a simple GET command with params. I’am asking this because could not find a way to create an action rule in Surveillance station that sends a POST command.

I found a way to translate a GET command from an action rule in Synology Surveillance Station into a POST command by running a little php scrip in synology’s web server. This scrip creates a sensor an then changes the state from OFF to ON and OFF again.
The get command in the action rules looks like:

http://SYNOLOGY-IP/home_assistant/hass.php?sensor=camera0&name=Camera%20Back
A sensor with the name “camera0” and de friendly name “Camera Back” will be created (if not allready done).
Then it will be toggle on and off again.

The PHP scrip running on the webserver

(be sure php 5.6 is anbled in de weberver)


<?php
// usage
// http://SYNOLOGY-IP/home_assistant/hass.php?sensor=camera0&name=Motion Camera 0
// Will create a binary_sensor with the name "camera0" and friendly name "Motion Camera 0"
// and toggles its state from off to on and off again
//
echo $_SERVER['HTTP_HOST'];
echo '<br>';
// Set variables from cmd parameters
$options = array(
  'sensor' => isset($_GET['sensor']) ? $_GET['sensor'] : false,
  'name' => isset($_GET['name']) ? $_GET['name'] : false,
);

// Remove empty values
$options = array_filter($options);

// Quit if not exactly 2 get values were found
if (count($options) != 2) {
  echo 'invalid options';
  die;
}

$headr = array();
$headr[]="x-ha-access: PASSWORD";
$headr[]="Content-Type: application/json";

// See https://home-assistant.io/components/binary_sensor.http/#examples
// for more information about binary_sensor

// Send POST ON command, this will create a sensor with then name "sensor" and friendly name "name" 
// and set the state to on

curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "http://$_SERVER[HTTP_HOST]:8123/api/states/binary_sensor.$options[sensor]",
  CURLOPT_POST => true,
  CURLOPT_HEADER => false,
  CURLOPT_HTTPHEADER => $headr,
  CURLOPT_POSTFIELDS => "{\"state\":\"on\", \"attributes\": {\"friendly_name\": \"$options[name]\"}}"
));
curl_exec($ch);
curl_close($ch);

echo '<br>';
sleep(10);

// Send POST OF command
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "http://$_SERVER[HTTP_HOST]:8123/api/states/binary_sensor.$options[sensor]",
  CURLOPT_POST => true,
  CURLOPT_HEADER => false,
  CURLOPT_HTTPHEADER => $headr,
  CURLOPT_POSTFIELDS => "{\"state\":\"off\", \"attributes\": {\"friendly_name\": \"$options[name]\"}}"
));
curl_exec($ch);
curl_close($ch);
echo '<br>';

?>

1 Like

Hi,
I have problems understanding what the POST commands should look like and I am not good at PHP. What does the resulting HTTP commands look like for On and Off state changes?