Toggle device based on MQTT payload change

Hi all

I have a Shelly device connected to HA over MQTT, it sends to the following topic every so often shellies/shelly1/input/0 a payload of either 1 or 0 depending on whether the switch state is on or off.

I’d like to set up an automation that runs ONLY if the payload has changed since the message was last sent. I’m struggling with how to achieve this.

How about a MQTT binary sensor:

binary_sensor:
  - platform: mqtt
    name: "shelly_1"
    state_topic: "shellies/shelly1/input/0"
    payload_on: "1"
    payload_off: "0"

and an automation, that triggers only on defined transitions:

trigger:
  - platform: state
    entity_id: binary_sensor.shelly_1
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: binary_sensor.shelly_1
    from: 'off'
    to: 'on

Here’s a very basic automation using an MQTT Trigger. It’s triggered by payloads published to a topic.

- alias: mqtt example
  trigger:
  - platform: mqtt
    topic: shellies/shelly1/input/0
  action:
  - service: persistent_notification.create
    data:
      title: MQTT Example 
      message: "Received: {{ trigger.payload }}"

The action simply posts a notification reporting what was received. Obviously, it would be replaced by whatever it is that you want the automation to do.

Thanks for this. The MQTT binary sensor was the piece of the puzzle I was missing. All working perfectly now