Filter value of single sensor into two seprate new sensor values?

We have a litter robot 4 that takes the weights of our cats. I want to be able to graph the weight of the cats on separate charts and also throw away any bad data, when they are both in the litterbox or like half in it. I had this for one cat and then duplicated it for the other cta but it only worked for the first one in the yaml.

  - platform: template
    sensors:
      flint_pet_weight:
        value_template: >
          {% set x = states('sensor.litter_robot_4_pet_weight')|float %}
          {% if 8.8 <= x <= 10 %}
            {{ x }}
          {% else %}
            unknown
          {% endif %}
  - platform: statistics
    name: "Flint Weight"
    entity_id: sensor.flint_pet_weight
    state_characteristic: mean
    max_age:

I then tried something like this but this doesnt work for either of them

  - platform: template
    sensors:
      flint_pet_weight:
        value_template: >
          {% set x = states('sensor.litter_robot_4_pet_weight')|float %}
          {% if 8.8 <= x <= 10 %}
            {{ states('sensor.flint_pet_weight') }}
          {% elif 7 <= x <= 8.7  %}
            {{ states('sensor.flora_weight') }}
          {% else %}
            unknown
          {% endif %}
  - platform: statistics
    name: "Flint Weight"
    entity_id: sensor.flint_pet_weight
    state_characteristic: mean
    max_age:
      hours: 168
  - platform: statistics
    name: "Flora Weight"
    entity_id: sensor.flora_weight
    state_characteristic: mean
    max_age:
      hours: 168

Any ideas thanks?!

I would try it with two trigger based template sensors, one for each kitty.


template:

  - trigger:
    - platform: numeric_state
      entity_id: sensor.litter_robot_4_pet_weight
      above: 8.79
      below: 10.0
    sensor:
      - name: "Flint's Weight"
        unique_id: test_202302271727
        icon: mdi:cat
        state: '{{ trigger.to_state.state }}'
        attributes:
          history: |-
            {% set current = this.attributes.get('history', []) %}
            {% set new = [{
            'weight': trigger.to_state.state,
            'at': now().strftime('%T')
            }] %}
            {{ (new + current)[:6] }}

The state only changes, when the weight is within the specified range. It requires that the scale resets quickly enough (I know from our cats that they go to the toilet one right after the other).

The attributes part is only a nice-to-have. You can drop it. I stumbled upon it a while ago here. The [:6] means that the last 6 state changes will be recorded.

  - platform: template
    sensors:
      flint_pet_weight:
        unit_of_measurement: lb # I hope this is not kg :)
        availability_template: "{{ states('sensor.litter_robot_4_pet_weight')|is number and 8.8 < states('sensor.litter_robot_4_pet_weight') <= 10 }}"
        value_template: "{{ states('sensor.litter_robot_4_pet_weight')|float(0) }}"
      flora_pet_weight:
        unit_of_measurement: lb
        availability_template: "{{ states('sensor.litter_robot_4_pet_weight')|is number and 7 < states('sensor.litter_robot_4_pet_weight') <= 8.7 }}"
        value_template: "{{ states('sensor.litter_robot_4_pet_weight')|float(0) }}"

  - platform: statistics
    name: "Flint Weight"
    entity_id: sensor.flint_pet_weight
    state_characteristic: mean
    max_age:
      hours: 168
  - platform: statistics
    name: "Flora Weight"
    entity_id: sensor.flora_weight
    state_characteristic: mean
    max_age:
      hours: 168

Note it is not recommended that you use this old template sensor format. The new format would look like this:

configuration.yaml

template:
  - sensors:
      - name: Flint Pet Weight
        unit_of_measurement: lb # I hope this is not kg :)
        availability: "{{ states('sensor.litter_robot_4_pet_weight')|is number and 8.8 < states('sensor.litter_robot_4_pet_weight') <= 10 }}"
        state: "{{ states('sensor.litter_robot_4_pet_weight')|float(0) }}"
      - name: Flora Pet Weight
        unit_of_measurement: lb
        availability: "{{ states('sensor.litter_robot_4_pet_weight')|is number and 7 < states('sensor.litter_robot_4_pet_weight') <= 8.7 }}"
        state: "{{ states('sensor.litter_robot_4_pet_weight')|float(0) }}"

sensors.yaml

  - platform: statistics
    name: "Flint Weight"
    entity_id: sensor.flint_pet_weight
    state_characteristic: mean
    max_age:
      hours: 168
  - platform: statistics
    name: "Flora Weight"
    entity_id: sensor.flora_weight
    state_characteristic: mean
    max_age:
      hours: 168
1 Like

Thanks this seems to work perfectly!

Glad to hear! And if not, test Tom’s clever approach.

any way to get this a bit more dynamic VS setting the cats weight range? like I have two cats one weight 5lb and the other 6lb but are growing. is there away to say take a snapshot of the two current values and then match based of a small growth in the number?

Sure, use a couple of input numbers for each cat’s weight range.