Commnad Line Switch State


I’m currently using HomeAssistant on a Windows laptop, controlling a few devices and a raspberry pi with sensors and relays connected amongst other things.

I have the command line switches setup like so.

- platform: command_line
      command_on: 'C:\Users\Dave\relay.bat on 25'
      command_off: 'C:\Users\Dave\relay.bat off 25'
      command_state: 'curl ""'
      value_template: '{{ value == "0" }}'
      friendly_name: Oscillating Fan
      command_on: 'C:\Users\Dave\relay.bat on 26'
      command_off: 'C:\Users\Dave\relay.bat off 26'
      command_state: 'curl ""'
      value_template: '{{ value == "0" }}'
      friendly_name: Light

My relay-status.php script to get the state returns a value of ‘1’ for on and ‘0’ for off, as in the documentation for command line switches (and I’ve set this to be updated the moment the switch is toggled as part of the ‘command_on’ and ‘command_off’ batch file (which is actually just ssh’ing into a rapsberry pi from the Windows HA box and running a python script).

My issue is that it always takes around 10-30 seconds after flipping the switch before the state is updated… this results in the switch being flipped back to the original state for those 10-30 seconds, then eventually going to the correct state and showing the right value.

I can manually check the relay-status.php script and it shows the right value the moment the switch is toggled, but HA doesn’t get it for a while.

Is this just the way it is? Is there any way to remedy this?

Edit: I’ve also tried without the ‘template_value’ line, as my command_state script returns the proper values anyway, but then it works even less (always reports as on/off no matter what I do to the switches)


I dont have a solution to your problem but it might be easier (and I know it is faster) to install HA on that pi, set up all your components in that instance and then make command line components on your windows machine that make API calls.

I have been thinking of installing HA on that Pi and just running it from there, I don’t even really need to use the Windows laptop at all, it was just where I started messing around with HomeAssistant before even setting up the Pi with sensors.

I already have everything I need (sensors, switches etc.) accessible on the Pi via very simple php scripts I’ve written and they respond almost instantly, except for the state of the relay from the command line switches.

Just seems like a lot of work completely moving a HA instance to another platform / device solely to get the command switch state to update correctly.

Agreed! Good luck!

I also had a similar issue with some command line switches.
I got around this by writing the state of the switch into a file when I turn on/off and the state check simply reads this file. I’ve then got a cronjob that updates all the states twice a minute to catch things which change state outside of HA and reflect them back in the HA UI

Yeah thanks that is what I already have. But the state doesn’t seem to be reflectcted in HA straight away.

What values do you have the state check returning? Are you using the value template on the state check?


ok, switch script exits a zero or 0 (zero = success) (1=fail), e.g.

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

Exactly the same with the status check script.
The trick for me was getting the switch as well as the status script to set an exit code of 0 or 1

And the YAML looks like

      command_on: '/bin/bash -c "/scripts/ 81 1 sensor"'
      command_off: '/bin/bash -c "/scripts/ 81 0 sensor"'
      command_state: '/bin/bash -c "/scripts/ 81 sensor"'

No value template stuff - controlled output within the shell scripts.

Thanks, the switch code you posted above shows an exit code of -1, I was going to try that but it just said in the documentation that on had to return 0.

I’ve since taken the advice of the first reply and moved my HASS instance to a Raspberry Pi B+. It’s not as quick as my old Windows laptop for sure, but everything just works (except for InfluxDB for some reason, HASS refuses to connect to it locally, I’ve had to leave that on the Windows PC along with the Grafana install).

I have Influxdb running on my Pi B+, so it is possible. It does seem to have a memory leak so I have to keep restarting it regularly

I do run grafana on my windows machines though, to limit the processing on the Pi.

I wouldn’t mind doing that with Grafana on the Windows machine, I just want InfluxDB local to the HA install so that if network goes down the data is still logged to InfluxDB.

It’s a weird one, I backed up the db and restored it to the new InfluxDB on the Pi, and Grafana on the Windows machine can connect to it, but HA just fails to connect to it and says there is something wrong with the config.