diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index c4c8f3a02f0..eeb43fb8756 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -1051,6 +1051,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.filters["atan2"] = arc_tangent2 self.filters["sqrt"] = square_root self.filters["as_timestamp"] = forgiving_as_timestamp + self.filters["as_local"] = dt_util.as_local self.filters["timestamp_custom"] = timestamp_custom self.filters["timestamp_local"] = timestamp_local self.filters["timestamp_utc"] = timestamp_utc @@ -1084,6 +1085,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.globals["atan2"] = arc_tangent2 self.globals["float"] = forgiving_float self.globals["now"] = dt_util.now + self.globals["as_local"] = dt_util.as_local self.globals["utcnow"] = dt_util.utcnow self.globals["as_timestamp"] = forgiving_as_timestamp self.globals["relative_time"] = relative_time diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 5b32634d8f0..237e529652a 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -511,6 +511,19 @@ def test_timestamp_local(hass): ) +def test_as_local(hass): + """Test converting time to local.""" + + hass.states.async_set("test.object", "available") + last_updated = hass.states.get("test.object").last_updated + assert template.Template( + "{{ as_local(states.test.object.last_updated) }}", hass + ).async_render() == str(dt_util.as_local(last_updated)) + assert template.Template( + "{{ states.test.object.last_updated | as_local }}", hass + ).async_render() == str(dt_util.as_local(last_updated)) + + def test_to_json(hass): """Test the object to JSON string filter."""