Command state issue

Hi

So I have set up my command line switch with command on and off specified. I also have the state command which gives me the state if I was to run it in terminal.

However when I insert the command state, it shows my switch as on whilst its off and it wouldnt turn off. I have also set the value template to 1, as what other HA users have done but no jouy

I was wondering if anyone could please help me?

Here is my configuration:

  - platform: command_line
    switches:
      display:
        command_on: "ssh [email protected] vcgencmd display_power 1"
        command_off: "ssh [email protected] vcgencmd display_power 0"
        command_state: "ssh [email protected] vcgencmd display_power"
        value_template: '{{ display_power == 1 }}'
        friendly_name: Display

Thank you.

What does the command return when the switch is on?
If it returns ‘1’, what about value_template: '{{ value == "1" }}' like in the examples?

It does return 1 and I tried what you suggested but it doesn’t work.

It remains off and doesn’t turn on. Well it turns on and back off.

What installation of HA are you running? (Link to tutorial)
If running in a venv, is it activated?
Anything in the logs?

I ended up having to write a bash file and then call this file from the command_line component. That way, I could pass the value back. Mine was being sent back as something that wasn’t exiting properly. So in my bash file I would review the result and then perform an exit 0 or 1 depending on the result. This allowed HA to handle it directly vs. having to use a value template.

Can you please share it?
Thanks

Running on docker my HA

I think thats the problem.
If you test your commands in a terminal, are you inside the ha docker?

Yeah inside ha docker

Hmm, then it should work in HA too.
Do the on and off commands work?
Nothing in the log?

Looking at my command_line switches with ssh, i have quoted the remote commands, so

command_on: "ssh [email protected] 'vcgencmd display_power 1'"
command_off: "ssh [email protected] 'vcgencmd display_power 0'"
command_state: "ssh [email protected] 'vcgencmd display_power'"
value_template: '{{ value == "1" }}'

if this doesn’t work, i’m out of ideas.

Sorry, I guess mine was in javascript and not a bash file, but I believe the concept would be the same thing (obviously different language). With a bash you’d want to determine your exit code based on the response from your command. This code calls a function within a nodejs module. The output of that function was in an array, so before returning the value I converted the array to a string as it returns the status of my lights in binary (it’s an old lighting program). This code sifts through the binary based on my original input parameters to return the status of the light in question. Then, based on the return value I exit with a 0 or a 1. That exit is handled by HA to display the status of the light. The exit code should be 0 for on or 1 (or other value) for off.

I’m not a programmer, but once I figured out the exit state it works flawlessly. I’m running hassbian and don’t know anything about docker so I can’t help you if it’s a docker issue.

var keypad = process.argv[2];
var button = process.argv[3];


var LiteTouch = require('litetouch');
var litetouch = LiteTouch.connect('192.xxx.x.xxx', xxxx1, function() {

});

litetouch.getLEDStates(keypad,function(err,callback) {

    var ledstatus = callback.substr(callback.length - button);
    var ledStateFinal = ledstatus.substr(0,1);

    if (err) {
	    console.log("ERROR ", err);
	    return;
    }

    else if (callback.length == 1 && callback.length < button) {
	    console.log(-1);
	    process.exit(-1);
    }	

    else if (callback.length < button) {
	    console.log(-1);
	    process.exit(-1);
    }
// Light is on
    else if (ledStateFinal == 1) {
	    console.log(0);
	    process.exit(0);
    }
    else
	    console.log(-1);
	    process.exit(-1);

});
1 Like

Hello. I know this is an old post, but thought I would share what I did to get this to work properly. In the value template, replace “value” with what is actually being reported with the call. In this case, the full result is “display_power=1” when on. So the YAML looks like this.

switch:
  - platform: command_line
    switches:
        pi3_display:
            command_on: ssh [email protected] 'vcgencmd display_power 1 2'
            command_off: ssh [email protected] 'vcgencmd display_power 0 2'
            command_state: ssh [email protected] 'vcgencmd display_power -1'
            value_template: '{{ value == "display_power=1" }}'
2 Likes

Hello. I am trying to configure the state and the icon of the below switch.
the on and off work well but for the rest I definetly havent done it correct! :slight_smile:
any suggestion?

- platform: command_line
  switches:
    energenie2:
      friendly_name: Led Strip
      command_on: /usr/bin/curl -s -d "pw=1" http://192.168.1.126/login.html && curl -s -d "cte2=1" http://192.168.1.126/ && curl -s -d http://192.168.1.126/login.html
      command_off: /usr/bin/curl -s -d "pw=1" http://192.168.1.126/login.html && curl -s -d "cte2=0" http://192.168.1.126/ && curl -s -d http://192.168.1.126/login.html
      value_template: '{{ value == "1" }}'
      command_state: "/usr/bin/curl -X GET http://192.168.1.126"
      icon_template: >
          {% if value == "1" %} mdi:toggle-switch
          {% else %} mdi:toggle-switch-off
          {% endif %}