Value Templates

More template fun from me today, I see historically you could use value templates when creating a template sensor, but I can’t see anything like that in the documentation for the modern templates,

Basically I am trying to take the value from a sensor and pass it on, unless it’s ‘unavailable’ in which case replace it with N/A or Off.

sensor:
  - name: Kitchen_timer
    value_template: >
      {% if states('sensor.kitchen_echo_next_timer') == "unavailable" %}
        {{states('N/A')}}
      {% else %}
      {{states('sensor.kitchen_echo_next_timer')}}
      {% endif %}

This is what I’ve come up with based on a sensor template info I’ve managed to find, but obviously I am doing sometehing wrong, anyone got any ideas.

Try


sensor:
  - name: Kitchen_timer
    state: >
      {% if states('sensor.kitchen_echo_next_timer') == "unavailable" %}
        N/A
      {% else %}
      {{states('sensor.kitchen_echo_next_timer')}}
      {% endif %}

This is an example of one of my template sensors for reference:

    - name: 'Spotify: Title'
      availability: "{{states('media_player.spotify') == 'playing'}}"
      state: >
        {% if states('media_player.spotify') != 'playing' %}
          Pause
        {% else %}
          {{state_attr('media_player.spotify', 'media_title') }}
        {% endif %}

← note that for static text output, you don’t need a states() call

First you need to change to the new Template integration:

Second - it’s pretty much the same:

template:
  - sensor:
      - name: Kitchen Timer
        unit_of_measurement: 'C'
        state: >
           {% if states('sensor.kitchen_echo_next_timer') == "unavailable" %}
             {{ states('sensor.kitchen_timer')|float(0) }}
           {% else %}
             {{ states('sensor.kitchen_echo_next_timer')|float(0) }}
           {% endif %}

You want to output the existing state of the sensor in the event the source sensor is unavailable I assume, this does that.

You can also tidy it up it a bit like this:

{% set c = states('sensor.kitchen_timer') %}
{% set e = states('sensor.kitchen_echo_next_timer') %}
{{ c|float(0) if e in ['unknown','unavailable'] else e|float(0) }}

Cheers lads and lasses, gotten it working thanks to Code_talker, I have no idea why I am having soo much trouble with templates, it’s all new to me and having to learn it all, getting there slowly.