Create a template sensor which outputs a list?

I have a template sensor that creates a text string with a description and a time to help me do things in time. The generated string is based on the current time and various sensors upon which I create some logic.
A short example of the template sensor:

{# States #}
{% set ts_now = as_timestamp(now()) %}
{% set asleep = states('input_boolean.is_asleep') %}

{# Event times #}
{% set leftbedtime = states('input_datetime.left_bed')|as_timestamp %}
{% set most_recent_med_time = states('input_datetime.latest_medicine')|as_timestamp %}
{% set most_recent_food_time = states('input_datetime.latest_food')|as_timestamp %}

{# Relative times #}
{% set timesincebed = (now()|as_timestamp)-leftbedtime %}
{% set timesincemedicine = (now()|as_timestamp)-most_recent_med_time %}
{% set timesincefood = (now()|as_timestamp)-most_recent_food_time %}

{% if asleep == "on" %} Wake up! (06:30)
{% elif timesincemedicine > 6*3600 %}
                Take medicine! ({{leftbedtime+10*60|timestamp_custom('%H:%M:%S')}})
{% elif timesincefood > 6*3600 %}
                Eat! ({{most_recent_food_time +8*3600|timestamp_custom('%H:%M:%S')}})
{% endif %}

So when I am asleep, the sensor will always tell me to wake up at 6:30.
If I left bed at 6:45, it will tell me to take medicine at 6:55.
After having taken the medicine, it will tell me to eat, and a time 8 hours after previous meal.

Now, I would want to separate the description (“wake up”, “eat”, “take medicine”) from the suggested time, into two sensors. My idea is to let this template sensor deliver a list entry and then create two other sensors that just present the first or the second part of that list as its sensor value.
Problem is, I tried to generate a list in the template editor, but it only worked with static strings, not with variables. Can you suggest a way to change the template above so that it returns a list instead of a text?

I suspect that if you shared more information about why you want to take this specific approach, it would be possible to suggest a different, cleaner way.

For example, logic of this nature is often more naturally handled in an automation.

It’s possible to store lists and dictionaries in sensor attributes, but a sensor state is typically a single value with an associated device class and measurement unit. One way to do it close to how you want to do it is to put your dictionary/list in an attribute of the sensor. The code complexity gets high very fast with such an approach.

Please show your expected output.

A jinja2 list looks like …

['a', 'b', 'c', 'x', 'k']

You are not building anything like that. So I am wondering what a ‘list’ looks like to you.

My template sensor has looked like the current one for long time. I have only used to look at it to remember what to do. It is a string describing what my next suggested action to do is, and at what time I have to do it at latest.

What’s new for now is that I wanted to extract the time part of the string, and use that one in an automation using tts to repeat the phrase every x mins if I am overdue.

I wanted a list with the current action.
So if I am asleep, the list should be:
[‘Wake up!’, ‘06:30’]

(Until now, it has created a text string that says “Wake up! (06:30)”)

However, I think I can manage this by letting it continue to generate text strings.
I will just modify the formatting in a csv style so it for example creates the text “Wake Up!;06:30;”.

Then I will create another template sensor that extracts the time.

The time template sensor:

{% set theevent = states('sensor.nextevent') %}
{{theevent.split(';')[1]}}

For something this limited, extracting the string in your TTS automation is a way more practical approach than resorting to lists in sensors. The advice might be different if you had something way more complex.

You can keep your old format (with the time in parens) and extract the string using a regular expression; that’s one step up from split.

Also, it feels to me that a more natural way to approach this—if you want to build it out in the future to cover more kinds of actions—is with a calendar or todo list, rather than a template sensor.

The full template sensor has about 30 different conditions to generate different strings.
I only want to manage that logic in one place, hence the desire to split the output from the sensor.

Ah, gotcha.

You could certainly put any symbol in there you want and do the split. Or, make a list. Or, whatever.

{% if asleep == "on" %} Wake up!|06:30
{% elif timesincemedicine > 6*3600 %}
                Take medicine!|{{leftbedtime+10*60|timestamp_custom('%H:%M:%S')}}
{% elif timesincefood > 6*3600 %}
                Eat!|{{most_recent_food_time +8*3600|timestamp_custom('%H:%M:%S')}}
{% endif %}

The above example would split on the pipe character (‘|’)

The calendar/todo list sounds promising.

1 Like

I am not sure how a todo list/calendar would benefit me with the sensor part…? I dont think it can be so flexible as I want it.

I have a similar issue, trying to create a sensor with a simple list of quarterly charge prices to display in an apex chart. The sensor works in the development-template environment, but not as part of the template.yaml file - keeps showing as unavailable.
Here my logic:

260302 - creation date

Name: elec_max_charge_price_apex

purpose: for display in apex chart -

Shows the maximum daytime quarterly price to allow charging using:

1. the nighttime charge prices of zero between 00:00 and 09:00

2. the daytime charge prices between 09:00 and 17:00

3. the evening charge prices of zero between 17:00 and 24:00

Calculations below have the folloPreformatted textwing arguments:

a. PRI is the maximum daytime charge price

b. PR1, PR2 and PR3 are used to establish the CNT of quarters to display 96 or 192

c. XXXHOUR and XXXQ are used to build the quarterly prices from hourly ones

d. LIST96 is used for the end result if CNT is not equal 192

e. LIST192 is built by doubling LIST96 an used for the end result if CNT == 192

  • sensor:
    • name: elec_max_daytime_charge_price_apex
      unique_id: kjhbvdf-n3056897tyub-nolaserfokcmnx
      unit_of_measurement: “€/kWh”
      state_class: measurement
      state: >
      {% set PRI = states(‘sensor.elec_max_daytime_charge_price’) | float(0.245) -%}
      {% set PR1 = state_attr(‘sensor.nordpool_kwh_nl_eur_3_10_021’,‘today’) | list -%}
      {% set PR2 = state_attr(‘sensor.nordpool_kwh_nl_eur_3_10_021’,‘tomorrow’) | list -%}
      {% set PR3 = PR1 + PR2 | list -%}
      {% set CNT = PR3 | count | int -%}
      {% set NIGHTHOUR = [0, 0, 0, 0, 0, 0, 0, 0, 0] | list -%}
      {% set NIGHTQ = NIGHTHOUR + NIGHTHOUR + NIGHTHOUR + NIGHTHOUR | list -%}
      {% set DAYHOUR = [PRI, PRI, PRI, PRI, PRI, PRI, PRI, PRI] | list -%}
      {% set DAYQ = DAYHOUR + DAYHOUR + DAYHOUR + DAYHOUR | list -%}
      {% set EVEHOUR = (0, 0, 0, 0, 0, 0, 0) | list -%}
      {% set EVEQ = EVEHOUR + EVEHOUR + EVEHOUR + EVEHOUR | list -%}
      {% set LIST96 = (NIGHTQ + DAYQ + EVEQ) | list -%}
      {% set LIST192 = LIST96 | list -%}
      {% if CNT == 192 -%}
      {% set LIST192 = (LIST96 + LIST96) | list -%}
      {% set MAXLIST = LIST192 | list -%}
      {% else -%}
      {% set MAXLIST = LIST96 | list -%}
      {% endif -%}
      {{MAXLIST}}

End of elec_max_daytime_charge_price_apex calculation

Please learn to format your post correctly: see here.

What is the output of your template in the developer tools? Note that sensor states have a maximum size of 256 characters, and are always strings.

There will be an error in your logs at startup telling you what’s wrong with your template.

Sorry about the formatting
Think I got it now

The logs throw an error with the following details:


Logger: homeassistant.components.template.validators
Source: components/template/validators.py:39
integration: Template (documentation, issues)
First occurred: 7 March 2026 at 06:30:31 (86 occurrences)
Last logged: 10:41:50

Received invalid sensor state: [20.45, 20.6, 19.09, 16.77, 17.2, 15.81, 16.95, 15.78, 16.62, 16.46, 17.19, 18.87]
for entity sensor.xxxxx, expected a number

What I want to accomplish is publish a list of numbers in a sensor, like here the historical EV efficiency by month of the year:

- sensor:
    - name: car_xxxxx_monthly_efficiency
      unique_id: yyyyyyy
      unit_of_measurement: W/km
      state_class: measurement
      device_class: power_factor
      state: >
        {% set EFF =  [20.45, 20.6, 19.09, 16.77, 17.2, 15.81, 16.95, 15.78, 16.62, 16.46, 17.19, 18.87] | list -%}
        {{ EFF }}

The result sofar is not a list of numbers, but the state being
unknown

Clearly I am in need of some guidance.

You need to remove the definitions for state_class, device_class, and unit_of_measurement. They are not appropriate for the output you want… with them (as the error states) a single numeric value is expected.

- sensor:
    - name: car_xxxxx_monthly_efficiency
      unique_id: yyyyyyy
      state: >
        {% set EFF =  [20.45, 20.6, 19.09, 16.77, 17.2, 15.81, 16.95, 15.78, 16.62, 16.46, 17.19, 18.87] %}
        {{ EFF }}
1 Like

Sometimes it is as simple as this :slight_smile:
Thanks !!!

There’s no need for the |list filter though. That’s trying to turn a list into a list, but the sensor state will still be a string. When you come to use it, you’ll have to refer it it using something like:

{{ (states('sensor.car_xxxxx_monthly_efficiency')|from_json)[now().month-1] }}
1 Like

I am starting to get dangerous … I have 6 lists with 15 minute electricity price data, each containing 8 hours of data, 3 for today and 3 for tomorrow, coming out of the ESPHOME sensor built with LEGOLAS’es electricity price ticker (works like a charm).

What the objective is, is to get this data into one list of prices, combined for today and tomorrow.

Here’s my code:

    - name: entsoe_15min_prices
      unique_id: as_erkuhv478b_vqn_lhfjib9348b_loijse789bc
      state: >
        {% set EP1 = states('sensor.entso_e_prices_entso_e_15_min_prices_eur_kwh_json_p1_00_00_07_45') | from_json -%}
        {% set EP2 = states('sensor.entso_e_prices_entso_e_15_min_prices_eur_kwh_json_p2_08_00_15_45') | from_json -%}
        {% set EP3 = states('sensor.entso_e_prices_entso_e_15_min_prices_eur_kwh_json_p3_16_00_23_45') | from_json -%}
        {% set TODAY = EP1 + EP2 + EP3 -%}
        {% set CNTT = TODAY | count -%}
        {% set NEP1 = states('sensor.entso_e_prices_entso_e_next_day_15_min_prices_eur_kwh_json_p1_00_00_07_45') | from_json -%}
        {% set NEP2 = states('sensor.entso_e_prices_entso_e_next_day_15_min_prices_eur_kwh_json_p2_08_00_15_45') | from_json -%}
        {% set NEP3 = states('sensor.entso_e_prices_entso_e_next_day_15_min_prices_eur_kwh_json_p3_16_00_23_45') | from_json -%}
        {% set TOMORROW = NEP1 + NEP2 + NEP3 -%}
        {% set CNTN = TOMORROW | count -%}
        {% if CNTN == 96 -%}
          {% set ENTSO = TODAY + TOMORROW | list -%}
        {% else -%}
          {% set ENTSO = TODAY | list -%}
        {% endif -%}
        {{ ENTSO }}

In the development section in HA this works well, but alas, the sensor has the state unkown

That’s 754 characters long. States are limited to 254.

^^ You have to do that in order to retain more characters. You also won’t need from/to_json