Set template sensor to undefined

Help me please! :slight_smile:
Does any way to set value of template sensor to “undefined” state?

I’ve try it to set like this, but it’s not work :frowning:

- platform: template
  sensors:
    owm_pressure_mmhg:
      entity_id: sensor.owm_pressure
      unit_of_measurement: 'mmHg'
      friendly_name: "OWM Pressure"
      value_template: >
        {% if states.sensor.owm_pressure %}
          {{ (states('sensor.owm_pressure') | float / 1.33322) | round(0) }}
        {% endif %}

I can’t tell if you want it to set it to undefined or not…

- platform: template
  sensors:
    owm_pressure_mmhg:
      entity_id: sensor.owm_pressure
      unit_of_measurement: 'mmHg'
      friendly_name: "OWM Pressure"
      value_template: >
        {% if states.sensor.owm_pressure.state is defined %}
          {{ (states('sensor.owm_pressure') | float / 1.33322) | round(0) }}
        {% else %}
          0
        {% endif %}

This template will never have ‘undefined’. But you’ll get 0 spikes on startups.

Thank you for trying to help, but it’s not solution.
I’ve already have zero values of that sensor for undefined states of initial sensor.
I need to have undefined values.

Ok then, try this

- platform: template
  sensors:
    owm_pressure_mmhg:
      entity_id: sensor.owm_pressure
      unit_of_measurement: 'mmHg'
      friendly_name: "OWM Pressure"
      value_template: >
        {% if states.sensor.owm_pressure.state is defined %}
          {{ (states('sensor.owm_pressure') | float / 1.33322) | round(0) }}
        {% else %}
          undefined
        {% endif %}

Just a question, why do you need to have undefined values?

For correct drawing of graphs:

1 Like

Ah ok, give the new template a whirl. If that doesn’t work, we may need to try other values in that spot like None.

I’ve looked in the sensor code: alas, at the now there is no way to set an undefined value. :frowning:

Thank you for trying to help…

You can use the first template in conjunction with the filter component to remove fliers. I.E. the zeros.

use the outlier filter. Then only track the filter sensor.

I had the same problem and found 2 solutions:

- platform: template
  sensors:
    owm_pressure_mmhg:
      entity_id: sensor.owm_pressure
      unit_of_measurement: 'mmHg'
      friendly_name: "OWM Pressure"
      value_template: >
        {% if is_state('sensor.owm_pressure', 'unknown') %}
          {{ states('sensor.owm_pressure') }}
        {% else %}
          {{ (states('sensor.owm_pressure') | float / 1.33322) | round(0) }}
        {% endif %}
      device_class: pressure

and

- platform: template
  sensors:
    owm_pressure_mmhg:
      entity_id: sensor.owm_pressure
      unit_of_measurement: 'mmHg'
      friendly_name: "OWM Pressure"
      value_template: "{{ (states('sensor.owm_pressure') | float / 1.33322) | round(0) }}"
      availability_template: "{{ not is_state('sensor.owm_pressure', 'unknown') }}"
      device_class: pressure

When I opened this topic, there was no availability_template field in the sensor. :slight_smile:
Subsequently, I initiated its addition to the sensor (though my version of the logic of this field turned out to be worse than the version from another participant).

I’m also added device_class and example without availability_template. I just left a solution for people like me who will find this question but without a solution :slightly_smiling_face:
You can mark this post as solved (if you agree with my solutions).