Automation to copy one sensor state value to another sensor state value

I have built a water flow monitor hooked up to a rPi which sends flow rate information to Home Assistant using MQTT, and where an MQTT sensor picks it up. This all works well.

Actual flows are intermittent, but the watch dog on the rPi sends periodic MQTT status keepalives where flow rate is often zero. This means that flow rate values are regularly overwritten, and most of the time the mqtt sensor state value is zero.

Unless there’s a better way, I want to build an automation which triggers when the mqtt flow sensor is non zero, and where the action is to copy the state value from the mqtt sensor to a template sensor, such that the template sensor indicates the last non zero flow rate. (Deviation from a baseline helps me identify leaks.)

Building the trigger is easy enough, but how do I create the action to copy the mqtt sensor state value to the template sensor state in the automation?

Thank you!!!

You don’t need an automation for this. It can be done in the template sensor.

  - platform: template
        friendly_name: "Non zero flow"
        unit_of_measurement: 'L/h' # or whatever
        value_template: >
          {% if states('sensor.your_mqtt_flow_sensor_here')|float > 0 %}
            {{ states('sensor.your_mqtt_flow_sensor_here')|float }}
          {% else %}
            {{ states('sensor.non_zero_flow')|float }}
          {% endif %}
1 Like

WOW!! Perfect!!! Implemented and working!!! Thank you so much.

I guess I finally need to learn how to use templates, but I have a long way to go! eg what is the purpose of the “else” condition:

{{ states('sensor.non_zero_flow')|float }}

Apologies for being such a noob!

Thanks again.

Turning that into pseudo-code:

if [he flow isn't zero
    Update the sensor with the flow
    Just keep the last value

The sensor has to update, this ensures that it won’t update with zero if there’s no flow.

1 Like