Value Template not showing correct values

Hi All,

I’ve been struggling with using the value template to change the value based on something a little more relevant. Using MQTT sensors which return a value of either OFF or ON. I want to translate these into Normal and Low respectively.

My sensor configuration is a follows:

  - platform: mqtt
    state_topic: "cbus/read/254/56/82/state"
    name: Greenhouse NFT Water Level Low
    value_template: >
        {% if is_state('sensor.greenhouse_nft_water_level_low', 'ON') -%}
          NFT Low
        {%- else -%}
          NFT Normal
        {%- endif %}

The sensor itself is currently ON and should be showing ‘NFT Low’. Instead it shows ‘NFT Normal’.

If I put this same value_template code into the Dev Tooling, it shows the correct value of ‘NFT Low’.

{% if is_state('sensor.greenhouse_nft_water_level_low', 'ON') -%}
  NFT Low
{%- else -%}
  NFT Normal
{%- endif %}

Any assistance would be appreciated while I get my head around HA.

Cheers,
Paul

You are looking at the sensors’ state rather than the received value. Try this:

  - platform: mqtt
    state_topic: "cbus/read/254/56/82/state"
    name: Greenhouse NFT Water Level Low
    value_template: >
      {% if  value == 'ON' %}
        NFT Low
      {% else %}
        NFT Normal
      {% endif %}

Or this:

  - platform: mqtt
    state_topic: "cbus/read/254/56/82/state"
    name: Greenhouse NFT Water Level Low
    value_template: "{{ 'NFT Low' if value == 'ON' else 'NFT Normal' }}"

Ahh ok, much appreciated. I love learning new things, but hate it at the same time, hope I get the hang of this sooner rather than later, will be easier on you guys :slight_smile:

So, I’m still not sure of the difference between state and value in this instance, they do sound like the same thing.

Regs,
Paul

{{ value }} is what you receive on the mqtt topic… ON or OFF.

{{ states('sensor.greenhouse_nft_water_level_low') }} is the state of your sensor, NFT Low or NFT Normal

So this is_state('sensor.greenhouse_nft_water_level_low', 'ON') would never be true as the state is never ON.

OK, that’s a bit clearer, much appreciated. Lots more playing and learning.

Cheers,
Paul