Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow iteration in python_script #8134

merged 2 commits into from Jun 21, 2017


None yet
4 participants
Copy link

commented Jun 21, 2017


This will allow us to iterate inside Python scripts and thus also use for-loops.

Example python_script

home = 0
for entity_id in hass.states.entity_ids('device_tracker'):
    state = hass.states.get(entity_id)
    if state.state == 'home':
        home = home + 1

hass.states.set('sensor.people_home', home, {
    'unit_of_measurement': 'people',
    'friendly_name': 'People home'


If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@pvizeli pvizeli merged commit 6398e92 into dev Jun 21, 2017

5 checks passed

cla-bot Everyone involved has signed the CLA
continuous-integration/travis-ci/pr The Travis CI build passed
continuous-integration/travis-ci/push The Travis CI build passed
coverage/coveralls Coverage remained the same at 93.652%
hound No violations found. Woof!

@lwis lwis deleted the python-script-allow-iter branch Jun 21, 2017

balloob added a commit that referenced this pull request Jun 21, 2017

Allow iteration in python_script (#8134)
* Allow iteration in python_script

* Add tests

This comment has been minimized.

Copy link
Member Author

commented Jun 21, 2017

Cherry-picked for 0.47.1

@balloob balloob referenced this pull request Jun 21, 2017


0.47.1 #8141


This comment has been minimized.

Copy link

commented Jun 22, 2017

Hi @balloob,

I'm playing with the new component (in 0.47.1) and I have noticed certain lacks related to this entry, I don't know if they are intended:

  • No hass object for comprehensions (NameError: name 'hass' is not defined)
# Not working:
local_data = {k: hass.states.get(k) for k in local_data.keys()}

# Working:
for k in local_data.keys():
    local_data[k] = hass.states.get(k)
  • No key-access to dicts (NameError: name '_getitem_' is not defined):
    close_house = local_data['input_boolean.close_house']
  • No getattr (NameError: name 'getattr' is not defined):
    close_house = getattr(local_data, 'input_boolean.close_house')

This is working:
close_house = local_data.get('input_boolean.close_house')

  • No vars starting with '_': Error loading script Line 2: "_variable_name" is an invalid variable name because it starts with "_"

  • Can't use max: NameError: name 'max' is not defined

  • Can't import anything: ImportError: __import__ not found

Do you want me to publish these as a new issue?


This comment has been minimized.

Copy link
Member Author

commented Jun 22, 2017

Please don't use PRs to report issues. Have a look here:

@home-assistant home-assistant locked and limited conversation to collaborators Jun 22, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.