piotr
(Peter)
December 26, 2016, 12:04pm
1
My automation:
alias: 'Upload gas usage to mindergas.nl'
trigger:
platform: time
hours: 13
minutes: 0
seconds: 0
action:
service: shell_command.mindergas
The shell_command
mindergas: 'python3 /home/hass/.homeassistant/mindergas.py'
The script:
#!/srv/hass/bin/python3
import requests
import json
import datetime
import homeassistant.remote as remote
mindergas = "http://www.mindergas.nl/api/gas_meter_readings"
mindergas_token = 'z25UYt-secret'
hass_ip = '127.0.0.1'
hass_password = ''
hass_port = '8123'
gas_sensor = 'sensor.gas_consumption'
api = remote.API(hass_ip,hass_password, hass_port)
gas_usage = remote.get_state(api, 'sensor.gas_consumption')
def post():
yesterday = datetime.date.today () - datetime.timedelta (days=1)
data = {'date': yesterday.strftime ("%Y%m%d"), 'reading':gas_usage.state}
headers = {'Content-Type': 'application/json', 'AUTH-TOKEN': mindergas_token}
r = requests.post(mindergas, data=json.dumps(data), headers=headers)
print (yesterday.strftime ("%Y%m%d"))
print (data)
print (r)
print ('Ready')
post()
The automation is triggered but the script does not run, what am I doing wrong ?
ih8gates
(Scott Reston)
December 26, 2016, 1:59pm
2
If you run this command from a command line, does it work?
I’d bet you have a permissions issue. Try switching to the hass user (assuming you installed with AIO):
sudo su -s /bin/bash hass
Then run that command. If it works, then you’re running it as Home Assistant does and we can diagnose from there. If it fails, it may expose an installation issue. All-in-one runs in a venv, so any required libraries need to be installed inside the venv.
piotr
(Peter)
December 27, 2016, 6:53am
3
It runs fine from the cli when I:
sudo su -s /bin/bash hass
activate the virtualenv
python3 /home/hass/.homeassistant/mindergas.py
It just doesn’t run when triggered by the automation in HA.
touliloup
(Romain)
December 28, 2016, 8:54am
4
Try to use double quotes instead of simple quote
mindergas: "python3 /home/hass/.homeassistant/mindergas.py"
Can you search for mindergas in your log and post the entry here. If the automation is triggered we should see an entry in the log.
piotr
(Peter)
December 28, 2016, 9:07am
5
Thanks ! It’s working now.
Jovink
(Jeroen)
April 26, 2018, 6:12am
6
I know it is a old topic but i want to use this script.
But if i run this script i get a error
Error running command: python3 /home/hass/.homeassistant/mindergas.py
, return code: 2
08:07 /usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py (ERROR)
piotr
(Peter)
April 26, 2018, 7:07am
7
Jovink
(Jeroen)
April 26, 2018, 5:04pm
8
Thanks!
I will look at it!