Template sensor that cycles through the values of multiple existing sensors every X seconds

Hey community,

I am trying to solve this, but seems I got stuck: I have multiple sensors that output text or numbers as values. I want to create a new template sensor now that shows every X seconds the current value of those existing sensors. So the value of the new template sensor would cycle every X seconds through the values and then start again.

The reason behind this is a KNX BUS based display in my house, that can only show one sensor/value, but I would like to see more values from different sources there.

For now I have this, but it is not cycling though the values, it is just sticking with value1:

sensor:
  - platform: template
    sensors:
      switch_sensor:
        value_template: >
          {% set values = ['value1', 'value2', 'value3'] %}
          {% set index = (now().second // 5) % 3 %}
          {{ values[index] }}

What I still need to solve is, use the real sensor entities and get it to cycle. So for it is not going through the values every 5 seconds.

Any help or suggestion I would be really happy about. Thank you!

Templates that use now() only update every minute. You will have to create a triggered template sensor and use a time pattern trigger ( seconds: "/5" ) if you want it to update every 5 seconds.

Replace your list of values with the states() function to get entity states. e.g.

{% set values = [states('sensor.foo'), states('sensor.bar'), states('sensor.foobar')] %}

Thanks a lot for your hint @tom_l !
My working solution looks now like this:
I have a automation that triggers every 5 seconds a index template sensor. And based on this index template sensor the “real” sensor I would like to use, cycles through the list of other sensors.

Sensors:

sensor:
  - platform: template
    sensors:
      knx_switch_sensor:
        value_template: >
          {% set index = states('sensor.knx_switch_sensor_index') %}
          {% set values = [states('sensor.watermeter_main_value'), states('sensor.daily_pv_generation'), states('sensor.apc_ups_servers_current_real_power')] %}
          {{ values[int(index)] }}
          
  - platform: template
    sensors:
      knx_switch_sensor_index:
        value_template: >
          {% set index = now().second // 5 % 3 %}
          {{ index }} 

Automation:

alias: "Update KNX switch sensor index"
description: ""
trigger:
  - platform: time_pattern
    seconds: /5
condition: []
action:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.knx_switch_sensor_index
mode: single

This works great for my case!

You should really be using the new template integration for new template sensors. It supports triggers so you don’t need an automation:

configuration.yaml

template:
  - trigger:
      - platform: time_pattern
        seconds: "/5"
    sensor:
      - name: KNX Switch Sensor
        state: >
          {% set index = now().second // 5 % 3 %}
          {% set values = [states('sensor.watermeter_main_value'), states('sensor.daily_pv_generation'), states('sensor.apc_ups_servers_current_real_power')] %}
          {{ values[index] }}

While the legacy template sensor platform will continue to be supported for the foreseeable future it will not be getting new features, like state_class.

Thank you @tom_l ! I was not aware of this new template feature. Thats super cool!
I will make sure to use that more in the future! Yours is the far better working solution!

Have a look here:

If you don’t need the entity for other means, you could use an automation instead of the template and use knx.send as action and template the payload: key.

1 Like