Using shell command to execute a script

Hey there,

I have my RPi running HA equipped with an active cooling fan with pwm-controll. To set the speed I downloaded and modified a script which works fine on it’s own. The script is in /usr/share/hassio/homeassistant and accepts a single argument. It works when called from a terminal connection to my pi. The problem comes with automating it with HA.
I added this to my config:

shell_command:
  pi_fan_pigpio_faster: "/usr/share/hassio/homeassistant/pi_fan_pigpio.py +5"
  pi_fan_pigpio_slower: "/usr/share/hassio/homeassistant/pi_fan_pigpio.py -3"

In my automation I can select the service Shell Command: pi_fan_pigpio_faster, but it does not affect the speed. Here my complete automation:

alias: Lüfter einstellen
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.processor_temperature
    above: '60'
  - platform: numeric_state
    entity_id: sensor.processor_temperature
    below: '55'
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.processor_temperature
            above: '63'
        sequence:
          - service: shell_command.pi_fan_pigpio_faster
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.processor_temperature
            below: '55'
        sequence:
          - service: shell_command.pi_fan_pigpio_slower
            data: {}
    default: []
mode: single

Is there someone who can tell me why there is nothing happening?

Best regards,
4nduril

Try it without quotes around the above and below values.
Does the shell_command work if you call it from Dev Tools/Services?

Hi @VDRainer ,
thanks for your reply. As I used the graphical interface to generate this automation I don’t think it will change anything, but still I will try.
The Dev Tools services call has the same effect: nothing happens. I can’t see any error message (where would an error be expected to show up?) and no change in the pwm setting.

edit: I found where there should be an error message an there is one…
it says return code 127, which indicates an unknown command according to my google search.
How is that possible when I can run it from an ssh connection?
Here is my script:

#!/usr/bin/python3

import pigpio
import sys

pi = pigpio.pi()
if int(float(sys.argv[1]))==0:
  signal = int( 200000 )
#  print("initialisieren")
else:
  signal = int(pi.get_PWM_dutycycle(18)) + 45000*int(float(sys.argv[1]))
#print(signal)
if signal >1000000:
  signal = 1000000
if signal < 100000:
  signal = 100000
#print(signal)
pi.hardware_PWM(18, 2500000, signal)

if not pi.connected: # exit script if no connection
  exit()

Which type of installation method are you running?
Errors should show up as a toast in Dev Tools/Services on call, or in HA log.

I’m using an installation with supervisor. As I edited above I found the error, but don’t know why.

Take a look here how you open a shell in the HA container to test your scripts.

Thank you very much for your help. I used the ssh web add-on according to your link to log into the HA docker container. There I found python3 installed in /usr/local/bin instead of /usr/bin as on the raspberry pi system. Therefore it could not properly run that python3 script.
I deleted my first 2 lines and changed the shell_command to include the call for python3. As the filesystem in the container is different I also changed to relativ path and ended up with "python3 ./pi_fan_pigpio.py +2" and that works perfect.

1 Like