Template sensor showing "unknown"

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

I configured a template sensor above… but it displays as unknown even though {{ states("sensor.bluetooth_proxy_miscale_weight") }} shows a number

Any ideas?

Has sensor.bluetooth_proxy_miscale_weight changed from below 80 to above 80?

That is when it will trigger.

1 Like

The state of sensor.bluetooth_proxy_miscale_weight must go from below 80 to above 80 in order to trigger. If it’s already above 80 nothing will happen.

Ignore me, ninja’d by Tom

1 Like

No wonder it failed, it did not cross 80, go from below to above 80.

But I have another which also led to unknown

  - trigger:
      - platform: state
        entity_id: sensor.bluetooth_proxy_miscale_weight
    sensor:
      - name: v_my_weight2
        state: "{{ states('sensor.bluetooth_proxy_miscale_weight') }}"
        unit_of_measurement: kg
        icon: mdi:scale-bathroom

has the state of sensor.bluetooth_proxy_miscale_weight changed since you added the template sensor? The state needs to change in order for the trigger to trigger.

… inb4 Tom … :slight_smile:

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?