Light switches with command line using python scripts and serial interface

Hello,

I am trying to configure some light switches using the command line and python scripts, but I am missing something, because the commands within HA are not working.
I am running HassOS 2.12 on a RPi3.

So far in the configuration.yaml file I have created a light switch (ON/OFF) as command line:

switch:
  - platform: command_line
    switches:
      kitchen_light_1:
        command_on: "/usr/bin/sudo /usr/bin/python3 /root/config/switchLight_Kitchen_1_On.py"
        command_off: "/usr/bin/sudo /usr/bin/python3 /root/config/switchLight_Kitchen_1_Off.py"

and I have created the scripts to switch the light (e.g. ON):

#!/usr/bin/env python3
import serial
ser = serial.Serial(port='/dev/ttyACM0', baudrate=9600, bytesize=8, parity='N', stopbits=1)
ser.write(('is000FFF000FFF'+'\n').encode())
ser.close()

I have then created a switch card using the switch defined above, but when triggered I always get an error: Command failed: /usr/bin/sudo /usr/bin/python3 /root/config/switchLight_Kitchen_1_On.py

If I go to the Terminal (I use the add-on “SSH & Web Terminal”) and I run the same py script the switch is working and the light is going on.

The script is using pyserial (“import serial” is used in the python script).
I had initially installed the library using the command “pip3 install pyserial”.

I have thought of two possible causes:

  • the scripts cannot be run by the HA user, but I have checked the permissions and it looks OK
  • the serial library is not seen by HA when launching the commands, although it is visible in the Terminal

In the second case, how can I check whether HA can access the pyserial library?
I could not find any add-on with such library online.

Thanks for helping.

Hello,

I could not find a solution to the issue using Python and finally I used a direct command.

In case someone has a similar issue, here is what I did:
In the configuration file I added the following (for each switch I have)…

switch:
  - platform: command_line
    switches:
      kitchen_light_1:
        command_on: "echo 'is000FFF000FFF' > /dev/ttyACM0"
        command_off: "echo 'is000FFF000FF0' > /dev/ttyACM0"

Then I used the switch light card with each switch.
Not an elegant way, but it is working (also after a reboot).

Hi,

Could you telle me more about those lights?
Are they plcbus?

Hi titismoke,
apologies for the delay, I somehow missed your post.

The lights are just normal lights, but I am using power switches from Intertechno to switch them on/off). E.g. Intertechno ITLR-3500, CMR-1000, ITL-230, etc.
It is a simple and economic system working on 433 MHz.

No problems :slight_smile:
I found it useful to made my plcbus almost nearly works. For the moment only the commands are ok but it put me on the right way :wink:

Happy to help! :wink:

Thanks for sharing the solution very helpful.

1 Like