Template: Pull Sensor Data, if in specific range

Hello!

Is there a way to create template sensors that only pull data from a sensor if it meets a specific criteria?

Example: Sensor measures temperature. Create two template sensors, one pulls only data if it’s above 100F and the other only pulls data if below 100F.

What would you want the sensor to return if the thermometer’s value is outside your target range?

I would want it to return the sensor data. So here is the real use case.

We have a bluetooth scale that sends my ESP32 weight every time someone uses it. I want to create two different template sensors, one for me and one for my wife. So under 130lbs would report that sensor data to my wife’s sensor and over 200lbs would report to my sensor. It doesn’t necessary need to be a range since our weight difference is substantial.

Original State-based sensor
template:
  - sensor:
      - name: Chris Weight
        unit_of_measurement: 'lbs'
        state_class: measurement
        state: >
          {% set current = this.state | default(0, 1) %}
          {% if states('sensor.example_scale') | float > 190 %}
          {{ states('sensor.example_scale') }}
          {% else %}{{ current }}{% endif %}
        availability: "{{ 'sensor.example_scale' | has_value }}"
      - name: Wife Weight
        unit_of_measurement: 'lbs'
        state_class: measurement
        state: >
          {% set current = this.state | default(0, 1) %}
          {% if  90 < states('sensor.example_scale') | float < 130 %}
          {{ states('sensor.example_scale') }}
          {% else %}{{ current }}{% endif %}
        availability: "{{ 'sensor.example_scale' | has_value }}"

EDIT: I think it would actually work better as a trigger-based sensor rather than state-based… that gives you the option of using the “for” configuration variable to let the scale reach a steady state, then the value will be set until the next trigger event.

template:
  - trigger:
      platform: numeric_state
      entity_id: sensor.example_scale
      above: 190
      for: "00:00:03"
    sensor:
      - name: Chris Weight
        unit_of_measurement: 'lbs'
        state_class: measurement
        state: >
          {{ trigger.to_state.state  }}      
  - trigger:
      - platform: numeric_state
        entity_id: sensor.example_scale
        above: 90
        below: 130
        for: "00:00:03"
    sensor: 
      - name: Wife Weight
        unit_of_measurement: 'lbs'
        state_class: measurement
        state: >
          {{ trigger.to_state.state }}
1 Like

Thank you!!

Spitballing here, but this might work for you. You would create two sensors, one for you and one for your wife that both use the same ESP32 device but only change value under certain circumstances:

  - name: "My Weight"
    unique_id: c4d4635d-7e82-41fd-acc8-acacf91fa397
    state: >-
      {% set curwt = float(states('sensor.my_weight') , 0) %}
      {% set newwt = float(states('sensor.esp32_weight'), 0) %}
      {% if newwt >= 200 %}
        {{ newwt | round(1) }}
      {% else %}
        {{ curwt | round(1) }}
      {% endif %}
1 Like

This makes a lot of sense. There are actually two sensors the scale provides, one is real-time and one is a final value. So your first template works perfectly, since I am using the final value sensor.

1 Like