Sending custom events from webserver to HA

I have a HA running in the office, our company also has a webshop.

I would like to send an event from our webserver to my HA instance so i can announce something through the speakers.

The HA API is accessable from our webservers using a VPN to the office and i can add python modules on the webserver.

But i am wondering what kind of ‘event’ i should send. How should i ‘model’ a webshop sale in HA ?
Is it a device ? Entity ?

I cannot really get my head around that.

Those aren’t events and they can’t be created via the API, only by integrations.

Just create an event. Literally, HA has those and you can make them via the API. At that link find POST /API/events/<event type> to see how the create event API works. Then in an automation you can listen for it using an event trigger and pull info you passed in out of the trigger object to make your announcement.

Or alternatively just use a webhook. Same idea except even easier because now you don’t even need a long lived access token. You basically just have a custom API that allows you to trigger your automation and make an announcement.

As Mike commented above, just fire an event or use a webhook. Here is how to fire an event and send payload data with a python script.

import requests
import json

EVENT_API_URL = "http://xxx.xxx.xxx.xxx:8123/api/events/"
HA_TOKEN = "YOUR_LONG_LIVED_HA_TOKEN"
HEADERS = {'content-type': 'application/json','Authorization': 'Bearer {}'.format(HA_TOKEN)}

def fire_event(event_type, payload):
        payload_str = json.dumps(payload)
        requests.post(EVENT_API_URL + event_type, headers=HEADERS, data=payload_str)
  

fire_event("my_event", {"extra_data": "my_extra_data"})

2 Likes

Thank you Mike and Tomas. An event indeed sounds more logical.
So I can just type website_sale as my event type, without first having to register such a ‘new’ event type ?

Will try this. With the extra data i can even pass on what product was sold. Sweet

Thanks fo the code, here is the powershell version.

function send_ha_event {
	param($endpoint,$event_type,$ss_token,$data)
	
	$uri = $endpoint + $event_type
	$body = convertto-json -inputObject $data
	$_iwr = @{
		URI = $uri
		Method = 'POST'
		AllowUnencryptedAuthentication = $True
		Authentication = 'Bearer'
		Token = $ss_token
		ContentType = 'application/json'
		Body = $body
		EA = 'SilentlyContinue'
	}
	Invoke-WebRequest @_iwr
}


$ha_event_api = 'http://ha_host:8123/api/events/'
$token_path = "$($psScriptRoot)\ha_event.ss"
$event_type = 'ps_event'
$data = @{	mydata = 'some'
			param = 'thing' }

$_she = @{
	endpoint = $ha_event_api
	ss_token = Get-Content $token_path | ConvertTo-SecureString
}
send_ha_event @_she -event_type $event_type -data $data

for this to run, you will need to convert your ha token to securestring and save it to a file.
you ca do it like this:

$ss = ConvertTo-SecureString -String "your_ha_token" -AsPlainText -force
$ss | ConvertFrom-SecureString | Set-Content '.\ha_event.ss'