Response of rest_command

Hi!

I have a script that runs a rest_command and then sends me a notification about it’s success. But obviously it will only fail if there’s a serious error in rest_command, and I’d be oblivious to other possible response-errors that it may have.

Is there a way to get response of the rest_command (post in my case) and then show it in notification? Or write it to disk into a file or whatever. With hass tools.

I suppose worst case scenario I can instead use an approach like:

  • Run external python script that POSTs to a website and writes response on disk into a txt file
  • Delay up to 10 seconds to give time for POST to complete (whatever timeout time is set)
  • Read the file and send it’s contents in notification

But it would be cool if I could avoid using python for this

Did you ever find a way to accomplish this?

Yes and no… I ended up using python scripts for this and running them with shell command actions.

can you share your code please. thinking about how i can output the response

Sure. Here’s how I’ve done it. The general idea is that python script is called as shell_command, it then does it’s thing and then reports back to Homeassistant by calling a Script through API and passing some variables to it.

import requests
import re

try:
    hash_re = r'<input type="hidden" name="hash" value="(?P<hash>.*)">'
    id_re = r'<input type="hidden" name="id" value="(?P<id>.*)">'
    message_re = r'<div class="alert alert-success" role="alert">\n(?P<message>.*)\n'

    url = "https://domain.com/signin.php"
    headers = {"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
                                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"}




    session = requests.Session()
    response = session.get(url).text
    print (response)
    hash_string = re.search(hash_re, response).groupdict().get("hash")
    id_string = re.search(id_re, response).groupdict().get("id")
    print (hash_string, id_string)


    data = {
            "email": "myemail",
            "id": id_string,
            "hash": hash_string
    }
    response = session.post(url, headers=headers, data=data).text


    message = re.search(message_re, response)
    if message is not None:
        message = message.groupdict().get("message")
        title = "Checked In!"
        message = message.strip()
    else:
        title = "Check In Error"
        message = "Could not find message in response."
except Exception as e:
    title = f"Check In Error!"
    message = str(e)


hass_token = r"HASS API TOKEN"
url = "http://IP:8123/api/services/script/turn_on"
headers = {
    "Authorization": f"Bearer {hass_token}",
    "content-type": "application/json",
}

response = requests.post(url, headers=headers, json={
    "entity_id": "script.customnotificationme",
    "variables": {"title": title, "message": message}
    }
)

And that script in Hass has 1 action:

service: notify.mobile_app_mi_9
data:
  title: '{{title}}'
  message: '{{message}}'