Skip to content

Rendering Templates Example

Daniel Lashua edited this page Oct 27, 2020 · 3 revisions

The following example shows how to render a template and play the result via the TTS Integration. You will need to set hass_is_global: true in your pyscript configuration.

Please check the related Home Assistant documentation for information on how to use templates.

from homeassistant.helpers.template import Template
def render_template(template, **kwargs):
    tpl = Template(template, hass)
    return str(tpl.async_render(kwargs))

def say_weather():
    message = render_template("Hello! It is {{ states('weather.home') }}!")
    tts.google_translate_say(message=message)

While there may be cases where rendering a Home Assistant Template is exactly what you need, keep in mind that, effectively, pyscript IS a template language of because of its syntax. The above say_weather() function could be written like this:

def say_weather():
    message = f"Hello! It is {weather.home}!"
    tts.google_translate_say(message=message)