Adding two units of measurement from one sensor

Hello, I am trying to configure my homebrew scales to display Kg of beer left inside the Keg but also X ‘pints’ left too. Is this possible to have two output values from one ESPhome HX711 sensor. With the second value having a multiplication in the YAML to change units of measurement from Kg to pints?

I currently have the YAML file set up to display unit of measurement in ‘pints’ only. However I would like to change this to Kg and have pints as a second line item on the HA card.

code for the HX711 YAML as follows:

sensor:
  - platform: hx711
    name: "HX711 Value"
    dout_pin: D1
    clk_pin: D2
    gain: 128
    update_interval: 5s
    unit_of_measurement: pints
    accuracy_decimals: 2
    filters:
      - calibrate_linear:
          - -98440 -> 0
          - -581202 -> 23042
      - multiply: 0.00176056338028169

I would like to change the code to the following to display Kg. but how can I maintain the ‘pints’ line too. They will be related to each other therefore I just need to somehow get the YAML to read the Kg value and then do a multiplication to convert to ‘pints’.

sensor:
  - platform: hx711
    name: "HX711 Value"
    dout_pin: D1
    clk_pin: D2
    gain: 128
    update_interval: 5s
    unit_of_measurement: Kg
    accuracy_decimals: 2
    filters:
      - calibrate_linear:
          - -98440 -> 0
          - -581202 -> 23.042

Im quite new to all of this and don’t understand coding very well so please explain in simple terms if possible.

Thanks.

You can only have one unit per sensor. So use template sensors for your outputs. e.g.

sensor:
  - platform: hx711
    name: "HX711 Raw Value"
    internal: true # i.e. do not publish this value to home assistant
    id: hx711_raw
    dout_pin: D1
    clk_pin: D2
    gain: 128
    update_interval: 5s
    unit_of_measurement: pints
    accuracy_decimals: 2
    on_value:   # update your template sensors whenever a value is received
      - then:
          - component.update: weight
          - component.update: pints

  - platform: template
    name: "Beer Weight"
    id: weight
    unit_of_measurement: Kg
    lambda: return id(hx711_raw).state;   # get the hx711 value
    filters:
      - calibrate_linear: # adjust it to weight in kgs
          - -98440 -> 0
          - -581202 -> 23.042
  - platform: template
    name: "Beer Pints"
    id: pints
    unit_of_measurement: Pints
    lambda: return id(hx711_raw).state;  # get the hx711 value
    filters:
      - calibrate_linear: # adjust it to pints.
          - -98440 -> 0
          - -581202 -> 40.567

For the weight I have removed the multiply filter by adjusting the calibrate value. This is one less computation your ESP has to do (multiplication by anything other than multiples of 2, 0, 1 or 0.5 is hard in binary).

I just got to try this and its perfect!!

Thank you so much, this is exactly what I wanted! You are a genius.

Hi @tom_l I need your help again please,

What you fixed for me above works but Ive no realised that its not giving me what I want as I had a slight oversight.

What I want is the volume of the liquid in the keg converted to pints minus the weight of the keg itself. (silly mistake really, apologies for wasting your time)

I have a sensor formula in my conf.yaml which is this:

template:   
    - sensor:
        - name: "Keg 1 Level"
          unique_id: 'kgl1'
          unit_of_measurement: "%"
          state: "{{ ((states('sensor.keg_scale_1_keg_weight')|float - states('input_number.keg_1_empty')|float ) / ((states('input_number.keg_1_full')|float - states('input_number.keg_1_empty')|float) / 100)) | round | int }}"

This then takes the weight from the scale (HX711 sensors) and removes the known weight of the keg to give a % left graph of the liquid inside.

What I need to give me a true pints of volume of liquid is something similar.

I need (‘sensor.keg_scale_1_keg_weight’ - ‘input_number.keg_1_empty’) x 1000 / 568

How do I write this in code?

Would it be:

template:   
    - sensor:
        - name: "Keg 1 Pints"
          unique_id: 'kgl1p'
          unit_of_measurement: "Pints"
          state: "{{ (((states('sensor.keg_scale_1_keg_weight')|float - states('input_number.keg_1_empty')|float) * 1000) / 568) | round | int }}"