diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index dfab80e5223..24852ab5cf3 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -559,8 +559,11 @@ class Template: @callback def async_render_with_possible_json_value( - self, value, error_value=_SENTINEL, variables=None - ): + self, + value: Any, + error_value: Any = _SENTINEL, + variables: dict[str, Any] | None = None, + ) -> Any: """Render template with value exposed. If valid JSON will expose value_json too. @@ -570,8 +573,7 @@ class Template: if self.is_static: return self.template - if self._compiled is None: - self._ensure_compiled() + compiled = self._compiled or self._ensure_compiled() variables = dict(variables or {}) variables["value"] = value @@ -580,9 +582,7 @@ class Template: variables["value_json"] = json_loads(value) try: - return _render_with_context( - self.template, self._compiled, **variables - ).strip() + return _render_with_context(self.template, compiled, **variables).strip() except jinja2.TemplateError as ex: if error_value is _SENTINEL: _LOGGER.error(