How to send a signal (not the messaging thing) into home assistant

Hi, I would like a completely random machine to send a signal to HASS. For example - when machine is going for shutdown, I would like to set a binary value somewhere in hass, to make hass aware of this fact so in conjunction with ping and power management it can take appropriate action. Same goes for reboot - but then, I would like to set different variable, so hass will not get trigger happy with forcing reboot of the machine.
I know how to use systemd / chron, so I’m not asking how to automate it on that arbitrary machine, but I’m asking about what interfaces ara available to “let hass know that something is happening from outside of it’s beautiful walled of garden”

REST API or Webhooks → both ways are by making HTTP request to the HA (via Curl etc.)

You can also publish some kind of MQTT “last will and testament”, whether an actual one (see, e.g., What is MQTT Last Will and Testament (LWT)? – MQTT Essentials: Part 9) or a custom one.

Thanks guys for the advice so far. Any other ways ? preferably stuff that I can limit just to single variable, rather than opening up the whole HASS to the outside world (like webhooks) ?

Maybe You can use file-sensor
File - Home Assistant.

You can have several file-sensors

i.e when machine-1 going for shutdown, send " One Going Down"

Random usually implies opening the the outside world.
If you use a cloud MQTT broker like HiveMQ, you can keep you HA “closed”.

key here was “whole hass”. So I’ve done some digging and I found out that webhooks, alto you can put some data into curl request, it get’s ignore so nobody can actually access other entities inside of hass, even if they magically find out webhook ID. (well please tell me if I’m dumb in my assumption)

Now, I’ve got webhooks setup and I’ve got a proper automation that when test equipment goes down for shutdown, the power everything else get automatically powered down after some timeout (hence I wanted some notification from test rig so simple crash or unplugging won’t just power down everything). My concern is that hass sits behind a reverse proxy, so it physically doesn’t know whenever request is local or from the evil world.

Again, if my assumption that webhook DOESN’T allow setting random values is wrong - do tell me.

not bothered to read my previous response but thanks guys again for all the suggestions :smiley: (and double thanks won’t hurt anyone)

Point is that HA webhooks use the the same “http server”, i.e. port, as the main HA webui.

So if you open your HA to webhooks from the external world, you’re actually opening your whole HA to the external world.

If you can do, e.g. curl -X POST -d '{"key": "data"}' http://<your external ip>:8123/api/webhook/foobar, then opening http://<your external ip>:8123 in a browser will lead to the HA Ui.

Sooooooo, alto the webhook is only used as a trigger, I can still use the same webhook to:

curl -X POST -d ‘{“input_boolean.parents_bathroom_auto_dehumidification_enabled”: “true”}’ https://my_magical_address_with_magical_webhook

I might be dumb but I tested that and it would not set - might be that I messed up the json and it really allow to set some random variables ?

Once again, there a single host:port for webui, webhook, api, whatever.
If you can use one, you can use the others. Paths will differ, though, so you might do filtering in a reverse proxy, if you understand what that mean.

That’s just a random “Hail Mary” attempt, right? :wink:
You have to use service calls through the api

How else do you do testing :smiley: but to be serious, it’s hard to test something you don’t fully know all it’s caveats. So I’m just lookign for confirmation whenever webhooks should be safe if used as a trigger for part of automation, OR can those be exploited for setting unforseed variables.
I think you’re raising a valid point about rest api, thou I don’t use restfull requests at the moment, just webhook for trigger due to simplicity.

OK, so … if somebody is looking for this, I’ll drop how I’ve done it so somebody would be able to replicate the stuff for their own project:

  1. on HASS, create a toggle variable (a binary variable)
  2. create new automation and search for webhook trigger.
  3. on executing set the variable to “true”
  4. create an automation that after that variable was set for a given time to “true” (10 minutes for me) it will be automatically set to false
  5. on machine in question create a new service with following content:

cat /etc/systemd/system/shutdown-webhook.service

Description=shutdown webhook trigger
DefaultDependencies=no - depending on your setup you MAY or MAY_NOT need this

ExecStart=curl -X POST -k https://address_for_your_magical_webhook


after which, in a console as a root (or with sudo obviously):

systemctl daemon-reload
systemctl enable shutdown-webhook.service 
  1. create your self a ping - and manually set it to refresh every 5 seconds
  2. create an automation where if ping switched from “connected” to “any” for longer than (in my case) 5 minutes, and your variable is true - switch the power socket OFF, and just to be on the safe side toggle the variable to “false”

Some may ask - why so much fuffing about ?! well, that machine is used for testing and sometimes I switch it on remotely. I’ve got a simple zigbee power socket, and machine is set to always power up when mains voltage shows up (settable in the BIOS), that automation adds convenience that I don’t have to check whenever machine was put to OFF state before switching off the power / I can simply switch power ON and machine comes up.