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

Template evaluation #589

Open
SVH-Powel opened this issue Feb 29, 2024 · 2 comments
Open

Template evaluation #589

SVH-Powel opened this issue Feb 29, 2024 · 2 comments

Comments

@SVH-Powel
Copy link

This is a feature request

Make it possible to evaluate templates in pyscript. Preferable with a function call that takes the template as a parameter and return the value from the evaluation.

https://community.home-assistant.io/t/pyscript-new-integration-for-easy-and-powerful-python-scripting/215967/304

@ALERTua
Copy link
Contributor

ALERTua commented Feb 29, 2024

with from homeassistant.helpers.template import Template you can instantiate Template('{{ states('light.office') == 'off' }}', hass) and .async_render() it and get your result :)

import homeassistant.helpers.template as template

@service
def tryouts(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
    templ = template.Template("{{ states('light.office') == 'off' }}", hass)
    result = templ.async_render()
    log.debug(f"{templ}: {result}")

Calling pyscript.tryouts service results in this log entry
Template<template=({{ states('light.office') == 'off' }}) renders=1>: True

@SVH-Powel
Copy link
Author

with from homeassistant.helpers.template import Template you can instantiate Template('{{ states('light.office') == 'off' }}', hass) and .async_render() it and get your result :)

import homeassistant.helpers.template as template

@service
def tryouts(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
    templ = template.Template("{{ states('light.office') == 'off' }}", hass)
    result = templ.async_render()
    log.debug(f"{templ}: {result}")

Calling pyscript.tryouts service results in this log entry Template<template=({{ states('light.office') == 'off' }}) renders=1>: True

Thanks. But still, I think this would be a nice feature without having to enable hass as a global variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants