pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Template evaluation

Open SVH-Powel opened this issue 1 year ago • 2 comments

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

SVH-Powel avatar Feb 29 '24 08:02 SVH-Powel

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

ALERTua avatar Feb 29 '24 09:02 ALERTua

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.

SVH-Powel avatar Feb 29 '24 09:02 SVH-Powel