Skip to content

Instantly share code, notes, and snippets.

@Flygenring
Created February 28, 2026 16:30
Show Gist options
  • Select an option

  • Save Flygenring/ce3098065f880cb2c0cff1898622cf84 to your computer and use it in GitHub Desktop.

Select an option

Save Flygenring/ce3098065f880cb2c0cff1898622cf84 to your computer and use it in GitHub Desktop.
Home Assistant: Sending, receiving, and using JSON data from a Template Sensor Helper
{% set aggregate = namespace(
id = None,
class = None,
state = None
) %}
{% set aggregate.id = 'device id' %}
{% set aggregate.class = 'device class' %}
{% set aggregate.state = 'computed state' %}
{% set ns = namespace(instance = {
"id": aggregate.id,
"class": aggregate.class,
"state": aggregate.state
}) %}
{{ ns.instance | to_json(sort_keys=True) }}
{# Guard unavailable sensor #}
{% if states('sensor.helper_sensor') == 'unavailable' %}
{% set helper_sensor_data = {"id": None, "class": None, "state": None} %}
{% else %}
{% set helper_sensor_data = states('sensor.helper_sensor') | from_json %}
{% endif %}
{##}
<center>
<h3>Device <tt>{{ helper_sensor_data.id }}</tt></h3>
<strong>Device class:</strong> <tt>{{ helper_sensor_data.class }}</tt>
<strong>Device state:</strong> <tt>{{ helper_sensor_data.state }}</tt>
</center>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment