How do I call a c++ program that use sudo

Hi

I’m trying to control some 433mHz switches (Mercury 350.115).

After alot of trial and error I have manage to create a c++ program that I can control the switches through shh.

$ sudo /home/hass/./myMercurySwitch 2 on

This works fine.

Now I want to use this in home assistant.
In my configuration.yaml I have this:

switch:
  platform: command_line
  switches:
    window_light:
      command_on: '/home/hass/./myMercurySwitch 2 on'
      command_off: '/home/hass/./myMercurySwitch 2 off'

This gives me “Command failed” in the home assistants log.

I think it has to do with that HA don’t allow me to use sudo, or that user hass are not allowed to execute the file myMercurySwitch. Trying to execute the file as hass gives me the error “can’t open /dev/mem”.

What permissions do I need to change to get this to work?
Or is command_line not the way to do it?

Hey,

  1. make sure you can run sudo without password (see here: http://askubuntu.com/questions/334318/sudoers-file-enable-nopasswd-for-user-all-commands)

  2. add HA to sudo group.

Should be good to go.

~Cheers

1 Like

You can add the HA user to sudoers following the instructions in this thread:

Note that the HA user name may be hass or homeassistant depending on your installation method.

1 Like

Thanks

I added both ‘hass’ and ‘homeassistant’ to sudoers

$ sudo visudo

hass ALL=(ALL) NOPASSWD: ALL
homeassistant ALL=(ALL) NOPASSWD: ALL

Then I added 'sudo ’ to the configuration.yalm.

switch:
platform: command_line
switches:
window_light:
command_on: ‘sudo /home/hass/./myMercurySwitch 2 on’
command_off: ‘sudo /home/hass/./myMercurySwitch 2 off’

This works now. :smile:

Many thanks

1 Like