mekaneck
(Rick Auch)
May 12, 2024, 4:53am
12
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
tom_l
May 12, 2024, 5:13am
14
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.
Did not mention this earlier because I was stuck at unknown
TheFes
(The Fes)
May 12, 2024, 9:05am
17
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.
TheFes
(The Fes)
May 12, 2024, 2:16pm
20
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
Have you ever thought about how to segregate weights of say 5-10pax?
TheFes
(The Fes)
May 12, 2024, 8:23pm
23
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.
TheFes
(The Fes)
May 13, 2024, 4:34am
25
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 %}
TheFes
(The Fes)
June 4, 2024, 9:22am
28
What’s the current state of sensor.impedanz_user1
it is “unknown”
and sensor.mi_body_composition_scale_acf7_impedance is 425
i did some restarts …
TheFes
(The Fes)
June 4, 2024, 10:51am
30
The last measured weight is probably either below 93, or above 113, so it wants to use the current state of the sensor itself.
However, as that is unknown
it doesn’t accept that.
This will resolve itself after the person weighing above 93 and below 113 used the scale.
TheFes
(The Fes)
June 4, 2024, 10:52am
31
Can you post your full code?
it needs to be a trigger based template sensor, you can’t use a template helper for this.
hi. i have it all in gui as template sensors - how should i post as code?
i copied it out:
impedanz_user1:
{% 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 %}
impedanz_user2
{% set weight = states('sensor.mi_body_composition_scale_acf7_mass') | float %}
{% if 80 <= weight <= 92.99 %}
{{ states("sensor.mi_body_composition_scale_acf7_impedance") }}
{% else %}
{{ states("sensor.impedanz_user2") }}
{% endif %}
gewicht_user1
{% set weight = states('sensor.mi_body_composition_scale_acf7_mass') | float %}
{% if 93 <= weight <= 112.99 %}
{{ states("sensor.mi_body_composition_scale_acf7_mass") }}
{% else %}
{{ states("sensor.gewicht_user1") }}
{% endif %}
gewicht_user2
{% set weight = states('sensor.mi_body_composition_scale_acf7_mass') | float %}
{% if 80 <= weight <= 92.99 %}
{{ states("sensor.mi_body_composition_scale_acf7_mass") }}
{% else %}
{{ states("sensor.gewicht_user2") }}
{% endif %}