Check for correct sensor values in a template sensor

Hello,
i am a newbee in HA and struggeld around for a day to check for correct values in a template sensor. The template sensor is working and in the ‘developer code checker’ it is also working inclusive the value check.
But when copying the code in de template file the sensor is updated but the value check is not working. I cannot find anything how to make it work.
Can somebody help me out?
Thanks in advance.
see below the yaml code

Never ever post a screenshot of a code.
Paste a code using a “preformatted text” button (or simply place a code inside triple backquotes).

Some remarks:

  1. Instead of
state: "{{ states['sensor.xxx']['state'] }}"

use

state: "{{ states('sensor.xxx') }}"
  1. This code
attributes:
  value_template: ...

is supposed to create an attribute literally named “value_template” - is it what you really need?
Not smth like

attributes:
  my_super_attr: ...

?

  1. These numerous “raw_value|float(default=0)” may be replaced by “raw_value|float” - since all these operations are performed if “raw_value | is_number”, thus no need to use a default value in “float()”.

What do you mean exactly?

hello,
thanks for the reply, what is exactly the difference between de syntax for state in your first tip?

I use float(default=0) because the original value is a string, but i can try to leave it out en see wat happens.

What i mean with the value check is not working is that incorrect readings wil still be in the data. So now en then i get readings of a COP 150 or more. I want filter them out.

See a note at the end of this chapter

Converting a string “12.34” to float 12.34 is done by “float”.
But a default value in “float(default=0)” is provided to avoid an error if a string is not numerical (“unavailable”, “unknown”). Since you performs these operations inside “is_number” block - these default values are not needed (the string value is supposed to be numerical).

Cannot say for sure why it could happen. For you, posting a properly formatted code (a strong suggestion you missed) would be a step forward to help other people to help you.

there is an issue with this template, as it will return 0.0, even if the entity is not available, or a number.

You are probably better off setting an availability_template on the entity being a number. In that case, you will be able to see when the raw data is correct

It will also make the main template much simpler, and more precise at all times

because, when the value is a number it will simply return the value of that number, and not mask it as 0.
like

unique_id: cop_as_number
name: COP as number
state: >
  {{states('sensor.heat_pump_coefficient_of_performance')}}
availability_template: >
  {{states('sensor.heat_pump_coefficient_of_performance')|is_number}}

Hi, thanks for the reply.
Normaly the value of the sensor is a number but it can have incorrect value’s.
It is de COP of a heatpump an sometimes a cop more then 7 (e.g. 231, 37.5 etc.) these values will destroy history grahps ans calculations. So i want to ignore the exceptional values en also the non numeric values. Ideal would be if the value is out of bounds it could be replaced by the last correct value. But i do not know how to retrieve the last know correct value.

I have now the script at the end of this post that returns only values from 0.0 to 7.0. :slight_smile:

The "| float(default=0) " can perhaps be ommited after the statement
{% set raw_value = states(‘sensor.heat_pump_coefficient_of_performance’) | float(default=0) %}

i do not know what will returned in raw_value if the value of the sensor is not a number in the {% set raw… %} statement.

- sensor:
    - name: COP_as_number_4
      unit_of_measurement: " "
      state: >
          {% set raw_value = states('sensor.heat_pump_coefficient_of_performance') | float(default=0)  %}
          {% if raw_value | is_number  %}
            {% if raw_value | float(default=0) >= 0.0 and raw_value | float(default=0) < 7.0 %}  
              {{ raw_value | float(default=0) }}
            {% else %}
              0.0
            {% endif %}
          {% else %}
            0.0
          {% endif %}


Note: i do not understand how exactly the availibilty template works out.
If the case is that the value will be ignored if not within bounds or not is a number then this is also a good solution. I gonna try to figure out how this works.

in case the availability template returns false, the entity will go unavailable, and wont destroy your graphs…

there are also core filter entities that remove outliers (extreme values) but you have to get acquainted with their syntax, and options

in case the availability (using is_number() is true) the defaults are no longer required.

as long as you can get a correct output in dev tools without it, you will be fine. Got to remember though that templates are always strings so you do need to set the |float filter

okay, so the availability template is checked first and if it returns true then the state: statement is used otherwise the value is ignored.

thanks again, i think i have it working.
Was a bit puzzeling for the correct syntax but it seems to work now.

so what did you end up with?