MQTT to toggle lights (switch - rpi_rf )

Hey,

I have a rpi_rf switch setup and I would like to control it via my mqtt topic by sending a message with this format:

{
    "data": [
        {
            "service": "switch.bedroom_light",
            "entity": "switch.bedroom_light",
            "ingredient": "on"   # OR "off"
        }
    ]
}

In my configuration file I have configured the mqtt and the switch:

switch:
  - platform: rpi_rf
    gpio: 17
    switches:
      bedroom_light:
        protocol: 1
        pulselength: 180
        code_on: 4273420
        code_off: 4273411

mqtt:
    broker: example.broker.com

And now in the automations.yaml file I would like to add the right configuration but I cant find any example how to handle the message and match it with the right switch state.

- id: '1581790322545'
- alias: 'Bedroom lights'
  trigger:
    platform: mqtt
    topic: 'channel/topic'
  action:
    service: homeassistant.toggle
    data:
      entity_id: switch.bedroom_light

Is there any idea how to switch the state by getting ON or OFF from the mqtt?

Cheers

You can use value_json to get the data in json format.

To answer your question, you would do:

condition:
  condition: template:
  # Optional, filter out messages we don't care about
  value_template: '{{ value_json.data[0].service == 'switch.bedroom_light'}}'
action:
  service_template: "switch.turn_"{{value_json.data[0].ingredient}}"
  entity_id: "{{ value_json.data[0].entity }}"