First contact

I am trying to run a python script to control customized window blinds.

Already tried using a switch,but not achieved(some help?)

I seems i have found a way, by creating an automation:

  • alias: ‘python script test’
    trigger:
    platform: state
    entity_id: input_slider.ac_temperature
    action:
    • service: python_script.hello_world
      data_template:
      “name”: ‘gerard’

The script looks like this (simple script for proof concept):
import datetime
with open(“resultatPython.txt”, ‘a’) as out:
out.write(datetime.datetime.now().str()+ ‘\n\n’)

But the file is not created at all, or script not executed:

But i can read:
2017-09-20 13:21:47 INFO (MainThread) [homeassistant.helpers.script] Script python script test: Executing step call service

How shell i proceed?

Thanks.

Hi @Garfius,

If you’ve provided a direct copy/paste of your code, you appear to have indentation issues
Also your data_template appears to be missing some chars:it should look like:

{
  "name": "you"
}

you have a combination or single and double quotes and you’re missing the curly braces
I’m not sure your python script looks ok either, see https://home-assistant.io/components/python_script/ for more help.
Also there are examples available on the community: https://community.home-assistant.io/c/projects/scripts

The script is ok, you may try it on python3, it writes actual date in resultatPython.txt.

Anyways, already tried the example from: https://home-assistant.io/components/python_script/
Triggering from:

  • alias: ‘python script test’
    trigger:
    platform: state
    entity_id: input_slider.ac_temperature
    action:
    • service: python_script.hello_world2
      data_template: {“name”: “Garfius”}

Can’t find any result at the hass stdOut or home-assistant.log, or any folder.

Any idea?

Hi @Garfius,

by “The script is ok” I guess you mean this part:

import datetime
with open(“resultatPython.txt”, ‘a’) as out:
out.write(datetime.datetime.now().str()+ ‘\n\n’)

I was not questioning that part but the HA automation part as it sounds like it does not get triggered at all.
You may want to use syntax highlighting to write your code as it still doesn’t look right, Yaml is VERY strict with identations so an extra space and it won’t work
(there is no indentation before platform: and entity_id: underneath the trigger: line

To see if the automation is triggered or not, you can either add it to the front end and click on it, it will show you when it was last triggered, or you can use the template tab un dev tools and enter this:
{{ states.automation.python_script_test.attributes.last_triggered }} and see if it gives you None (never ran) or an actual date.
you’d have to make sure you move the input_slider first to trigger the automation
Last thing I’m unsure of if the alias. I’d remove the single quotes and rename it into python_script_test

Let me know how it goes

:thinking: How i get to know which platform is needed to run a python script?, Also, where can i find a description of each available object at config?
Thanks

The template shows a date/time.

The scripts are located at:
Named:
/home/orangepi/.homeassistant/python_scripts/hello_world.py
/home/orangepi/.homeassistant/python_scripts/hello_world2.py

I run hass as root (to assure it can write to F.S.)

The actual configuration is:
automation:

  • alias: python_script_test
    trigger:
    platform: state
    entity_id: input_slider.ac_temperature
    action:
    • service: python_script.hello_world
      data_template: {“name”: “Garfius”}
  • alias: python_script_test-2
    trigger:
    platform: state
    entity_id: input_slider.ac_temperature
    action:
    • service: python_script.hello_world2
      data_template: {“name”: “Garfius”}

Any idea?

Also:
2017-09-21 13:52:36 WARNING (MainThread) [homeassistant.core] Unable to find service python_script/hello_world
2017-09-21 13:52:36 WARNING (MainThread) [homeassistant.core] Unable to find service python_script/hello_world2
?!¿
-rwxrwxrwx 1 orangepi orangepi 131 Sep 21 12:51 hello_world2.py
-rwxrwxrwx 1 orangepi orangepi 165 Sep 21 13:03 hello_world.py

Got it!, i have 0.46.1 , Python script:Introduced in release: 0.47

Thanks.

Now that could explain why :wink:
Let me know how it goes after the upgrade…

Good!
I’m just fighting with limitations.
It seems ‘include’ is not allowed, and subsequently i guess filesystem access is not possible.

I am working on an orangepi hardware, so, the access to gpio is done using a different object than raspberry pi, and is not available on python3: pyA20.gpio

So i am trying to run a python script to set the window blinds to a position set by a slider, but the parameter does not pass.

  executar_comanda2:
    friendly_name: 'botons executen python'
    command_off: /home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state | int}}
    command_on: '/home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py 2'

The hardcoded one works, the other one does not.

Any suggestion?

Have you tried putting single quotes around the first one too?

It seems Hass doesn’t process the brackets, thus the string after ‘:’ is directly sent to execution.

Already tried:
Single or double quoted everything, or no quotes:
Parameters:
0- /home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py
1- {{
2- states.input_slider.ac_temperature.state
3- }}

I am using:
switch:

  • platform: command_line

Already tried:

shell_command:
executa_python: /usr/bin/python /home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}
shell_command:
executa_python: /home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}
shell_command:
executa_python: ‘/home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}’

Is there a way or i must do th workarround using a bash script?

Thanks

(Please format your YAML correctly as explained in the large blue box at the top)

According to the documentation for the shell command, that does support templates. However, there the executa_python is the internal name for that command, so you need to name them all differently.

I know, there where 3 examples i tried separatedly

I know it can pass arguments, it works on bash scripts.
But it does not seems to work running python script, it doesn’t even run in any way, with or without parameters.

Seems the only way might be an sh script workarround, but NOT:
shell_command:
executa_python: ‘bash /home/orangepi/pythonBridge.sh /home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}’

Doesn’t work anyways, it doesn’t even try?¿ Is there a security

I just wanted to create a custom window blinds with positioning using a slider on a orangepi, but is it possible? or i need another software?

I’m getting lost.

If you don’t post correctly formatted YAML (again, see the big blue box) it’s hard for us to help. YAML requires correct formatting, including the number of spaces. Any mistake can be critical, and because you’re just pasting as text, it loses all that information.

I’d also check your logs, to see if there are error messages.

Oh, and your scripts do have the execute bit set (chmod a+rx /home/orangepi/pythonBridge.sh)?

Ok i get it working using an automation.

But also need to be able to run scripts (on and off) using a toggle, and i found this only work when i switch on, but doesn’t work when i switch off.

The part of the conf.

shell_command:
executa_python: ‘/home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}’
set_ac_to_slider: ‘/home/orangepi/cosa.sh {{ states.input_slider.ac_temperature.state }}’

switch:

  • platform: command_line
    switches:
    executar_comanda:
    friendly_name: ‘botons executen comanda’
    command_off: bash /home/orangepi/cosa.sh apagat
    command_on: bash /home/orangepi/cosa.sh enjegat
  • platform: template
    switches:
    executar_comanda2:
    value_template: “{{ is_state(‘switch.executar_comanda2’, ‘on’) }}”
    friendly_name: ‘botons executen python’
    turn_on:
    service: shell_command.executa_python
    entity_id: switch.bona
    turn_off:
    service: shell_command.executa_python
    entity_id: switch.dolenta

Sorry about the formatting, the web forum editor won’t let me publish straight from the code (the dash sign messes all off, publishes a dot and a ‘lf’ ), and erases most of the spaces, but trust me, the yaml is valid.

How can i get a button to trigger a script for on and also for off?

Thanks in advance.

It’s not hard - see the big blue box at the top that I’ve directed you to twice already, or if you’re in a hurry, just select the text you want to mark as code and push </> on the editing bar.

If you can do that, we can look at what you’re posting and possibly provide suggestions.

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 600
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Europe/Madrid


#-------------------------------------barres de la esquerra
# Enables support for tracking state changes over time.
history:

# View all events in a logbook
logbook:

# Enables the frontend (la fila automations de la esquerra)
frontend:

# Enables configuration UI (fila config de la esquerra) <-- restart recarrega config
config:

logger:
  default: info
#--------------------------------------------------------


http:
  # Uncomment this to add a password (recommended!)
  # api_password: PASSWORD
  # Uncomment this if you are using SSL or running in Docker etc
  # base_url: example.duckdns.org:8123
  
# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
#updater:

# Discover some devices automatically
#discovery:

# Show links to resources in log and frontend (el welcome de la principal)
#introduction:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Track the sun (la rodona de dalt que ensenya el sol)
sun:

# Weather Prediction (rodona temps de dalt)
sensor:
  platform: yr

# Text to speech
tts:
  platform: google

python_script:




input_slider:
  ac_temperature:
    name: Valor Temperatura
    initial: 5
    min: 0
    max: 10
    step: 1
    

shell_command:
  executa_python: '/home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}'
  executa_python2: '/home/orangepi/.homeassistant/python_scripts/fesAmbTemps.py {{ states.input_slider.ac_temperature.state }}'
  set_ac_to_slider: '/home/orangepi/cosa.sh {{ states.input_slider.ac_temperature.state }}'

switch:
  - platform: command_line
    switches:
      executar_comanda:
        friendly_name: 'botons executen comanda'
        command_off: bash /home/orangepi/cosa.sh apagat
        command_on: bash /home/orangepi/cosa.sh enjegat
  - platform: template
    switches:
      executar_comanda2:
        value_template: "{{ is_state('switch.executar_comanda2', 'on') }}"
        friendly_name: 'botons executen python'
        turn_off:
          - service: shell_command.executa_python2
            entity_id: switch.dolenta
          - service: variable.set_variable
            data:
              variable: test_timer
              value_template: 'on'
        turn_on:
          service: shell_command.executa_python
          entity_id: switch.bona

#        value_template: "{% if is_state('switch.executar_comanda2', 'on') %}on{% else %}off{% endif %}"
#        value_template: "{{ is_state('switch.executar_comanda2', 'on') }}"
#media_player:
#  - platform: cast

camera:
  - platform: generic
    name: Plaça major de Vic
    still_image_url: http://www.meteosona.cat/static/estacions/images/webcams/VicPlaca.jpg
  - platform: local_file
    name: Webcam USB
    file_path: /home/orangepi/.motion/lastpicture.jpg
  - platform: local_file
    name: Gràfica ping
    file_path: /home/orangepi/graph/resultat.png

automation:
  - alias: 'Valor de slider a lampara'
    trigger:
      platform: state
      entity_id: input_slider.ac_temperature
    action:
      service: shell_command.executa_python
  - alias: 'Valor de slider a shell'
    trigger:
      platform: state
      entity_id: input_slider.ac_temperature
    action:
      service: shell_command.set_ac_to_slider
  - alias: python_script_test
    trigger:
      platform: state
      entity_id: input_slider.ac_temperature
    action:
      - service: python_script.hello_world
        data_template:
          name: "Garfius"
          dades: '{{states.input_slider.ac_temperature.state | int}}'
          dades2: '{{states.input_slider.ac_temperature.state}}'
  - alias: python_script_test-2
    trigger:
      platform: state
      entity_id: input_slider.ac_temperature
    action:
      - service: python_script.hello_world2
        data_template: {"name": "Garfius"}


#group: !include groups.yaml
#automation: !include automations.yaml

Thank you

Back to the problem at hand, you said you want to be able to run the on and off scripts (command_on and command_off) by a toggle. Can you provide an image of what you’re referring to?

There must be a simple way to do simple things.

I just need a simple, non dependant, toggle switch able to fire a services( mostly scripts) when it’s turned on, and a different one when turned off. And of course i need the slider position value.

Can you provide an example?

Thanks.