Pyscript - new integration for easy and powerful Python scripting


has anyone else struggled with getting time_triggers to behave as expected according to your timezone?

I’ve noticed that have I to account for my timezone e.g. when trying to have a nightly trigger at midnight:

@time_trigger("period(midnight, 1 days)")

seems to trigger at midnight UTC time, so I have to specify

@time_trigger("period(midnight + 2 hours, 1 days)")

instead to get midnight in UTC+2.

No big deal, but it means updating the automations every DST change. Any easy fix I can do on my end? I’ve set the timezone correctly in my Home Assistant install.

I need to convert a text string containing json into a json object. The string comes in as an mqtt message and I would call a pyscript in the action section of a mqtt platform trigger.

I cannot find a way to do this. Can it be done? Could you provide a simple example of a @service pyscript which does this?

Alternatively, maybe I can pass a json object (not string) in to pyscript from a yaml template filter “tojson”. If this can be done, can anyone provide a simple example of this?

Thanks for any help or suggestions!

P.S. I completely green to pyscript, python_script and/or AppDaemon. I am trying to find a way to to get data: into a generalized service: call from a yaml automation.

I.E., this doesn’t seem to be possible:

- alias: 'MqttAPI'
  initial_state: true
  mode: parallel
    platform: mqtt
    topic: 'HomeAssistant/API/Service/#'
    - service: system_log.write
        level: info
        message: "MqttAPI: service: {{ trigger.topic | replace('HomeAssistant/API/Service/', '') | replace('/', '.') }} message: ({{ trigger.payload }})"
    - service: "{{ trigger.topic | replace('HomeAssistant/API/Service/','') | replace('/', '.') }}"
      data: |
        {{ '{"entity_id": "media_player.d_office_mini", "media_content_id": "/local/audio/hello.mp3", "media_content_type": "music"}' | tojson }}

So I need a way to do this using a pyscript…

The code above is just for testing. The actual service call data would be:

- service: "{{ trigger.topic | replace('HomeAssistant/API/Service/','') | replace('/', '.') }}"
      data: "{{  trigger.payload | tojson }}"

I figured it out:

- alias: 'MqttAPI'
  initial_state: true
  mode: parallel
    platform: mqtt
    topic: 'HomeAssistant/API/Service/#'
    - service: pyscript.mqttapi
        service: "{{ trigger.topic | replace('HomeAssistant/API/Service/','') | replace('/', '.') }}"
        params: "{{ trigger.payload }}"


def mqttapi(service=None, params={}):
    """Call HA service.""""Service: {service} Data: {params}")
    if service is not None:

MQTT publish:

mosquitto_pub -t HomeAssistant/API/Service/media_player/play_media -m '{"entity_id":"media_player.d_office_mini", "media_content_type":"music", "media_content_id":"/local/audio/hello.mp3"}'

Experts, please check and let me know if you see any problem/issues/improvements.


Can anyone help me getting this to work within PyScript? w1ll1am23/pydukeenergy: Access to the undocumented Duke Energy usage API (

Honestly, I’m not even sure how I would go about getting it to work. What I am wanting to do is pull data from my Duke Energy account and display it on the energy dashboard.

@craigb Regarding your TCP connection above,
How would this be modified to use UDP instead of TCP? I’m looking at online docs now, but am a bit unsure. I have an AV processor that I need to send an XML string to for command control. For example:

<?xml version="1.0" encoding="utf-8"?<emotivaControl><power_on value="0" ack="no" /></emotivaControl>

I’m unsure if I want to pass that entire string as a payload to my “UDP Sender” or if I want to pass the two variables for “command” and “value”; letting this script plug in the values. I’m looking for something simple really. Click a button that sends either variables to the script, or the entire XML string, which gets sent to the AVP.
Thank you in advance!

First of all, pyscript is amazing! Incredibly powerful to be able to express scripts in Python, be able to import modules, and it is very nicely integrated.

Is there, or are there any plans for a gallery of contributed scripts or libraries? Or a list of repos where people have done useful pyscripts?

I’m working on some image processing and if nothing else would be interested in contributing it back.


pyscript 1.3.2 installed yesterday on HA Core (Raspbery PI flash install) 2022.2.9

I have set up requirements.txt is failing on module called json. I have tried with and without a version number.

2022-03-02 08:59:02 INFO (MainThread) [custom_components.pyscript] Installing the following packages: {'json': {'version': '_unpinned_version', 'sources': ['/config/pyscript/requirements.txt'], 'installed_version': None}}

2022-03-02 08:59:03 INFO (SyncWorker_3) [homeassistant.util.package] Attempting install of json

2022-03-02 08:59:20 ERROR (SyncWorker_3) [homeassistant.util.package] Unable to install package json: ERROR: Could not find a version that satisfies the requirement json (from versions: none)

ERROR: No matching distribution found for json

WARNING: You are using pip version 20.2.4; however, version 22.0.3 is available.

@Pacers31Colts18 Did you get anywhere with this?

I want to do the same with my energy provider - I have found working python on GIT but getting it working here is not as easy as I expected.

I’m not sure that pyscript is the best platform to host arbitrary external python packages. It runs inside of Home Assistant and isn’t exactly a the standard python interpreter environment that you’d run at the command line.

For something like this Duke Energy polling application, I’d just build an external script using that library to poll the external provider. Then I’d gather the response and push that data into Home Assistant using MQTT to MQTT sensors. I do this with a number of one-off sort of integration problems (like my Tempest Weatherflow weather station.)

I have done some other work with pyscript, and it’s a really great solution for a bunch of different problems. I think that complicated external libraries might cause you issues if they can’t operate in the async environment that pyscript provides. I could be off-base here; I’ve only done one non-trivial pyscript bit of development which solves a different flavor problem.


I’ve been struggling to create a selenium script using pyscript. I have read the docs and tried to implement my understanding of them but haven’t been able to get this working. Is this something someone can please help with?

Main issue:

  • Script not running with below error:
RuntimeError: Blocking calls must be done in the executor or a separate thread; Use `await hass.async_add_executor_job()` at custom_components/pyscript/, line 1899: return func(*args, **kwargs)

I’ve tried different approaches from the documentation I’ve read and added the following line to my script:

if "/config/pyscript_modules" not in sys.path:

I’ve created the pyscript_modules file under my home assistant config folder. This folder contains which has the get_info function.

I have a file called under the pyscripts directory and added this line of code to call my script:

task.executor(scraper.get_info, location)

This then gives me a new error saying:

ModuleNotFoundError: No module named 'scraper'

Although this imports the module fine when I have it under pyscript/modules/

Other issues I’ve faced but managed to progress:

  • Not installing modules listed in requirements.txt file. I’m on docker and in the end I just installed it on the container manually using pip although it would have been nice to get the auto install working from this file. I have allow all imports enabled.
  • ‘AttributeError: module ‘selenium’ has no attribute ‘webdriver’’ when importing selenium module using the config below. I managed to sort this by changing how I import this after reading a post on here. But the below config wasn’t working for me.
	from selenium import webdriver
	from import Options

Many thanks in advance


Never mind got this working now. Sometimes all it needs is a restart :slight_smile:

Good point. Is there a lightweight API for creating a bridge between pyscript in HA and a service (also probably python) in a container add-on?

Hello there.

I’ve just discovered this cool integration as I want use the plexapi module for some automations.
I’m trying to get the number of unwatched episodes from af specific tv show, but I don’t understand why I get this error:

Exception in <file.main.fetch_unwatched_episode_count> line 8: shows = task.executor(plex.library.section, ‘Tv-serier’) ^ RuntimeError: Blocking calls must be done in the executor or a separate thread

The code so far is:

from plexapi.server import PlexServer

def fetch_unwatched_episode_count():
    baseurl = ''
    token = 'myToken'
    plex = task.executor(PlexServer, baseurl, token)
    shows = task.executor(plex.library.section, 'Tv-serier')

Can anyone explain me how this is?`


Well I moved all the plexapi calls to a function in another .py-file and imported it as a module, then running the new function with task.executor. Then it works.


I am trying to find a somewhat over the top way of moving my blinds to follow the sun across the windows in my house. I started with a template binary_sensor, but the jinja2 required is just too much, especially given the amount of standard libraries one could use from Python to implement standard algorithms. So I would like to know if pyscript could do the following:

  • The output should be a binary sensor which will be turned on whenever the script passes a bunch of conditions.
  • The script will need to import the shapely python module, so that I can deal with a bunch of projective geometry and unions of polygons without rewriting everything from scratch
  • I’ll need to have access to a few sensors and the sun’s position from HA.
  • Ideally I would have one such script and run separate instaces for every cover in the house, with a simple config for each of them.

Is this something that pyscript can do? Essentially I want it to be a much more powerful way of writing a template sensor. Or do I need to do the whole automation inside it, rather than just having a simple binary_sensor as an output?

I seem to have managed to do what I wanted with pyscript Rather than some automation, I have used it to compile and compute some values which go into a sensor. I then have normal automations triggering on values of this.

It’s very nice to be able to just use normal python to process data instead of mucking around with jinja2.

1 Like

Hey, very interesting stuff!

May I ask how did you manage to run shapely in pyscript? I was trying myself and it seems not supported yet.


The issue is that you need to install the geos package into the docker image that HA runs on. Not sure if there is a way to do it permanently, so I just execute a shell script with an automation on HA startup:

apk update
apk add geos

If you then turn on the pyscript option to import any module and you add shapely to requirements.txt, you can import shapely in your script and use it.

This method means that when you upgrade HA, the first time it reboots, geos is not available for some reason, but another reboot fixes it.

Would love to know if there is a better way of doing it.

The thing is that pyscript is a custom component and not an AddOn.

It’s possible to use AppDaemon which is an addon where you can specify apk’s to install.
I’ve made an AppDaemon script that allows you to call another command. You can call an external command by calling python3.
Alternatively, you implement your script as an AppDaemon module.

Have a look at my forum reply the gist I shared there:

Any news on support for new classes? I’ve had medium success using classes for now, but it breaks as soon as I try inheritance or state_triggers within a class.

Ive just converted my old python script that scrapes for bin days and color, At the moment it outputs to a text sentence like ‘Next rubbish day is Monday the 27th and is Black’

Im trying to find how to display this text in a markdown card.

Do I need to create the entity first in home assistant or can it be created at runtime in the script.

Ive tried various variations around this in my Pyscript code like:

state.set(entity_id="sensor.rubbish", value="test", new_attributes=None)

just to test the setting up off entities but not getting anywhere, any help would be appreciated.