Template sensor showing "unknown"

Yes, I step on the scale each time I reload the YAML. See OP for my full script

omg… does the 1st trigger stops the 2nd trigger? how do i define multiple templates then?

Your code looks fine, so just an idea: since you don’t define a unique_id; if you change the details of the sensor and reload the yaml, HA will think you’ve defined a new sensor. If you didn’t change the name, HA will add an _2 or _3 etc to the entity ID to avoid conflicting with the first one.

So the scenario is:

  1. you created the YAML sensor, unknowingly made a mistake in the template, and reloaded the YAML.
  2. Saw that the sensor called sensor.v_my_weight2 was “unknown” so went back into the code and fixed the template and reloaded the yaml
  3. Looked at the original sensor entity ID and saw that sensor.v_my_weight2 was still unknown without realizing that you should instead be looking at an entitiy ID that is sensor.v_my_weight2_2

Moral of the story is to define a unique_id

Thanks, it seems better with the unique ID.

Is there a way using templates with triggers to separate 2 sensors based on 80?
i.e. below, go to sensor1, equal or above, go to sensor2

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.bluetooth_proxy_miscale_weight
      - platform: template
        value_template: "{{ states('sensor.bluetooth_proxy_miscale_weight')|float > 80 }}"
    sensor:
      - name: v_my_weight
        unique_id: v_my_weight
        state: "{{ states('sensor.bluetooth_proxy_miscale_weight') }}"
        unit_of_measurement: kg
        icon: mdi:scale-bathroom

This works now. Thank you!

That template isn’t going to achieve anything, but I’m not sure what your end goal is either.

If you want to track every weight that is below 80 on one entity, and every weight that is above (or equal to 80) on another entity, this would be one option:

  - trigger:
      - platform: state
        entity_id: sensor.bluetooth_proxy_miscale_weight
        not_to: 
          - unavailable
          - unknown
        variables:
          threshold: 80
    sensor:
      - name: Weight Eq and Above Threshold
        unique_id: f700e52a-1304-4d47-8bea-5b2c8f4049fe
        unit_of_measurement: kg
        device_class: weight
        state: >
          {{ trigger.to_state.state if trigger.to_state.state | float(0) >= threshold else this.state }}
      - name: Weight Below Threshold
        unique_id: 42e1e5c9-1476-4e8d-9e8d-9071021ebb4b
        unit_of_measurement: kg
        device_class: weight
        state: >
          {{ trigger.to_state.state if trigger.to_state.state | float(0) < threshold else this.state }}

Well, yours is certainly shorter (nicer), but mine did work, if the weight was below 80, it did not update v_my_weight. I planned to duplicate that for another that is below 80.

Anyway, thanks for your script, there is a lot of new stuff such as this.state

There is no need for a triggered template sensor. This is redundant:

Templates are already monitored for state changes of the included sensors.

This will do exactly the same thing:

template:
  - sensor:
      - name: Weight Eq and Above Threshold
        unique_id: f700e52a-1304-4d47-8bea-5b2c8f4049fe
        unit_of_measurement: kg
        device_class: weight
        state: >
          {% set weight = states('sensor.bluetooth_proxy_miscale_weight') | float %}
          {{  weight if weight >= 80 else this.state }}
        availability: "{{ has_value('sensor.bluetooth_proxy_miscale_weight') }}"

      - name: Weight Below Threshold
        unique_id: 42e1e5c9-1476-4e8d-9e8d-9071021ebb4b
        unit_of_measurement: kg
        device_class: weight
        state: >
          {% set weight = states('sensor.bluetooth_proxy_miscale_weight') | float %}
          {{  weight if weight < 80 else this.state }}
        availability: "{{ has_value('sensor.bluetooth_proxy_miscale_weight') }}"

when I left it out, it did not seem to register, let me try again.

UPDATE

Removing the 3 lines as suggested broke the template

      - platform: state
        entity_id:
          - sensor.bluetooth_proxy_miscale_weight

Ok, finally the “code” is getting short enough for my definition of elegance.

This 80 threshold is to separate different individuals by weight. On top of this, I wanted to calculate BMI. :slight_smile:

Did not mention this earlier because I was stuck at unknown

This is how I did it, I trigger on impedance, as that changes after the scale fully processed it’s weighing

https://github.com/TheFes/HA-configuration/blob/978224ab2dd88eba84f19fa02e087ffd08bc7dc3/include/template/trigger/xiaomi_scale_person.yaml

The min and max weights are managed in input numbers

I use the custom integration and card to show BMI and other statistics

My weights are send via BLE to a esp32 Bluetooth proxy. The only issue was i wanted short code n not an automation.

Why do you need to account for impedence? My older unit sends weight automatically.

Mine sends both weight and impedance. The impedance is used for statistics like muscle mass.
The weight sensor is updated first and then the impedance sensor. By triggering on the impedance sensor I ensure I combine both weight and impedance of the same person. Otherwise it could be that I use the weight of one person, and the impedance of the person who used the scale earlier

Ok, I got what you mean. My unit only sends weight, I will remember your solution when I upgrade. Thanks :slight_smile:

Have you ever thought about how to segregate weights of say 5-10pax?

Don’t think that’s possible unless they are really having different weights

My idea was to have 10 phystical MQTT buttons, press yours before stepping on. It would be even better if I don’t have to add buttons for each new person.

I do see possibilities with those buttons. Without them it will only work if there is no overlap in weights

hi, is there a chance to get this fixed - i think i dont understand the solution:

i have 2 users
both with impedance and weight:

{% set weight = states(‘sensor.mi_body_composition_scale_acf7_mass’) | float %}
{% if 93 <= weight <= 112.99 %}
{{ states(“sensor.mi_body_composition_scale_acf7_impedance”) }}
{% else %}
{{ states(“sensor.impedanz_user1”) }}
{% endif %}

Sensor None has device class ‘None’, state class ‘measurement’ unit ‘ohm’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘unknown’ (<class ‘str’>)

Test this code…

{% set weight = states('sensor.mi_body_composition_scale_acf7_mass') | float(0) %}
{% if 93 <= weight <= 112.99 %}
{{ states('sensor.mi_body_composition_scale_acf7_impedance') }}
{% else %}
{{ states('sensor.impedanz_user1') }}
{% endif %}