Run Python Script in Automation

Can’t make the appropriate switch to run in Automation depending on the state of some sensors.
Following examples from Community forum, made below config in configuration.yaml:

  - platform: command_line
    switches:
    intercom:
    swimpool_on: "python3 /config/python_scripts/swimpool1.py"
    swimpool_off: "python3 /config/python_scripts/swimpool2.py"

and getting as a result:

* Invalid config for [notify.command_line]: required key not provided @ data['command']. Got None. (See ?, line ?).
* Invalid config for [sensor.template]: [switches] is an invalid option for [sensor.template]. Check: sensor.template->switches. (See ?, line ?).
* Invalid config for [sensor.command_line]: [switches] is an invalid option for [sensor.command_line]. Check: sensor.command_line->switches. (See ?, line ?).
* Error loading /config/configuration.yaml: mapping values are not allowed here in "/config/configuration.yaml", line 268, column 16
* Invalid config for [switch.command_line]: [name] is an invalid option for [switch.command_line]. Check: switch.command_line->name. (See ?, line ?).

I have python_scripts normally working for sensors from “command_line”, but can’t configure switch properly. What is wrong?

Take a look at the docs.

Sir, I read that docs before posting my issue.
Here is the formatted exerpt from configuration.yaml, I corrected in accordance with documentation.
What is wrong?

switch:
  - platform: command_line
    switches:
      intercom:
        swimpool_on: "python3 /config/python_scripts/swimpool1.py"
        swimpool_off: "python3 /config/python_scripts/swimpool2.py"

getting

Logger: homeassistant.util.yaml.loader
Source: util/yaml/loader.py:63
First occurred: 12:14:04 (1 occurrences)
Last logged: 12:14:04

mapping values are not allowed here in "/config/configuration.yaml", line 268, column 16

and

Logger: homeassistant.components.hassio
Source: components/hassio/__init__.py:269
Integration: Hass.io ([documentation](https://www.home-assistant.io/integrations/hassio), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+hassio%22))
First occurred: 11:55:54 (10 occurrences)
Last logged: 12:32:07

* Invalid config for [sensor.command_line]: [switches] is an invalid option for [sensor.command_line]. Check: sensor.command_line->switches. (See ?, line ?).
* Error loading /config/configuration.yaml: mapping values are not allowed here in "/config/configuration.yaml", line 268, column 16
* Invalid config for [switch.command_line]: [name] is an invalid option for [switch.command_line]. Check: switch.command_line->name. (See ?, line ?).
* Invalid config for [switch.command_line]: expected dictionary for dictionary value @ data['switches']. Got None extra keys not allowed @ data['intercom']. Got None extra keys not allowed @ data['swimpool_off']. Got 'python3 /config/python_scripts/swimpool2.py' extra keys not allowed @ data['swimpool_on']. Got 'python3 /config/python_scripts/swimpool1.py'. (See ?, line ?).
* Invalid config for [switch.command_line]: [swimpool_on] is an invalid option for [switch.command_line]. Check: switch.command_line->switches->intercom->swimpool_on. (See ?, line ?).

I can’t find swimpool_on and swimpool_off options in the docs. :thinking:

3 Likes

As stated earlier it should be command_on and command_off
However, why not use the Python Script integration directly?

Python_scripts won’t work as in Hassio it has restrictions while executing import directives like import requests. Scripts work under command_line, but won’t work if it’s executed under switch. Tried this option first with no luck.
Anyway, thanks for the advice!