Command line switch command_state

Hi!

I can’t get the command_state in the command line switch to work.

switch:
  platform: command_line
  switches:
    test:
      command_on: './test.sh on'
      command_off: './test.sh off'
      command_state: './test.sh state' #will return 0 if on and 1 if off

The command_on and command_off both work as expected but not the command_state. I’ve tried hard coding return values but it doesn’t matter, and the logs says nothing.

I’ve tried both “echo 0” and “exit 0” in my bash script without success.

Please help :slight_smile:

exit 0 for on and -1 for off

e.g. an example of a command line switch status check that works for me…

#!/bin/bash

if [ $status = 1 ]; then
echo 1 > /scripts/conf/vera_device_$1
exit 0
else
echo 0 > /scripts/conf/vera_device_$1
exit -1
fi

1 Like

You could try adding the following to your command_line switch:

value_template: '{{ value == "0" }}'

When the value_template is true, the state of the switch will be on.

1 Like

been trying with all kinds of exit codes and value templates, but it doesn’t seem like the switch reading the result of my script at all.

Is there some way you can verify that home-assistant actually does what it’s supposed to do? Or is it possible to force fetch the command_state?

Things you could try:

  • Run your script from the command line (SSH) to make sure it works and you’re getting the expected output. Also make sure to run the script as the user hass/homeassistant, as that is exactly what Home Assistant will do.
  • Check the home-assistant.log file in your .homeassistant folder for errors.
  • Search your system’s syslog file for related messsages with a command like cat /var/log/syslog | grep test.sh or cat /var/log/syslog | grep switch.test or …

Finally solved it!

Been playing around with just about everything, so I’m not sure exactly what did what :slight_smile:

If not using value_template it works with 0 and 1, and if using value_template the script works when using “echo 1” instead of “exit 1” and {{ value == “1” }} instead of {{ return_ value == “1” }}.

Thanks for your help!

1 Like

thanks a lot @servous I had the same issue :slight_smile: