How to create a card with buttons that execute/trigger URLs?

I have some EventGhost webserver URLs that execute various tasks such as:

http://myurl.com:8090/?PortableAC-HumidityMode
http://myurl.com:8090/?PortableAC-QuietMode
http://myurl.com:8090/?TV-Xbox-Source
http://myurl.com:8090/?TV-HTPC-Source

What’s the easiest way to create a Home Assistant card with buttons that execute these URLs without actually trying to display the webpage? …just quietly executes the URL link.


e.g.
rest_command:
  ac_humidity_mode:
    url: http://myurl.com:8090/
    payload: '?PortableAC-HumidityMode'

Call the command from the button tap action.

Thanks Tom. I really appreciate all your help. But could you also show the code to add a respective button for that with a respective button icon?

I tried the below code, but it’s obviously wrong:

type: button
name: Portable AC - Humidity Mode
rest_command:
  ac_humidity_mode:
    url: http://myurl.com:8090/
    payload: '?PortableAC-HumidityMode'

The rest command config I posted goes in your configuration.yaml file.

Do a config check, then restart if it passes then use Lovelace to add a button:

The tap action will be to call a service. The service will be your rest command.

Is this correct?

configuration.yaml

rest_command:
  service_name: rest_command.ac_humidity_mode
  ac_humidity_mode:
    url: http://myurl.com:8090/
    payload: '?PortableAC-HumidityMode'

Card:

type: button
name: AC Humidity Mode
tap_action:
  action: call-service
  service: ac_humidity_mode

No the service is not correct. Look in developer tools / services for the command name. It will probably be:

service: command.ac_humidity_mode

I’m doing something wrong for sure. I looked under developer tools /service… the only thing I could find related to the service is called, rest_command.ac_humidity_mode

However, when I have the below settings, if I click on the card, nothing happens. I can make it happen if I just put the url in my web browser directly.

rest_command:
  ac_humidity_mode:
    url: http://myurl.com:8090/
    payload: '?PortableAC-HumidityMode'
name: AC Humidity Mode
tap_action:
  action: call-service
  service: rest_command.ac_humidity_mode
type: button

I don’t get any errors in the config or the card… however, nothing happens when I use the mouse to click on the card I created. I dont see an button in the card either. Just a card. The below command DOES work in the web browser though:

http://myurl.com:8090/?PortableAC-HumidityMode

Does it work when you call the service from the developer tools / services menu?

If not, try:

rest_command:
  ac_humidity_mode:
    url: 'http://myurl.com:8090/?PortableAC-HumidityMode'

If it still doesn’t work, try this instead of the rest command (config yaml again):

shell_command:
  ac_humidity_mode: 'curl -k "http://myurl.com:8090/?PortableAC-HumidityMode"'

This will change the name of the service to shell_command.ac_humidity_mode

Thanks. No, it doesn’t work when I execute the service. Okay… Ill try the other ones.

Thank you so much ! This worked:

rest_command:
  ac_humidity_mode:
    url: 'http://myurl.com:8090/?PortableAC-HumidityMode'

I was even able to specify an icon for this service. Just one more thing… could you give me an example card code if I have another button with a slightly different service name to be included in the same card? I tried and tried, but couldn’t figure it out.
I basically want to have a card that has multiple items like the example below:

I was just writing a reply before you deleted your post. Did you get it working?

The version you had without the hyphens looked correct to me.

Hey Tom. I really appreciate all your help. I was able to figure what I did wrong. I was using hyphens in the additional service names I had added instead of underscore. As soon as I switched to underscore and rebooted all the services were available to me. I deleted my post because I didn’t want to take up your time unless I absolutely had to. It’s all working thanks to you.

1 Like

Yeah don’t use hyphens in names / entity_ids.

Glad you got it sorted.

I was doing a bit of reading about restful commands and see why my original idea didn’t work. Payloads aren’t just appended to the URL in restful, they’re actually passed to whatever api is listening on the resource to do things like update delete or get values.

So what we ended up with is a bit of a hack. We’re actually asking an api (that doesn’t exist) to send (get) info from the URL resource. It works but strictly speaking the shell_command I posted above would be better suited in this case.

Switched everything to use shell_command:. The good news is now I know how to use both. I’ll use rest commands if I ever need to do real GET or POST REST API commands.

2 Likes

I tried both ways, does not seem work. Please help on this code:

rest_command:
iroff:
url: ‘ttp://x.x.x.x/web/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=close’

Log:
2021-01-21 11:10:29 WARNING (MainThread) [homeassistant.components.rest_command] Error. Url: http://x.x.x.x/web/cgi-bin/param.cgi?cmd=setinfrared&-infraredstat=close. Status code 401. Payload: None

If i add username and password to above rest url or separate username: password: it does not work either.

then for shell script:

shell_command:
shelliroff: ‘curl -k “ttp://x.x.x.x/web/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=close”’

Log:
Error running command: curl - k “ttp://x.x.x.x/web/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=close”`, return code: 2

When I post the url in my webbrowser the it works.

I had to leave the h out of http, can not post more then 2 links in a post as new user.

Hi Pita, the log shows that you’re getting an “Unauthorized” response aka 401 response. It’s possible that endpoint may only allow supported web browser user-agents only. Whatever the actual reason, it’s the endpoint that’s not allowing Home Assistant to make the REST calls.

I did exactly the same thing, but I can’t find where the button is. I need this button to make an automation.

No you don’t. You only create a button in your dashboard to call the rest command manually, when someone clicks the button.

To use the rest command in an automation you just use the service. No button required.

action:
  - service: rest_command.ac_humidity_mode