Trigger on MQTT only when new value are published

Is there a preferred way to make my MQTT triggered automation to trigger only when a new value arrive?

Sample:

trigger:
  - platform: mqtt
    topic: my/topic/data
action:
  - service: input_select.select_option
    data:
      entity_id: input_select.options
      option: >-
        {{ ['A', 'B', 'C'][trigger.payload | int] }}
    entity_id: input_select.options

I think of one solution to have an Input Number to store the last value, and then set a condition. But are there any better way?

How about:

condition:
  - condition: template
    value_template: "{{ ['A', 'B', 'C'][trigger.payload | int] != states('input_select.options') }}"

I have thought about it too, but the MQTT topic will receive an update about every 20-30 sek, And if I am not lucky I will not be able to set the value (of the input select) all the time, as it will be overwritten by the automation. As the data can be set by either the input select and the MQTT read-topic…

The MQTT have a read topic:
my/topic/data
And a write-topic I publish to:
my/topic/data/set

Then when the device have read the /set-topic and used the value, it will be published to the read-topic. Maybe the problem is the design of this topic (but I cannot change it).

You asked if you could:

That is what the condition does.

I don’t understand what you are saying now.

Sorry for being unclear… :slight_smile:

This scenario (simplified, data can be A, B or C published from device to MQTT topic every 30 sec):

  • Automation triggers by MQTT topic my/topic/data - value: A - Sets the input select to A.
  • I manually adjust the input select in HA to value B.
  • An automation triggers on the input select change and publishes value B to the topic my/topic/data/set
  • Before my/topic/data/set have been read by the device it publishes the old value A to my/topic/data, which triggers the automation to set the input select back to A again. As I understand it the condition you mention cannot solve this.

Depending on timing it will work most of the time, but not if I change the input select just before the 30s update.

Maybe I just need to have another helper just to keep track of the last published value coming FROM my/topic/data, and have it as a condition.

Why not make an MQTT Sensor instead of using an MQTT Trigger?

An MQTT Sensor doesn’t report a state-change if it receives a value identical to its current value.

sensor:
  - name: whatever
    platform: mqtt
    state_topic: my/topic/data
automation:
  - alias: example
    trigger:
      - platform: state
        entity_id: sensor.whatever
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.options
        data:
          option: "{{ ['A', 'B', 'C'][trigger.to_state.state | int(0)] }}"

Perfect! I understand! :slight_smile:
It’s hard to know what to do when you don’t know what to look for… Thanks for your guidance and patient @tom_l and @123 !

1 Like