Sensor template doesn't is not working as I expect

I have an Ultrasonic sensor on an ESPhome integration. Based on a certain distance, I would like to set a state of a template sensor.

Looking in the developer tools-> states, the ultrasonic sensor reading is 0.91.

If I want to set the output of the template sensor, I would like to see if the distance is between 0.85 and 0.95. Therefor, I use this template:

{%if states(‘sensor.sbf4_ultrasonic_sensor’) |float < 0.95 and
states(‘sensor.sbf4_ultrasonic_sensor’) |float > 0.85%}
Empty
{% else %}
Full
{% endif %}

The output of the template is “Full”, but the sensor state is 0.91

Looking at the template tool:

{{states (‘sensor.sbf4_ultrasonic_sensor’) |float |round (4) }}
This results in 0.0

Looking at the States tool:
image

It shows state: 0.91

So there is a difference in processing the ultrasonic sensor value.
The ultrasonic sensor is only outputting its value a couple of times per hour and the ESP is also using sleep modes. However, in the developer tools-> states, the 0.91 is always there. If I overwrite the value and update it again, it will show 0.91 again, while the template shows 0.0.
What is going on here

No space between states and (

Please format your pasted code and show the full sensor configuration.

Hi Tom,

I haven’t configured the complete sensor yet. Since I am not an expert in coding, I thought to test it in the developer test tool first. Adding the space didn’t change anything.

You were asking about the yaml code, I suppose?

What does this report in the Template Editor?

{{ states('sensor.sbf4_ultrasonic_sensor') }}
{{ states('sensor.sbf4_ultrasonic_sensor') | float(0) }}
{{ states('sensor.sbf4_ultrasonic_sensor') | float(0) | round(4) }}

sensor.sbf4_ultrasonic_sensor doesn’t exist on my system so, for me, the three templates report the following:

Hello Taras,


Does this help?

I would expect that the template would also show 0.91 instead of 0.0.

It’s due a spelling error.

The sensor’s entity_id is sensor.sfb4_ultrasonic_sensor but in the template you used sensor.sbf4_ultrasonic_sensor.

Screenshot from 2022-01-14 11-09-11

Screenshot from 2022-01-14 11-08-36

Change sbf4 in the template to sfb4.

Hello Taras,

Thanks a lot!! Silly me. I spent a long time figuring out what was going on. Now it is working :slight_smile:

The code looks like this (naming is in Dutch)

  sfb4_hooi:
    friendly_name: "SFB4 hoeveelheid hooi"
    value_template: >-
     {% if states('sensor.sfb4_ultrasonic_sensor')|float < 0.95 and
     states('sensor.sfb4_ultrasonic_sensor')|float > 0.85%}
     Leeg
     {% else %}
     Vol
     {% endif %}

:ok_hand: :+1:

You’re welcome!

If you’re interested, you can reduce the template by using an ‘inline if’ like this:

  sfb4_hooi:
    friendly_name: "SFB4 hoeveelheid hooi"
    value_template: "{{ 'Leeg' if 0.85 < states('sensor.sfb4_ultrasonic_sensor')|float(0) < 0.95 else 'Vol' }}"

The way to read it is like this:
Report Leeg if the sensor’s value is greater than 0.85 and less than than 0.95 otherwise report Vol.

Hi Taras,
That’s really compact and efficient coding. I notice that I have a lot to learn here :slight_smile:
Thanks!