Receive an MQTT JSON to an switch service

Hi everybody, I’m having some crazy ideas around here and I want to know if it could work…

I have some eWelink Sonoff switches at home working just fine with the official eWelink addon.
And I have some custom build devices integrated to the Home Assistant using MQTT.

I want to send from my custom device (let’s call Dude) an JSON with the device id and the status:
{ deviceId: ‘switch.abc123’, status: ‘on’ }

And the Home Assistant receive it throught an Automation (or something else) and set the status.

I’ve tried some crazy things and last one was something like this:

Blockquote - id: ‘1646194850624’
alias: Teste liga
description: ‘’
trigger:
- platform: mqtt
topic: home-assistant/ligator
condition:
action:
- service: switch.turn_on
data: {}
target:
entity_id: “{{ value_json.deviceId }}”

And this:

Blockquote - id: ‘1646194850624’
alias: Teste liga
description: ‘’
trigger:
- platform: mqtt
topic: home-assistant/ligator
condition:
action:
- service: switch.turn_ + “{{ value_json.status}}”
data: {}
target:
entity_id: “{{ value_json.deviceId }}”

How could I make it work? Is it even possible?

Based on the information found here we can reference the payload’s JSON key-values like this:

- id: teste_liga
  alias: Teste liga
  trigger:
    - platform: mqtt
      topic: home-assistant/ligator
  condition: []
  action:
    - service: 'switch.turn_{{ trigger.payload_json.status }}'
      target:
        entity_id: '{{ trigger.payload_json.deviceId }}'
1 Like

Thank you very much!

1 Like