@Burningstone Good question about AppDaemon. First, a caveat - I’ve only read its documentation and not actually used it (I actually only heard about it a few days ago, since I’m quite new to HASS), and I spent some time re-reading the docs before responding. Overall the goals of pyscript and AppD… Answer from craigb on community.home-assistant.io
🌐
Readthedocs
hacs-pyscript.readthedocs.io
Pyscript: Python Scripting for Home Assistant — hacs-pyscript 2.1.0 documentation
Pyscript: Python Scripting for Home Assistant · View page source · This HACS custom integration for Home Assistant allows you to write Python functions and scripts that can implement a wide range of automation, logic and triggers. State variables are bound to Python variables and services ...
🌐
Home Assistant
community.home-assistant.io › share your projects!
Pyscript - new integration for easy and powerful Python scripting - Share your Projects! - Home Assistant Community
August 2, 2020 - Pyscript is a new integration that allows you to write Python functions and scripts that can implement a wide range of automation, logic and triggers in a very simple manner. State variables are bound to Python variables, and services are callable ...
Discussions

How to start using Pyscript
I have installed the PyScript integration with the aim to write more extensive python scripts. So put my first PyScript in the pyscript directory, but HOW can I use it? It doesn’t show up in the entities (with all the relevant filters on) and I’ve done a PyScript reload. More on community.home-assistant.io
🌐 community.home-assistant.io
4
0
June 6, 2024
Pyscript - how to use it to run a python script?
I want to run a “siple” python script on Home Assistant to extract some data out of my energymanager. The script works just fine on the studioCode Terminal … but how to get it running / calling as automation. therefore i did following pyscript installed include pyscript/config.yaml into ... More on community.home-assistant.io
🌐 community.home-assistant.io
2
0
December 11, 2023
PyScript: Why isn't everyone using it already?
I have yet to find any automations I can't do very easily from the gui or yaml. Perhaps I'm not creative enough but I have lighting and security automated. More on reddit.com
🌐 r/homeassistant
73
42
July 10, 2023
Guidance for First pyscript
I’d like to configure HA to trigger every 5 minutes, and scrape a website of police/ems dispatches. A pyscript should interpret that data and update a hass-helper, as well as determine whether to notify me (notification service already setup) I already have a python script working by itself, ... More on community.home-assistant.io
🌐 community.home-assistant.io
3
0
September 11, 2023
🌐
GitHub
github.com › custom-components › pyscript
GitHub - custom-components/pyscript: Pyscript adds rich Python scripting to HASS · GitHub
Installing the Pyscript Jupyter kernel is optional. The steps to install and use it are in this README. Go to the Integrations menu in the Home Assistant Configuration UI and add Pyscript Python scripting from there.
Author: custom-components
🌐
Home Assistant
community.home-assistant.io › configuration
How to start using Pyscript - Configuration - Home Assistant Community
June 6, 2024 - I have installed the PyScript integration with the aim to write more extensive python scripts. So put my first PyScript in the pyscript directory, but HOW can I use it? It doesn’t show up in the entities (with all the r…
🌐
GitHub
github.com › craigbarratt › home-assistant-pyscript › blob › master › home-assistant.io › source › _integrations › pyscript.markdown
home-assistant-pyscript/home-assistant.io/source/_integrations/pyscript.markdown at master · craigbarratt/home-assistant-pyscript
Pyscript provides functionality that complements the existing automations, templates and triggers. It presents a simplified and more integrated binding for Python scripting than Python Scripts, which provides direct access to Home Assistant ...
Author: craigbarratt
🌐
GitHub
github.com › craigbarratt › home-assistant-pyscript
GitHub - craigbarratt/home-assistant-pyscript: pyscript component for home-assistant · GitHub
Pyscript provides functionality that complements the existing automations, templates, and triggers. It presents a simplified and more integrated binding for Python scripting than Python Scripts, which provides direct access to Hass internals. ...
Author: craigbarratt
Find elsewhere
🌐
Medium
medium.com › @abrinkman_78676 › open-up-new-possibilities-with-pyscript-for-home-assistant-825a7543c363
Open up new possibilities with PyScript for Home Assistant | by Alexander Brinkman | Medium
May 12, 2023 - When you have HACS, installation is pretty easy. Just go to your HACS integrations page, add a new integration and search for pyscript. After adding it you will need to restart Home Assistant. Then add PyScript on your Home Assistant integrations page.
🌐
Home Assistant
community.home-assistant.io › third party integrations
Pyscript - how to use it to run a python script? - Third party integrations - Home Assistant Community
December 11, 2023 - I want to run a “siple” python script on Home Assistant to extract some data out of my energymanager. The script works just fine on the studioCode Terminal … but how to get it running / calling as automation. therefore i did following pyscript installed include pyscript/config.yaml into ...
🌐
Reddit
reddit.com › r/homeassistant › pyscript: why isn't everyone using it already?
r/homeassistant on Reddit: PyScript: Why isn't everyone using it already?
July 10, 2023 -

I initially worked with YAML and native automations, only to find they were challenging to scale and decipher. I tried NodeRed as an alternative; while it was a step forward, the necessity to duplicate similar logic via copied nodes was tedious. Additionally, it didn't gel well with version control due to its source files not being human-readable.

However, my discovery of PyScript a few months ago transformed my approach. As a software engineer, I find using Python for all my automation needs appealing. It empowers me to create intricate, reusable functions and the process of coding automation logic becomes enjoyable, to a large extent!

Let's delve into a couple of examples.

Example 1: Code Shared by Bathrooms

Every bathroom in my home shares a set of logic:

  • Motion detection: Trigger lights.

  • Door closure: Activate lights and fan.

  • Door opening: Switch off lights and fan.

  • Absence of motion with an open door: Lights out.

  • Absence of motion with a closed door: Lights out after 'X' minutes (where 'X' depends on the bathroom, e.g., upstairs bathrooms have a more extended timeout to accommodate showering).

Thanks to PyScript, I could devise a single BathroomTriggers class encapsulating this shared logic, then generate an instance of that class for each bathroom:

# FILE: entity_ids.py

class HalfBath:
    AREA_ID = 'half_bathroom'
    MOTION = 'binary_sensor.half_bathroom_motion_sensor_iaszone'
    DOOR = 'binary_sensor.lk_zb_doorsensor_d0002_iaszone'

# ... other class definitions ...

# FILE: bathrooms.py

class BathTriggers:
    def __init__(self, room, lightsOutDelayMins):
        @motion_trigger(room.MOTION)
        def motion_lights_on():
            if is_open(room.DOOR) and input_select.sleepstatus != 'Sleeping':
                light.turn_on(area_id=room.AREA_ID)
        self.motion_lights_on = motion_lights_on
        #  ... other methods ...

# Invoke the shared trigger class for each bathroom:
halfBathTriggers = BathTriggers(HalfBath, lightsOutDelayMins=10)
basementBathTriggers = BathTriggers(BasementBath, lightsOutDelayMins=20)
upstairsBathTriggers = BathTriggers(UpstairsBath, lightsOutDelayMins=45)

Example 2: Inovelli Switches

I use several Inovelli switches, which come with a customizable LED strip operable via Zigbee or Z-Wave. Although they're fantastic, the command to send the Zigbee LED signal is verbose and non-intuitive. To sidestep repeatedly issuing the command, I devised a straightforward LED control function:

def set_inovelli_switch_led(
  device, color, brightness=255, effect=Effects.SOLID, duration=255):
    '''Configures the light switch LED based on the specified parameters.'''
    zha.issue_zigbee_cluster_command(
      ieee=device_ieee,
      endpoint_id=1,
      command=1,
      cluster_id=64561,
      cluster_type='in',
      command_type='server',
      manufacturer=4655,
      params={
        'led_effect': effect,
        'led_color': color,
        'led_level': brightness,
        'led_duration': duration})

With this, modifying the switch LED becomes a breeze:

set_inovelli_switch_led('42:a2:f2:13:42', Colors.RED, effect=Effects.FAST_BLINK)

Moreover, I've integrated custom enums for Colors and Effects, which map to numeric codes, making it more intuitive (Colors.BLUE is more readable than 172!).

Why Persist with YAML?

Despite its remarkable features, I'm perplexed that PyScript isn't as mainstream as it could be. For those not yet using PyScript: Do you prefer YAML for your automations over Python? Or is there some other factor discouraging your transition to PyScript? I'd love to hear your thoughts!

🌐
Home Assistant
community.home-assistant.io › configuration
Guidance for First pyscript - Configuration - Home Assistant Community
September 11, 2023 - I’d like to configure HA to trigger every 5 minutes, and scrape a website of police/ems dispatches. A pyscript should interpret that data and update a hass-helper, as well as determine whether to notify me (notification …
🌐
Newerest
newerest.space › home-assistant-pyscript-python-automations
Mastering Home Assistant Scripting with pyscript: Bridging Python's Power with Smart Home Automations
October 27, 2025 - Home Assistant's YAML automations ... make them unwieldy. This is where pyscript shines, allowing you to embed full Python scripting directly within Home Assistant....
🌐
Home Assistant
community.home-assistant.io › configuration
Pyscript! are you using it? - Configuration - Home Assistant Community
October 13, 2020 - If you aren’t using pyscript already, you probably should be. It installs as a custom component and makes writing automations that would normally take several different automations and many lines of template code and “choose” actions quite easy with just a little Python.
🌐
Home Assistant
community.home-assistant.io › configuration
Py script service with response - Configuration - Home Assistant Community
January 13, 2024 - I have a python script that retrieves water usage and responds with the json of the date and usage. I have this .py file added to HACs pyscript integration in the /homeassistant/pyscript/ directory and is an available …
🌐
YouTube
youtube.com › watch
Install Pyscript integration for use with View Assist in Home Assistant - YouTube
A video for installing Pyscript integration for use with View Assist in Home Assistant. This will provide the ability to easily extend functionality for pu...
Published: July 16, 2024
🌐
Readthedocs
hacs-pyscript.readthedocs.io › en › latest › tutorial.html
Jupyter Tutorial - Pyscript: Python Scripting for Home Assistant
@service def hello_world(action=None, id=None): """hello_world example using pyscript.""" log.info(f"hello world: got action {action} id {id}") if action == "turn_on" and id is not None: light.turn_on(entity_id=id, brightness=255) elif action == "fire" and id is not None: event.fire(id, param1=12, param2=80) After starting Home Assistant, use the Actions tab in the Developer Tools page to call the service pyscript.hello_world with parameters ·
🌐
Reddit
reddit.com › r/homeassistant › pyscript: python scripting for home assistant
r/homeassistant on Reddit: Pyscript: Python Scripting for Home Assistant
August 3, 2020 - 112 votes, 23 comments. Pyscript adds rich Python scripting to HASS. Contribute to custom-components/pyscript development by creating an account on…
🌐
Reddit
reddit.com › r/homeassistant › pyscript! are you using it?
r/homeassistant on Reddit: Pyscript! Are you using it?
October 14, 2020 -

If you aren't using pyscript already, you probably should be.

It installs as a custom component and makes writing automations that would normally take several different automations and many lines of template code and "choose" actions quite easy with just a little Python.

It's similar to AppDaemon in that you're writing your Automations in Python. However, it doesn't require a separate process, an API Key, or any YAML if you don't want it to.

The syntax is quite simple and Craig has crafted it so that variable names are what you'd expect in Home Assistant. For instance, want to turn on a light when motion is detected?

if binary_sensor.motion == 'on':
   light.turn_on(entity_id="light.overhead")

It also has quite a few advanced features -- like packaging code into an "app" to be used by others with just a little YAML to configure it. So, if you make something useful (like my conditional average app) you can share it and no one else ever has to write that code again. In a way, it's like the "Apps" that SmartThings has, only in Python, and specifically for Home Assistant.

Give it a shot. And if you get stuck on something, I'm happy to help.