Automation is triggered but python script does not run

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 ?

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.

It runs fine from the cli when I:

  1. sudo su -s /bin/bash hass
  2. activate the virtualenv
  3. python3 /home/hass/.homeassistant/mindergas.py

It just doesn’t run when triggered by the automation in HA.

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.

Thanks ! It’s working now.

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)

See this topic: Component rest_command, add header information and forget about the script.

Thanks!
I will look at it!