Automation is triggered but python script does not run

My automation:

  alias: 'Upload gas usage to'
    platform: time
    hours: 13
    minutes: 0
    seconds: 0
      service: shell_command.mindergas

The shell_command

mindergas: 'python3 /home/hass/.homeassistant/'

The script:

import requests
import json
import datetime
import homeassistant.remote as remote

mindergas = ""
mindergas_token = 'z25UYt-secret'
hass_ip = ''
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.timedelta (days=1)
    data = {'date': yesterday.strftime ("%Y%m%d"), 'reading':gas_usage.state}
    headers = {'Content-Type': 'application/json', 'AUTH-TOKEN': mindergas_token}
    r =, data=json.dumps(data), headers=headers)
    print (yesterday.strftime ("%Y%m%d"))
    print (data)
    print (r)
    print ('Ready')


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/

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/"

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/, return code: 2
08:07 /usr/lib/python3.6/site-packages/homeassistant/components/ (ERROR)

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

I will look at it!