See the README: The with_unit helper returns a Pint Quantity. The helpers to_unit, from_unit and without_unit return the plain number. I suggest to do the conversion to the desired unit at the very end and use Pint Quantity objects for any calculations.
So in your case:
- Line 1: Provide ‘states.sensor.total_energy_kwh’ to
with_unit helper. This helper recognizes the text as a sensor name, fetches the value and unit from that entity and returns a Pint Quantity (which results in value + unit as text when “handed back to HomeAssistant”
- Line 2+3: Provide ‘states.sensor.total_energy_kwh’ to
to_unit helper. This helper recognizes the text as a sensor name, fetches the value and unit from that entity, transforms to ‘MWh’ and ‘Wh’ respectively and returns a plain number (without unit) which is handed over to with_unit helper. That last helper in the pipeline does not have any information about the unit anymore (because to_unit stripped away the unit information) and hence returns dimensionless as the unit
Pint Quantity objects should not be used for further processing in HomeAssistant - only within the Jinja2 expression itself. The result of a Jinja2 expression should typically be a pure (dimensionless) number. If the result of your expression is a Pint Quantity object, then this object will be transformed into a text consisting of the value and the unit. As mentioned in the README on GitHub, the helpers to_unit, from_unit and without_unit return plain numbers while with_unit returns a Pint Quantity object.
While using with_unit is not necessary (because it is called internally for text values), I believe that explicity using with_unit is a cleaner way (i.e. explicity transforming to a Pint Quantitiy).
I can’t help myself, but pipes are a little bit clumsy to read for me. That’s why I would prefer the following expression:
{{ to_unit('states.sensor.total_energy_kwh', 'Wh') }}
This expression transforms your sensor to Wh (no matter what unit it initially had) and returns a simple (dimensionless) number similar to what states(....) would return.
If you need to do more calculations with the entity value (and don’t want to loose the unit information) then use with_unit.
Example:
{{ (with_unit('states.sensor.total_energy_kwh', 'Wh') + with_unit(1.2, 'kWh')) | without_unit }}
The result is a pure number (without unit) reflecting the value in Wh. However, a much clearer way would be the following:
{{ (with_unit('states.sensor.total_energy_kwh') + with_unit(1200, 'Wh')) | to_unit('MWh') }}
In this example it is quite clear that the result is in MWh.
If you don’t like pipes, then use the following:
{{ to_unit(with_unit('states.sensor.total_energy_kwh') + with_unit(1200, 'Wh'), 'MWh') }}
However, in this case I would prefer the pipe syntax.
When dealing with Pint Quantity objects, you don’t need to care about the units. So you can safely use the following expression to test if your total energy exceeds 1 MWh (no matter what the actual unit of your sensor is):
{{ with_unit('states.sensor.total_energy_kwh') > with_unit(1, 'MWh') }}