Help with command_line switch

All, I am trying to configure a command_line switch to stop and start the Homebridge service. Starting and stopping works, however I cannot get a status to work. I have used the command_state below on a command line and it will return a value of 1 or 0. However, I can never get that to show in the HA gui. Any thoughts?

  - platform: command_line
    switches:
      homebridge_start_service:
        friendly_name: 'Homebridge Service'
        command_on: "sudo systemctl start homebridge.service"
        command_off: "sudo systemctl stop homebridge.service"
        command_state: '"sudo systemctl status homebridge.service --full | grep -oPc "Active: active""'
        value_template: '{{ value == "0" }}'

did you give your homeassistant user sudo rights? by default the HA user does not have sudo rights.

Yes. Did that. The switch itself actually works. It’s the getting the status piece that I’m having trouble with

Are you testing the command as your home assistant user in the virtual environment? Some commands will run for your general user but the Homeassistant user may not have the right permissions or other issues and the command cannot run when requested from the Homeassistant user or from the virtual environment (or both).

If your not sure provide the installer you used so I can give instructions.

I used the all in one installer. I have tried switching to that user but I can’t get anything to run with sudo since I don’t know that password. The switches did only start working when I added homeassistant (the user) to the sudo group though.

Thanks for your help. Below is the HA error.

17-02-03 16:04:08 ERROR (Thread-5) [homeassistant.components.switch.command_line] Command failed: sudo systemctl status homebridge.service --full | grep -oPc 'Active: active'
  - platform: command_line
    switches:
      homebridge_start_service:
        friendly_name: 'Homebridge Service'
        command_on: "sudo systemctl start homebridge.service"
        command_off: "sudo systemctl stop homebridge.service"
        command_state: "sudo systemctl status homebridge.service --full | grep -oPc 'Active: active'"
        value_template: '{{ value == "1" }}'

The above is working to set the status of my switch properly. I only get an error in the logs when the service is shutdown so that’s good progress :slight_smile:

1 Like

Any further progress on this or did you stick with the code from Feb 4?

I’m looking to do the exact same thing with my setup and this is a huuuuuuuge help!

Since you got it to work do you have an idea why it does not work for me.
I have similar issues described here and just can’t find the reason why my command_states are not working.

apologies for the delayed response. Yes the above code is what I have been using and it has been working well

Did you make sure that the home assistant user has the rights to run that command? I had to add mine to the sudo group to get this to work

Yep, I had to do the same thing in order for it to run. I’ve got it running successfully, only I keep getting errors if Homebridge service is not running (as you mentioned).

Any idea how to fix this and clean up our error logs?

Ah ok. I did change the default logger settings and I’m pretty sure I excluded this. Let me look copy that config over tomorrow

Ahhhhhh, good suggestion.

I was able to figure out how to exclude the logs using the code below (for those that are following this).

logger:
  default: warning
  logs:
    homeassistant.components.switch.command_line: critical
1 Like

Great! Glad it’s working

In my environment a have a camera connected to pi running volumio (debian) that is served via motion service. My hassbian sits on another pi, and task was to control camera with switch with correct status monitoring. Switch itself was running fine, (ssh to volumio and start or stop motion service) but had a trouble with getting right status. Solution was to write sh script, which was returning status as numeric value and pointing switch status to that

1 Like

For reference, I used systemctl is-active -q, and that works great. i.e. for kodi:

  - platform: command_line
    switches:
      kodi:
        command_on: sudo -u root systemctl start kodi.service
        command_off: sudo -u root systemctl stop kodi.service
        command_state: systemctl is-active kodi.service -q

with a corresponding entry in /etc/sudoers.d/020_kodi:

ALL     ALL=(root) NOPASSWD: /usr/bin/systemctl restart kodi.service
ALL     ALL=(root) NOPASSWD: /usr/bin/systemctl start kodi.service
ALL     ALL=(root) NOPASSWD: /usr/bin/systemctl stop kodi.service