Shell_command: pi versus HA

ok, I think I must be missing something obvious here.

I’m using hassbian and running a few python scripts by using the shell_command component, it uses three times the same python script but with a different argument coming from the userinterface.

shell_command:
set_temp_high: ‘python3 /home/homeassistant/.homeassistant/python_scripts/set_temperature.py -t {{ states.input_number.slider_day.state }}’
set_temp_low: ‘python3 /home/homeassistant/.homeassistant/python_scripts/set_temperature.py -t {{ states.input_number.slider_night.state }}’
set_temp_now: ‘python3 /home/homeassistant/.homeassistant/python_scripts/set_temperature.py -t {{ states.input_number.slider1.state }}’

This is all working fine (added the script as a reference below), however I wanted to add some robustness to make sure no multiple entries would be made at the same time to the communication bus of the heater.

Here I found the interesting python module ilock which seems to do what it should do (installed through ssh’ing to the pi and executing pip3 install ilock). i.e. When I start several times the same script from the terminal window it just waits till the other one is finished.
However when executing this from home assistant, it fails at the line from ilock import ILock, so it doesn’t look like ilock is known by home assistant.

It sounds like I’m missing something very fundamental here … , any guidance?

thanks in advance!

import subprocess
import time
import getopt
import sys
#from ilock import ILock

check if passed options are valid

try:
options, args = getopt.getopt(sys.argv[1:], ‘t:’,[‘temperature_setpoint=’])
print(options)
print(args)
except getopt.GetoptError:
print(“incorrect syntax”)
print(“usage: python3 set_temperature.py -t ”)
print(“default to 12 degrees”)
msg2=12
sys.exit(2)
for opt, value in options:
if opt in (‘-t’,‘-T’,‘–temperature_setpoint’):
msg2=value
print(“successful argument”)
print(msg2)

#with ILock(‘ebus’, timeout=200):
msg1="ebusctl write -c f37 Hc1DayTemp "
cp = subprocess.run([msg1+msg2],shell=True,stdout=subprocess.PIPE)

msg1="ebusctl write -c f37 Hc1NightTemp "
cp = subprocess.run([msg1+msg2],shell=True,stdout=subprocess.PIPE)

time.sleep(30)

#check if it is truly set
cp = subprocess.run([“ebusctl read Hc1DayTemp”],shell=True,stdout=subprocess.PIPE)
temp=cp.stdout
if int(float(temp[0:4]))!=int(float(msg2)):
# if not set correct
msg1="ebusctl write -c f37 Hc1DayTemp "
cp = subprocess.run([msg1+msg2],shell=True,stdout=subprocess.PIPE)

cp = subprocess.run([“ebusctl read Hc1NightTemp”],shell=True,stdout=subprocess.PIPE)
temp=cp.stdout
if int(float(temp[0:4]))!=int(float(msg2)):
# if not set correct
msg1="ebusctl write -c f37 Hc1NightTemp "
cp = subprocess.run([msg1+msg2],shell=True,stdout=subprocess.PIPE)
else:
print(“setting correct”)

If you’re running hassbian, HA runs in a python venv.
You have to activate this venv before you install modules with pip.
It’s the same like updating HA.

1 Like

Thank you for the suggestion, but doesn’t look like the solution to the problem.

I uncommented the import ilock line, python script runs fine from the (homeassistant) environment (see response below). It doesn’t work from home assistant.

 (homeassistant) homeassistant@hassbian:/home/pi $ python3 /home/homeassistant/.homeassistant/python_scripts/set_temperature.py -t 20.5
 [('-t', '20.5')]
 []
 successful argument
 20.5
 setting correct
 (homeassistant) homeassistant@hassbian:/home/pi $ which pip3
 /srv/homeassistant/bin/pip3
 (homeassistant) homeassistant@hassbian:/home/pi $ pip3 list | grep ilock
 ilock                   1.0.2     

calling the script as a service or through one of the automations in home assistant gives the following entry in the log.

Error running command: `python3 /home/homeassistant/.homeassistant/python_scripts/set_temperature.py -t {{ states.input_number.slider1.state }}`, return code: 1

NoneType: None

(uncommenting the import ilock line makes it work again.)

thanks

I think when you run a command through shell_command, it creates a new shell that does not invoke the virtual environment.

However, it does run as user homeassistant. Did you do your testing as user homeassistant, and if not did you use sudo to install ilock? If not, you are likely to have installed ilock locally to the user you tested with, rather than globally, so the homeassistant user does not have access to it.

1 Like

awesome, running pi@hassbian:~ $ sudo pip3 install ilock made it work !

good there are people who know what they’re talking about :slight_smile:

thanks!