I want that a switch executes a python script that is stored in my HASS folder (All in one installation)
Here is my switch configuration:
- platform: command_switch
switches:
autoremote:
command_on: python ‘[/home/hass/.homeassistant/switchon.py]’
command_off: python ‘[/home/hass/.homeassistant/switchoff.py]’
If i manually click on the scripts it runs bit the switch doesnt work.
Any ideas?
Your code contains a few errors. Try this:
switch:
- platform: command_line
switches:
autoremote:
command_on: 'python3 /home/hass/.homeassistant/switchon.py'
command_off: 'python3 /home/hass/.homeassistant/switchoff.py'
If you have your switches in a seperate file (which you include in your configuration.yaml file), then drop the first line (switch:
).
1 Like
Thx for the help, it worked!!!
1 Like
And if i want to call this script directly from an automation how should i write the action?
trigger:
platform: state
entity_id: binary_sensor.aeotec_dsb29_doorwindow_sensor_2nd_edition_sensor_3
state: ‘on’
action:
service: ???
entity_id: ???
data: ???
You would have to use a shell command, for example:
shell_command:
autoremote_on: 'python3 /home/hass/.homeassistant/switchon.py'
autoremote_off: 'python3 /home/hass/.homeassistant/switchoff.py'
With these shell commands, your automation action would simply be:
action:
service: shell_command.autoremote_on
or
action:
service: shell_command.autoremote_off
Or you could just switch on/off the switch you defined above to trigger the script:
action:
service: switch.turn_on
entity_id: switch.autoremote
and
action:
service: switch.turn_off
entity_id: switch.autoremote
4 Likes
Its an old post, but I had the same problem and got here, so I’ll just post my solution here for the next one to find it.
when you run python directly from bash in SSH, its not the same version of Python as the one running in the Homeassistant virtual enviroment.
try running the script from the homeassistant user:
sudo su -s /bin/bash homeassistant
python3 /home/hass/.homeassistant/switchon.py
In my case, I was using the serial module in my script, and that was not installed on the python version in the virtual enviroment. runnin pip3 install pyserial from the homeassistant user solved the problem.