Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

States not working? #722

Unanswered
banter240 asked this question in Q&A
Discussion options

Hello, i currently try to get pyscript running but I have some strange issues... I can't get state to work. Lol. I have my config set. I can also import OS.. what am I doing wrong?

allow_all_imports: true
hass_is_global: true
This error originated from a custom integration.
Logger: custom_components.pyscript.file.debug_pyscript_environment.debug_pyscript_environment_info_en
Source: custom_components/pyscript/eval.py:1983
integration: Pyscript Python scripting (documentation, issues)
First occurred: 10:36:39 (3 occurrences)
Last logged: 10:36:39
DEBUG: 'state' object is NOT defined globally in Pyscript.
DEBUG: 'file' object is NOT defined globally in Pyscript.
DEBUG: Python's built-in 'open()' is NOT defined in the global scope.

Testscript:

import datetime
@service
def debug_pyscript_environment_info_en():
 """
 Outputs debug information about the Pyscript environment in English.
 """
 log.info("--- Pyscript Environment Debug START (English) ---")
 try:
 current_time = datetime.datetime.now()
 log.info(f"DEBUG: datetime usable. Current time: {current_time.strftime('%Y-%m-%d %H:%M:%S')}")
 except Exception as e:
 log.error(f"DEBUG: Error with datetime: {e}")
 try:
 log.info(f"DEBUG: 'hass' object type: {type(hass)}")
 if hasattr(hass, "components"):
 log.info("DEBUG: hass.components IS AVAILABLE.")
 if hasattr(hass.components, "history"):
 log.info("DEBUG: hass.components.history IS AVAILABLE.")
 else:
 log.info("DEBUG: hass.components.history is NOT available.")
 else:
 log.info("DEBUG: hass.components is NOT available.")
 except NameError:
 log.error("DEBUG: 'hass' object is NOT defined globally in Pyscript.")
 except Exception as e:
 log.error(f"DEBUG: Error inspecting 'hass' object: {e}")
 try:
 log.info(f"DEBUG: 'state' object type: {type(state)}")
 if hasattr(state, "get_history"):
 log.info("DEBUG: state.get_history() IS available as an attribute.")
 else:
 log.info("DEBUG: state.get_history() is NOT available as an attribute.")
 
 if 'state' in locals() or 'state' in globals():
 try:
 sun_state_obj = state.get('sun.sun')
 if sun_state_obj is not None:
 sun_attrs = state.getattr('sun.sun')
 friendly_name = sun_attrs.get('friendly_name') if sun_attrs else "N/A"
 log.info(f"DEBUG: state.get/getattr('sun.sun') work. Friendly Name: {friendly_name}")
 else:
 log.info("DEBUG: state.get('sun.sun') returned None.")
 except Exception as e_state_get:
 log.error(f"DEBUG: Error using state.get() or state.getattr(): {e_state_get}")
 else:
 log.info("DEBUG: 'state' object not found before state.get() attempt.")
 except NameError:
 log.error("DEBUG: 'state' object is NOT defined globally in Pyscript.")
 except Exception as e:
 log.error(f"DEBUG: Error inspecting 'state' object: {e}")
 try:
 log.info(f"DEBUG: 'file' object type: {type(file)}")
 if hasattr(file, "write"):
 log.info("DEBUG: file.write() IS available as an attribute.")
 else:
 log.info("DEBUG: file.write() is NOT available as an attribute.")
 if hasattr(file, "append"):
 log.info("DEBUG: file.append() IS available as an attribute.") # Should be false if write is the one
 else:
 log.info("DEBUG: file.append() is NOT available as an attribute.")
 except NameError:
 log.error("DEBUG: 'file' object is NOT defined globally in Pyscript.")
 except Exception as e:
 log.error(f"DEBUG: Error inspecting 'file' object: {e}")
 
 try:
 if callable(open):
 log.info("DEBUG: Python's built-in 'open()' IS AVAILABLE and callable.")
 else:
 log.info("DEBUG: Python's built-in 'open()' is in scope but not callable.")
 except NameError:
 log.error("DEBUG: Python's built-in 'open()' is NOT defined in the global scope.")
 
 log.info("--- Pyscript Environment Debug END (English) ---")
 service.call("persistent_notification", "create",
 title="Pyscript Debug (EN)",
 message="Pyscript environment debug info (English) has been written to HA Logs.",
 notification_id="pyscript_debug_env_done_en")
log.info("Pyscript 'debug_pyscript_environment.py' loaded. Service 'pyscript.debug_pyscript_environment_info_en' should be available.")
You must be logged in to vote

Replies: 2 comments

Comment options

So for state you can't access it directly this way - you have to eg. get a specific entity using state.get(...) etc. - you can't inspect it as if it was a standalone object and I think there's also no such thing as get_history() on it. See here for reference on how to use it and here the disclaimer. Remember pyscript is emulating python's behavior in order to provide async operations out of the box to not block home assistant's main thread (it's not "real" python so some things might work differently from what you are used to - to escape this behavior check out @pyscript_compile but beware of its limitations or try creating a native python package alongside pyscript which you can then import into your scripts - you can find out how here).

File operations such as file and open should not be used at all. See here. You should use an async version of these. Here's some information on how to do that

You must be logged in to vote
0 replies
Comment options

@IgnusG - excellent answer - thanks!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /