Post data from PHP to Home Assistant via webhook

Hello,

I would like to send some data from my own PHP code on Debian cloud server to the Home Assistant (with supervisor) running on raspberry pi with Home Assistant OS.

The suitable option for me seems to be webhooks - update input_text object via webhook is more than enough for me, I can then create automations based on this input_text and my custom strings

I spend 48 hours searching and reading about it and yet I can’t figure out what I am doing wrong. Could you please look and my code? maybe someone here can help me :slight_smile: Do you know any troubleshooting steps/software I could try to identify what am I doing wrong?

Let me know if I should post more information here.
Thanks in advance! :hugs:

php file posting to webhook API - using curl

// where are we posting to?
$url = "https://%MYURL%/api/webhook/m6status";

// what post fields?
$fields = array(
   'field1' => "test1",
   'field2' => "test2",
   'text' => "axasxasx"
);

// build the urlencoded data
$postvars = http_build_query($fields);

// open connection
$ch = curl_init();

// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

// execute post
$result = curl_exec($ch);
echo $result; // I always got 1 - as successfull
// close connection
curl_close($ch);

configuration.yaml

input_text:
  mazdastatus:
    name: mazdastatus
    initial: nothing

automations.yaml

- id: 'xxxxxxxxxxxxxx'
  alias: 'webhook mazda status '
  description: ''
  trigger:
  - platform: webhook
    webhook_id: m6status
  condition: []
  action:
  - service: input_text.set_value
    data:
      value_template: '{{ trigger.data.text }}'
    entity_id: input_text.mazdastatus
  mode: single

errors from log:

While executing automation automation.webhook_mazda_status
14:49:14 – Automatizácia (ERROR) - správa sa prvýkrát vyskytla o 14:49:00 a opakuje sa 2 krát

webhook mazda status : Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data['value_template']
14:49:14 – Automatizácia (ERROR) - správa sa prvýkrát vyskytla o 14:49:00 a opakuje sa 2 krát
3 Likes

Thank you for the code, it helped me! I wanted to send a message to my phone based on the data input in the webhook. Did you resolved your question ?