Avoid esp home sensor displaying status "unavailable"

I created a sensor like this:

dallas:
  - pin: D1 #GPIO5 
    update_interval: 5s

sensor:
- platform: dallas
  index: 0
  name: "Dallas Temperature"
  id: dallas_temperature

Sometimes the sensor returns “unavailable” for one cycle what I want to prevent.
For a Home Assistant sensor I found a solution like this:

For Esp Home I found this discussion:

So, how I can create a trigger based sensor directly in esp home which get’s only updated if the new value of original sensor isn’t “unavailable” or “unknown” ?

My idea is to create an additional template sensor and only return a new value if the original sensor isn’t “unknown” or “unavailable”:

But the syntax seems to be wrong… Can anybody help how I can check the status for “unknown” and “unavailable” ?

- platform: template
  id: dallas_temperature_helper
  name: "Dallas Temperature Helper"
  unit_of_measurement: '°C'
  icon: mdi:thermometer
  lambda: |-
    if (id(dallas_temperature).state == 'unknown')
    {
      return {};
    } 
    else 
    {
      return id(dallas_temperature).state;
    }

To get the new value from my temperature sensor I updatet this sensor like this:

- platform: dallas
  index: 0
  name: "Dallas Temperature"
  id: dallas_temperature
  on_value:
    - sensor.template.publish:
          id: dallas_temperature_helper
          state: !lambda 'return id(dallas_temperature).state;'

This is a working solution for me, so now I provide the temperature to my additional sensor only if the original sensor is in a range.

- platform: dallas
  index: 0
  name: "Dallas Temperature"
  id: dallas_temperature
  on_value:
    - if:
        condition:
          - sensor.in_range: #avoid "unavailable" / "unknown" status
              id: dallas_temperature
              above: -100
              below: 100
        then:
          - sensor.template.publish:
              id: dallas_temperature_helper
              state: !lambda 'return id(dallas_temperature).state;'
- platform: template
  id: dallas_temperature_helper
  name: "Dallas Temperature Helper"
  unit_of_measurement: '°C'
  icon: mdi:thermometer
  on_value:
    ## do something

Anyhow, I would like to check for “unknown” and “unavailable”, does anybody know how to check for this ?

You should be able to use isnan() in a lambda function. I haven’t tested this, but off the top of my head it would go something like this:

- platform: dallas
  index: 0
  name: "Dallas Temperature"
  id: dallas_temperature 
  on_value:
    lambda: |-
      if (!(isnan(x))) {
        id(dallas_temperature_helper).publish_state(x);
      }

The x variable contains the current value of the sensor.

Thanks :slight_smile:

sensor:
- platform: dallas
  index: 0
  name: "Dallas Temperature"
  id: dallas_temperature
  unit_of_measurement: '°C'
  icon: mdi:thermometer
  accuracy_decimals: 1
  on_value:
    - lambda: |-
        if (!(isnan(id(dallas_temperature).state))) 
        {
          return id(dallas_temperature_helper).publish_state(id(dallas_temperature).state);
        }
- platform: template
  id: dallas_temperature_helper
  name: "Dallas Temperature Helper"
  unit_of_measurement: '°C'
  icon: mdi:thermometer
  on_value:
    ## do something