Template sensor with several states

Hi there

I want to template a specific sensor which will have at the end different status (more than two).
E.q. state1, state2, state3,…
So I am wondering how to perform this, I have not seen any helpful information so far. Hopefully anybody could assist me on this. Thanks in advance - JJ

There is no problem. If you have a binary sensor - than you have only 2 values - true / false (on / off), but with “sensor” you can return as many states as you want. Template returns different states (and as it’s sensor) - they can be words.

Thanks, but how to setup such a sensor? Currently no clue at my side how to do such a template sensor, do you have any example and/or link where I could read more of that? Tx - JJ

Template sensors are documented here:

It’s important to be clear on what you are asking for. A sensor has exactly one state: for example, a temperature sensor has the temperature as its state. One state, can be many values.

If you are looking for more than one state, then you have two options:

  • multiple sensors
  • use attributes

Each sensor has one state but can have any number of attributes. For example, our temperature sensor could have humidity and pressure as attributes — or those values could be set up as states of additional sensors.

Here’s an example: this phone sensor has battery power as its state, its main measurement — and it has current and voltage as additional attributes:

If, however, you are looking to create a “tri-state” sensor (one state, three possible values), you can do something like this:

template:
  - sensor: 
      - name: Power rating
        state: >
          {% if states('sensor.power')|float(0) < 100 %}
            low
          {% elif states('sensor.power')|float(0) < 1000 %}
            medium
          {% else %}
            high
          {% endif %}

If you get stuck, come back — but be clear on what it is you’re trying to achieve.

2 Likes