MQTT Button with JSON Payload - Template help

Hi,

I am working on a WiFi MQTT push button that has multiple buttons on it and sends the payload back to HA in JSON format. This is code that I have cobbled together from bits I have found on the internet.
The problem I am having is writing the condition template for the automation to toggle the correct Boolean switch in HA.
This is the JSON data that is received from the device

{“category”:“switch”,“status”:1,“button”:2,“battery”:1.860000014}

I would like to use the value of “button” in my automation contrition and then call the toggle service if it matches the correct value. The device I have has 3 switches on it, and I can add more in the future.
This is the automation that I have so far but it does not work.
There is no “on” or “off” data for the button when it is press. Just a new JSON message with the button number.

alias: MQTT Switch 
description: ""
trigger:
  - platform: mqtt
    topic: stat/ESPNOW_xxxxxxxxxx/status
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.button == '1' }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.switch_1
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.button == '2' }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.switch_2
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.button == '3' }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.switch_3
mode: single

I am new to using templates and JSON payload so I know it is something to do with the syntax and any help would be appreciated.

2 is not the same as ‘2’

try

            value_template: "{{ trigger.payload_json.button == 2 }}"

in addition to what @francisp said (He is correct BTW, that is your problem), you can reduce your automation to be a little easier to manage if you’re just linking it to on/off entities.

alias: MQTT Switch 
description: ""
trigger:
  - platform: mqtt
    topic: stat/ESPNOW_xxxxxxxxxx/status
variables:
  buttons:
    1: input_boolean.switch_1
    2: input_boolean.switch_2
    3: input_boolean.switch_3
  button: >
    {{ buttons.get(trigger.payload_json.button) }}
condition:
- condition: template
  value_template: "{{ button is not none }}"
action:
- service: homeassistant.toggle
  target:
    entity_id: "{{ button }}"

Then, if it has more buttons, you simply add a new buttons config, e.g.

  buttons:
    1: input_boolean.switch_1
    2: input_boolean.switch_2
    3: input_boolean.switch_3
    4: input_boolean.switch_4

Thanks, that was it. I tried so many different ways but not this combination.

petro. That is really neat and easy to scale. Lot to learn and so many different ways to do things