[probably solved itself] Can't get command line switch to work

I am trying to get a Command Line Switch to work. My ultimate goal is to trigger a shell script via SSH on another server to do something.

I can use sensor to read something (e.g. uptime) from a server via SSH, so i got that going - had cost me already quite some time.

Now I am trying to get a button/switch working, but it won’t work right.

After a lot of back and forth without really understanding what I am doing, I got this

command_line:
  - switch:
      name: OnOffSwitch
      unique_id: switch.cli.OnOff
      command_on: "echo 1 > /config/servercontrol.status"
      command_off: "echo 0 > /config/servercontrol.status"
      command_state: "cat /config/servercontrol.status"
      value_template: '{{ value == "1" }}'

I could toggle the switch once and then it stopped working somehow.

My two (first) questions would be:

  1. Should this work in general or am I missing something? (Apart from the icon)

  2. Can someone explain the value_template to me? I read the documentation, but i still don’t understand how it works. Does it result to “ON/TRUE” when the condition (in my example value==1) evalutes TRUE? Or what is the logic?

Thank you!

It may only work on another server if you use SSH Keys for a switch. Sensor may work without keys.
I see your code is not on another device e.g. not [email protected], but on home assistant server. Not sure if I read you wrong.

Thanks for your response, i had not updated my post yet because i was still adapting my setup.

Yes, i am aware :slight_smile:

My code was a simplified version that would do something locally. My way of troubleshooting is usually to simply things and then go one step at a time.

I do not know what I did but now it works.

My (second, complex) switch looks now like this

  - switch:
      name: HeliosBoot
      unique_id: switch.cli.heliosboot
      command_on: "/config/./servercontrol.sh on"
      command_off: "/config/./servercontrol.sh off"
      command_state: "cat /config/servercontrol.status"
      value_template: '{{ value == "on" }}'
      icon: >
        {% if value == true %} mdi:toggle-switch
        {% else %} mdi:toggle-switch-off
        {% endif %}

servercontrol.sh contains the SSH commandline. I externalized the ssh commandline to a) be able to change it quicker, without having to reload the YAML all the time and b) add more commands for debugging.

servercontrol.sh is as follows, if anyone is interested:

#!/bin/bash
echo "`date` $1 requested" >> /config/servercontrol.log

case $1 in 
    on)
        echo "turn on..."
        ssh -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_d_on "" 2>&1 
        echo "`date` On issued" >> /config/servercontrol.log
        echo "on" > /config/servercontrol.status
    ;;
    off) 
        echo "turn off.."
        ssh -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_d_off "" 2>&1 
        echo "`date` Off issued" >> /config/servercontrol.log
        echo "off" > /config/servercontrol.status
    ;;
  *) 
    echo "no input"
    exit 1
    ;; 
esac
exit 0

(Additional info: i use two distinctive ssh-keys, the actual code is run on the second node and the key decides what command is run)

It seems to work now. I think the biggest change i did in order to get it working is to change to “single” line values.
Somewhere in the examples there have been something like this

...
      command_on: > 
        "/config/./servercontrol.sh on"
      command_off: > 
         "/config/./servercontrol.sh off"
      command_state: >
         "cat /config/servercontrol.status"
...

So, they used > in order to have “multiline”.

But i am not sure if that was the only change i did in 3 or so hours of trying to adapt things to make it work…