zhbjsh / homeassistant-ssh

Control and monitor devices in Home Assistant by executing commands via SSH
MIT License
50 stars 4 forks source link

Services binary switch does not work #13

Open viktorp75 opened 6 months ago

viktorp75 commented 6 months ago

I configured binary sensor switch to control services on my OpenWrt router, but I get only status of the service. Command "service @{id} stop/start" does not work.

My code in the sensor commands section is this ``

zhbjsh commented 3 months ago

Hi! Could you please post the output of your command? You can test it in Homeassistant -> Developer tools -> Actions, select SSH: Execute command and choose your device as target.

smithjw039 commented 1 week ago

how was this resolved? I have the same issue as stopping service requires interactive authentication

here are my results when run from debug

results:

smithjw039 commented 1 week ago

I got this to work by modifying the sudoers file to allow the homeassistant user i set up to start and stop the services I wanted without password by adding the following to the file. homeassistant ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop server.service homeassistant ALL=(ALL) NOPASSWD: /usr/bin/systemctl start server.service (both my services ended with server.service) I also had to change the syntax of the command_off and command_on parameters in the integration settting for the sensor to include sudo.

command_off: sudo systemctl stop @{id} command_on: sudo systemctl start @{id}

viktorp75 commented 1 week ago

I am sorry, but I have not noticed any replies till now.

For me it is still not working. I think the problem is that we do not know what @{id} is or it is not correctly defined. Otherwise the service would be shut down.

Developers tools show this:

results:
  - device_id: c552a9de3f20d4825ab3716f9ae0d13e
    device_name: Rpigateway
    success: true
    command: service | awk '/cron|samba4/ {print $1 "," $3}'
    stdout:
      - /etc/init.d/cron,running
      - /etc/init.d/samba4,running
    stderr: []
    code: 0

results:
  - device_id: c552a9de3f20d4825ab3716f9ae0d13e
    device_name: Rpigateway
    success: true
    command: service samba4 stop
    stdout: []
    stderr: []
    code: 0
zhbjsh commented 1 week ago

@viktorp75 The @{id} is the part on the left side of the comma in your stdout. If you want to make the dynamic switch work, you have to modify the command to output only cron,running and samba4,running without the /etc/init.d/ part.

Of course you can always create static switches to do the same thing, in your case that would probably look something like this:

- command: service | awk '/cron|samba4/ {print $3}'
  scan_interval: 300
  sensors:
    - type: binary
      name: Cron
      command_on: service cron start
      command_off: service cron stop
      payload_on: running
    - type: binary
      name: Samba 4
      command_on: service samba4 start
      command_off: service samba4 stop
      payload_on: running