Conditional sensor templated dependent on input helper

I have a smart scale which I use to track multiple people’s weight. The weight is provided by the entity sensor.mi_body_composition_scale_c8f9_mass.

Now, Jane and Paul have a similar weight, so I cannot use only the weight to decide who’s standing on the scale. My idea was to have a button in the home assistant UI, which I can activate to enable weighing one person while the other person is weighed when the switch is disabled.

To this end, I added a boolean input helper (input_boolean.log_weight_paul) and made it accessible via a button into the UI. Then I set up the following sensor templates:

sensor:
  - platform: template
    sensors:
      weight_jane:
        friendly_name: "Weight Jane"
        unique_id: "sensor_weight_jane_79c2f3"
        device_class: weight
        value_template: >-
          {% set weight = states('sensor.mi_body_composition_scale_c8f9_mass') | float %}
            {% if (50 <= weight < 59) and is_state('input_boolean.log_weight_paul', 'off')%}
              {{ states("sensor.mi_body_composition_scale_c8f9_mass") }}
            {% else %}
              {% set state = states('sensor.weight_jane') %}
              {{ state | float if is_number(state) else '' }}
            {% endif %}
        unit_of_measurement: "kg"
        icon_template: mdi:weight-kilogram

  - platform: template
    sensors:
      weight_paul:
        friendly_name: "Weight Paul"
        unique_id: "sensor_weight_paul_79c2f4"
        device_class: weight
        value_template: >-
          {% set weight = states('sensor.mi_body_composition_scale_c8f9_mass') | float %}
            {% if (56 <= weight <= 65) and is_state('input_boolean.log_weight_paul', 'on') %}
              {{ states("sensor.mi_body_composition_scale_c8f9_mass") }}
            {% else %}
              {{ states("sensor.weight_paul") }}
            {% endif %}
        unit_of_measurement: "kg"
        icon_template: mdi:weight-kilogram

For some reason this is not working. When the switch is enabled (the boolean is true), the weight gets assigned to the Paul sensor as expected. However, once the switch is disabled the previous weight is assigned to Jane. Also when the switch is off it will be assigned to both.

Does anybody know what the issue is here? I guess I got something wrong in how these sensor templates work.

Try this:

sensor:
  - platform: template
    sensors:
      weight_jane:
        friendly_name: "Weight Jane"
        unique_id: "sensor_weight_jane_79c2f3"
        device_class: weight
        value_template: >
          {% set weight = states('sensor.mi_body_composition_scale_c8f9_mass') | float %}
            {% if (50 <= weight < 59) and is_state('input_boolean.log_weight_paul', 'off') %}
              {{ weight }}
            {% else %}
              {{ this.state }}
            {% endif %}
        unit_of_measurement: "kg"
        icon_template: mdi:weight-kilogram
        availability_template: "{{ has_value('sensor.mi_body_composition_scale_c8f9_mass') }}"
  - platform: template
    sensors:
      weight_paul:
        friendly_name: "Weight Paul"
        unique_id: "sensor_weight_paul_79c2f4"
        device_class: weight
        value_template: >
          {% set weight = states('sensor.mi_body_composition_scale_c8f9_mass') | float %}
            {% if (56 <= weight <= 65) and is_state('input_boolean.log_weight_paul', 'on') %}
              {{ weight }}
            {% else %}
              {{ this.state }}
            {% endif %}
        unit_of_measurement: "kg"
        icon_template: mdi:weight-kilogram
        availability_template: "{{ has_value('sensor.mi_body_composition_scale_c8f9_mass') }}"

Also you should really be using the new template integration for new template sensors.

Thanks, I’ll give it a try! As for the new template integration, I’ll have to look into that. The sensors are pretty old and the need for disambiguation has arisen only recently :wink:

After some measurements were taken it seems that it works only partly. When the switch is activated the weight is correctly assigned to Paul. However, when Jane is measured, both, the last state of Paul and her own weight are assigned to her.

On further usage it seems like whenever the person changes, like measuring Person A then B then A, every time one steps on the weight, he/she gets logged both, his/her current weight and the weight of the previous person as his/her own weight.