Command_line switch state

Hi guys,
I have this command_line switch in my configuration.yaml:

 - switch:
    name: heater_switch
    unique_id: switch_heater_switch
    scan_interval: 5
    command_on: "/usr/bin/curl -X POST '' --data-urlencode 'value=1803'"
    command_off: "/usr/bin/curl -X POST '' --data-urlencode 'value=1802'"
    command_state: "/bin/bash /config/"
    value_template: "{{ value == '1' }}"

my script is like this:


status=$(curl -X GET | xmlstarlet sel -N x= -t -v //x:value -n)
if [ "$status" = "1803" ]; then
	echo "1"	
	echo "0"

Problem is, that command_state is not rendering correct switch state - it runs every 5 seconds but even if script returns 1, switch will not change its state.
I tried to remove all content except echo “1” from script to test if switch can react to script and it worked(also with echo “0”) and switch changed its state. Also I tried to run that curl via Terminal and it also worked.
Please help what I’m missing?
Thank you

value_template string (Optional)

If specified, command_state will ignore the result code of the command but the template evaluating to true will indicate the switch is on.

Can you please elaborate? It seems I do not get it. Thank you

Remove this line and it should work. If you use value_template then the result code of your script will be ignored. That’s documented here

Unless I’m missing something that is not going to help.

The result code of the script depends on the script completing successfully, not the state of the switch (the value returned by the script).

So the switch could be off and the script could still complete successfully giving the state as on.

1 Like

My understanding is that the problem here is that the state of the switch is wrong. I had the same problem when I used value_template because then {{ value == '1' }} is always false because the result of the command_state script is ignored and value seems to be never 1 in this case. When I deleted value_template the state was correct. Let’s see what Max87 says.


looks like you are right. Deleted value_template: “{{ value == ‘1’ }}” and the switch still not changing its state correctly.

Is there a way to console log value of value variable?
We tried to delete value_template, no luck. Also to retur 0/1 as integer, not string and evaluate it as string/int - no luck.
Strange is that if I remove that curl from string and just print “1”, it worked…

Hmm. Seems like the curl command used in the script is not executing properly. Is xmlstarlet a command that HA can find? Maybe you need to specifiy the full path?

When I try this command in Terminal on HA, it prints correct value, but in command_state it doesn’t.
I had to add xmlstarlet as a package

How did you install HA? Is it running as a docker Container?

Hello, it is running as HomeAssistant OS, on Raspbery Pi4

So that means that HA is running as a docker container. I assume you did not install xmlstarlet within the HA docker container but on the host OS. But from within a container you cannot directly execute host commands. Possible solutions are to use ssh or pipes. This is discussed here: