Scaling MQTT Values

Having Resolved an issue as per this thread, Afterbuner MQTT to HomeAssistant - Configuration - Home Assistant Community (home-assistant.io)

I would now like to change some of the Usages Values received from the MQTT Broker from ml to Litres. Simply divide them by 1000.

Have tried a few things with no success, & not sure if some of the Synax I see in examples is still current.

Bit of code returning the Usage Value is this,
Including some commented out failed attempts at getting the Actual Value in the First place.
Still unsure on which of those Syntax’s are wrong, & which is best current practice.

mqtt:
sensor:

  - name: Afterburner Current Usage
    unique_id: afterburner_current_usage
    state_topic: "AfterburnerFF4E3C/sts/FuelUsage"
    value_template: "{{ value }}"
#    value_template: "{{ states ( value ) | float * 0.001 }}"
    unit_of_measurement: mL
    state_class: total_increasing

  #  - name: Afterburner Current Usage
  #    unique_id: afterburner_current_usage
  #    state_topic: "AfterburnerFF4E3C/sts"
  #    value_template: "{{ value_json.FuelUsage }}"
  #    unit_of_measurement: mL
  #    state_class: measurement

  #  - name: Afterburner Current Usage
  #    unique_id: afterburner_current_usage
  #    state_topic: "AfterburnerFF4E3C/sts"
  #    value_template: '{{ value_json["FuelUsage"] }}'
  #    unit_of_measurement: mL
  #    state_class: measurement
  #    unit_of_measurement: "°C"


Can anyone show me the current Syntax to return a Current Value of 8,542.5ml to 8.542 Litres?

Thanks

Phil.

mqtt:
sensor:
  - name: Afterburner Current Usage
    unique_id: afterburner_current_usage
    state_topic: "AfterburnerFF4E3C/sts/FuelUsage"
    value_template: "{{ value * 0.001 }}"
    unit_of_measurement: L
    state_class: total_increasing

Tried that just now & it’s Returning Unknown.
Remove the “* 0.001” & the Value Returns.

As in Line 4 returns the Value, but Line 5 returns Unknown.

  - name: Afterburner Current Usage
    unique_id: afterburner_current_usage
    state_topic: "AfterburnerFF4E3C/sts/FuelUsage"
#    value_template: "{{ value }}"
    value_template: "{{ value * 0.001 }}"
#    value_template: "{{ states ( value ) | float * 0.001 }}"
    unit_of_measurement: L
    state_class: total_increasing

Double Checked it for Typo’s.

Ok the value must be a string instead of a number, try this:

value_template: "{{ value|float(0) * 0.001 }}"

Thanks,

That Works & Returns 8.5.
I tried Float(1) & 2 to see if I could get it to show 8.54, but both returned just the one decimal.

Is it possible to make it return those extra decimals?

Phil.

Also tried applying it to this block as well.
The Total Lifetime Usage of the Unit.

Unlike the other block, this one now returns 491 Litre with a value of .7781 for the decimal portion.

Previously it showed 491,7778.1 ml

Total Lifetime Consumption woild be best with just one Decimal,
but Current Consumption, (What’s been used since the 10 litre tank was refilled),
would be better with 2 decimals, as the controller is set to shutdown at usage of 9.85 Litres.

  - name: Afterburner Consumption (Total)
    unique_id: afterburner_consumption
    state_topic: "AfterburnerFF4E3C/sts/TotalFuelUsage"
    value_template: "{{ value|float(0) * 0.001 }}"
    unit_of_measurement: L
    state_class: total_increasing

The number in the brackets is not the precision. It is the default value if the sensor becomes unknown or unavailable. Without this the sensor will generate errors.

As your sensor has a unique id the number of decimals can be set from the UI.

Click the entity in your dashboard, click the cog icon top right of the pop-up. Set the Display Precision option to whatever you want.

Ahh,

Understand better now & have it all displaying how I want it.

Thanks for the Help!

1 Like