One-time button to execute command

I’ve created a button that, when pressed, executes a command (remote via ssh). I wanted it to be a one-time switch, so when pressed, immediately returning to the off state. What I’ve done is the following:

Create a switch in configuration.yaml:

- platform: command_line
  switches:
    kodi_reboot:
      command_on: ssh -i /config/ssh/id_rsa <IP Address> "systemctl restart kodi"

Then in scripts.yaml:

reboot_button:
  alias: Reboot Kodi
  sequence:
    - service: homeassistant.turn_on
      entity_id: switch.kodi_reboot
    - delay: 00:00:01
    - service: homeassistant.turn_off 
      entity_id: switch.kodi_reboot

And finally in ui-lovelace.yaml:

- type: "custom:button-card"
          entity: script.reboot_button
          name: Reboot Kodi
          icon: mdi:kodi
          tap_action:
            action: call-service
            service: script.turn_on
            service_data:
              entity_id: script.reboot_button

It work, but I can’t believe that this is the most efficient way to build such a button. Are there any shortcuts or better ways to this solution?

I don’t think you should need the homeassistant.turn_off call? And you should be able to call it directly, something like
service: script.reboot_button

For example

type: button
tap_action:
  action: call-service
  service: script.all_media_off
show_icon: true
show_name: true
name: Media Off

…which should simplify both the script and the lovelace portion. Not sure why you need a custom card?
There may be a better way of running a command line - not sure on that one.

Thanks Geoff!

I’ve now added a shell_command:

shell_command:
  reboot_pi_kodi: ssh -i /config/ssh/id_rsa <IP Address> reboot

And call this with that button you showed. Works like a charm and much simpler!

2 Likes

Sorry to revive an old thread, but could you please share your full configuration for the button that calls your shell command? I’m trying to do something similar for a PTZ Camera to move to a Preset and nothing happens when I click my button. I created the Shell Command in my configuration.yaml and it shows in the system when I am setting the button to Call a Service.

The Shell Command documentation explains how to add the various commands to the configuration.yaml but then falls short of telling you how to actually use them in a dashboard.

Thanks!

James, if your shell command works when you test it in the Services tool, you should only need to change the service specified in the button config to your shell command service.

type: button
tap_action:
  action: call-service
  service: shell_command.your_shell_command
show_icon: true
show_name: true
name: PTZ Cam Preset Return

I figured out the issue. It didn’t like the ‘!’ in the password I was passing in the URL.

I had to change the shell command quotations like:
from

'curl "http://<user:pass>@<IP_of_Camera>/web/cgi-bin/param...."'

to

"curl 'http://<user:pass>@<IP_of_Camera>/web/cgi-bin/param....'"