Command_state for command_line switch returning incorrect value

Hello,

I’m configuring a switch and I have quite some problems to get the command_state to return the actual state of the switch.

Here is the configuration:

switch:
  - platform: command_line
    switches:
      audio_depot:
        friendly_name: Audio Dépôt
        command_on: echo "SPR 0 5 1\r" | nc 192.1.1.37 49152
        command_off: echo "SPR 0 5 0\r" | nc 192.1.1.37 49152
        command_state: echo "GPR 0 5\r" | nc 192.1.1.37 49152
        value_template: "{{value[-1]}}"

The on and off commands work fine, the device switches the audio on and off as expected.

But the command_state gives me problems:
It should return the following values:
“GPR OK\nPRM 0 8 1\n”
OR
“GPR OK\nPRM 0 8 0\n”
I tested this with a packet sender successfully. but Home assistant returns
“GPR OK PRM 0 8\r 1” or “GPR OK PRM 0 8\r 0”

Using a sensor, I can confirm getting this string back, so I wrote the value template as to get the last character to get the sate of the switch.
But it is always on ‘off’ status…

Am I missing something? If the command_state returns 0 or 1, isn’t it working?

Thanks,
Daniel

See the documentation for value_template. It needs to return true to indicate on.

So, try this:

value_template: "{{ value[-1] == '1' }}"

Thanks a lot, it works as expected!
I don’t know how I missed that.

1 Like