Adding a value template to an MQTT Sensor

I have an Aquaro water flow sensor I’m trying to integrate and am having an issue trying to convert its data from liters to gallons. It sends a cumulative total as an MQTT message, and I set it up in the GUI like this:

Works great; but it’s in Liters.

So, I got a value template from Aquaro, and did this:

But that doesn’t work - All I see after I do this is either “unknown” or zeros.

Can someone give me some direction here?

The value_template defines the state value for the entity, so that template is kind of nonsense… it’s basically saying “The state of this sensor is the state of this sensor times 0.264…”

IIRC, if the topic supplies a direct value instead of a json object, you should be using the value variable.

{{ value | float(0) * 0.264172 }}

I’m grasping at straws here and I’m sure there are others much more knowledgeable than I. Of course you RTFM and this FM and this FM

What’s coming from the MQTT broker is text and needs to be converted to float, hence the I float(0). But is states needed?

The template editor could speed up your thrashing (see: settings–>developer tools–>templates)

So try this

  • {{ (‘sensor.aquaro_sensor_water_total’) ! fload(0) * 0.264172 }}
    or
  • {{ ‘sensor.aquaro_sensor_water_total’ ! fload(0) * 0.264172 }}
    or
  • {{ (states.sensor.aquaro_sensor_water_total.state) | float(0) * 0.264172 }}
    or
  • {{ states.sensor.aquaro_sensor_water_total.state | float(0) * 0.264172 }}