HASSIO python scripts with imports

Ive been doing a lot of reading regarding running python scripts with imports and I thought I’d provide my simple work around in HASSIO. Whilst sensors work using the command_line sensor if you wanted to just run a script with a simple button press this is my 2 step method that works great for me.

I have a python script that will adjust the fan speed of my aircon using the command_line switch component… low, med and high. Here is the code in my configuration.yaml…

switch:
  platform: command_line
  switches:
    aircon_fan_low:
      command_on: "python3 /config/python_scripts/airconFanSpeed1.py"
      command_off: "python3 /config/python_scripts/airconFanSpeed1.py"
    aircon_fan_med:
      command_on: "python3 /config/python_scripts/airconFanSpeed2.py"
      command_off: "python3 /config/python_scripts/airconFanSpeed2.py"
    aircon_fan_high:
      command_on: "python3 /config/python_scripts/airconFanSpeed3.py"
      command_off: "python3 /config/python_scripts/airconFanSpeed3.py"

So as you can see here I have set the on and off command to be the same script, reason being I only plan to run these scripts as a simple button.

Now if you create a script to run this it will look something like this…

## Aircon Fan Speed ##
aircon_low:
  alias: Aircon Fan Low
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.aircon_fan_low
aircon_med:
  alias: Aircon Fan Med
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.aircon_fan_med
aircon_high:
  alias: Aircon Fan High
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.aircon_fan_high

You can see here I am just simply calling the switch.turn_on service because it doesn’t really matter if its on or off, its just to create a button. Then in home assistant it will look like this…

If you’re using HADashboard this method will then enable you to place simple buttons on your dashboard by using the scripts widget.

I know this isn’t perfect but it is a work around that I’ve been searching for and I know a lot of others are too so I hope this helps some people.

Interesting. Where did you put the imports and how did you get them there?