rrooggiieerr / homeassistant-okokscale

Home Assistant integration for OKOK Scales
Apache License 2.0
10 stars 0 forks source link

Sensor gives a non numeric value in template sensor #4

Closed Mattdejong closed 5 months ago

Mattdejong commented 5 months ago

The code:

sensor:

output:

Sensor x has device class 'weight', state class 'measurement' unit 'kg' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'sensor: - platform: template sensors: scale_weight: unit_of_measurement: "kg" value_template: "16.1"' (<class 'str'>)

The scale gives a string back instead of a float. I want to make a template sensor because the scale goes in a deepsleep and gives back the value unavailable.

rrooggiieerr commented 5 months ago

Hi Matt,

Sensors in HA always give string values, even if they're numeric values. To use them in templates you need to cast them back to their original type like so: states('sensor.okok_scale_b2c5_weight') | float

Frome the HA documentation: states can also be used as a function, states(entity_id, rounded=False, with_unit=False), which returns the state string (not the state object) of the given entity, unknown if it doesn’t exist, and unavailable if the object exists but is not available.

Mattdejong commented 5 months ago

Even with the float it still gives: Sensor None has device class 'weight', state class 'measurement' unit 'kg' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'template: sensor: - name: "Gewicht Schaal" unit_of_measurement: "kg" state: 77.6' (<class 'str'>).

rrooggiieerr commented 5 months ago

I think the error actually applies to your own template sensor, and not to the scale sensor.

You also to use the legacy template sensor format https://www.home-assistant.io/integrations/template/

Your value_template is missing the : and the value is between two kind of brackets and ".

The modern format should be something like:

template:
  - sensor:     
    - name: ...
      unique_id: ...
      unit_of_measurement: "kg"
      state: "{{ states('sensor.okok_scale_b2c5_weight') | float }}"

You can also try out your template in the Developer Tools > Template Looking up the sensor state in Developer Tools > States might also give you some clues

rrooggiieerr commented 5 months ago

Personally I'm using automations to detect who is on the scale and calculate the BMI in the same time:

alias: Scale - Detect Rogier
description: "Detects if Rogier is on the scale and if so sets Rogier's weight to the current scale weight and calculates the BMI"
trigger:
  - platform: state
    entity_id:
      - sensor.scale_weight
    to: null
condition:
  - condition: state
    entity_id: person.rogier
    state: home
  - condition: template
    value_template: >-
      {{ not is_state('sensor.scale_weight', 'unavailable') and
      float(states('input_number.rogier_weight')) * 0.9 <
      float(states('sensor.scale_weight')) <
      float(states('input_number.rogier_weight')) * 1.1 }}
action:
  - service: input_number.set_value
    data:
      value: "{{ trigger.to_state.state }}"
    target:
      entity_id: input_number.rogier_weight
  - service: input_number.set_value
    data:
      value: >-
        {{ (float(trigger.to_state.state) /
        (((float(states('input_number.rogier_height')) / 100) ** 2))) | round(1)
        }}
    target:
      entity_id: input_number.rogier_bmi
mode: single

You can create this automation for every person in your household. The automation compares the scale weight with the persons weight and if it differs less than 10% it is assumed that this person is on the scale. It then sets the persons weight to the new value and calculates the BMI.

If the difference is out of the 10% range the scale weight is ignored for this person, but might match for an other person.

This only works if the persons in your household differ in weight by at least 10% from each other. Otherwise you have to tweak the template condition 0.9 and 1.1 which defines this 10%

It uses 3 number helpers for each person, one for weight, one for BMI and one for the persons length needed to calculate the BMI. You cold hard code the length in the automation to get rid of that one helper.

Mattdejong commented 5 months ago

Hmmmm, Looks interesting I will take a look into it. I want also want to Multiply the state of the scale, but it has more to do with the protein intake for m2. You sure are a help 2 me. im really still a beginner. I don't know everything, and the frustrating part is that: every solution that is on the internet says it works with this methode, even chatgpt. But I can't get it to work. I will open a home assistant question if its not a bug, thanks for the response

Edit I got it to work, for the UI set up you don't have to use the:

template:

only the states attribute