How can I use MQTT to process retained and new messages from a subscription?

I have the following sensor and automation.

When I use an MQTT client and subscribe to OpenZWave/1/node/11/instance/1/commandclass/99/# it happily spits out 5 retained messages in JSON format. Then let’s say after I subscribe, some events occur which causes 2 more messages to be sent to that subscription. I’d like to process each message, and count the number of messages. In the following I’m expecting to get 7 messages, but am only getting 1.

When I use any simple MQTT client I am getting 7 messages (the 5 retained and 2 new). What am I doing wrong?


  - platform: mqtt
    name: mqttlock
    state_topic: "OpenZWave/1/node/11/instance/1/commandclass/99/#"
    json_attributes_topic: "OpenZWave/1/node/11/instance/1/commandclass/99/value/#"
    value_template: "{{ value_json.TimeStamp }}"
    json_attributes_template: "{{ value_json | tojson }}"

    - alias: MQTTuserVal
        platform: state
        entity_id: sensor.mqttlock
        - service: input_number.set_value
            entity_id: input_number.mqcount 
            value: '{{ (states.input_number.mqcount.state | int ) + 1 }}'

You are not actually publishing anything to MQTT is the reason your count does not go up.

I don’t understand. When I subscribe with a client, it gives me what’s in the buffer already. Why am I not experiencing the same here?

Any MQTT gurus care to chime in? I’m pounding sand on this one.