How to include all MQTT values in a total sensor

This is not really a project but I just want to share some fall-pits i needed to tackle in creating a template sensor (which increases itself).

Ok, I needed to completely revise this topic because I slowly realised that you cannot use a template sensor which is increasing itself. So if you try to do it (like me) stop reading and struggle further :grinning:.

Situation: With an Arduino UNO + Proximity sensor: LJ12A3-4-Z/BY-5V and a simple home brewed sketch the number of water usage in liters is counted and after a certain time of inactivity a MQTT topic is published. A typical sequence of values is:
3, 7, 1, 3, 3, 3, 1. All the values should be included in the totals.

My tips to avoid fall-pits are:

  1. Do not try to do this directly with a template sensor but use first a input helper (later you can copy the result of this field to a template sensor if required)

  2. Include in your MQTT sensor settings “force_update: true”.
    Because HA will “see” successive identical values.

  3. Do not include in your trigger TO, FROM because with this HA will only look to state.state and the desired effect of “force_update: true” is gone.

  4. More a generic tip add always “unique_id” to any manual sensor to avoid the creation of a second sensors (_2).

For the most of you this already sounds obvious but for those NOOBS (like me) just starting this is hopefully helpful.

I found an alternative way for “force_update: true” for MQTT sensors this is a bit of a overkill here but perhaps useful in other situations:

json_attributes_topic: "mqtt/sensor"
json_attributes_template: >
    "random": "{{ range(1, 99999) | random }}"

Below the complete yamls if you want to test things:

# get inspired by thanks to:
# Easiest test for Windows is to use MQTT explorer.
# publish in topic "mqtt/sensor": {"interval":9,"liters":1,"timer":99,"treshold":600}
# Objective: a sequence of in example 3, 7, 1, 3, 3, 3, 1 should ALL be processed
# So not only                         3, 7, 1, 3,       1 
# for this you need in your mqtt sensor definition: force_update: true

  - unique_id: test_mqtt
    name: "Test MQTT"
    icon: mdi:water
    state_topic: "mqtt/sensor"
    value_template: "{{ value_json.liters }}"
    force_update: true # required to force update of identical values in a row
    device_class: water
    unit_of_measurement: L
    #  - topic: "mqtt/LWT"
    #    payload_available: "Online" # watch out might be case sensitive
    #    payload_not_available: "Offline"

# input_number for the initial input
# this field can also be use to set the (re)set value of the meter. 
    name: "test mqtt helper"
    min: 0
    max: 999999
    icon: mdi:water
    mode: box
    unit_of_measurement: L

# automation of all actions
- alias: mqtt test
  description: mqtt test
    - platform: state
        - sensor.test_mqtt
      # to: null  # do not use this
  condition: []
    - service: input_number.set_value
        value: >-
          {{ states('input_number.dwl_helper_test')|int +
          states('sensor.test_mqtt')|int }} 
        entity_id: input_number.dwl_helper_test
  mode: single