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.