How to fire an event with correct json payload in an automation

I have to write an automation that has an event action. And my idea is to format the event data as a json string.

First Question: Is that a good idea or should event data not be JSON?

Second Question: I tried two ways

- id:    lars1
  alias: lars1
  trigger:
    platform: mqtt
    topic:    MS0/fhem_rc/rc_rc4_btn2/stat/state
    payload:  'off'
  action:
  - event: button_pushed
    event_data:
      state:      false
      attributes: 
      - button: fs20_rc4_btn2
        source: automation_lars

My other attempt was

- id:    lars2
  alias: lars2
  trigger:
    platform: mqtt
    topic:    MS0/fhem_rc/rc_rc4_btn2/stat/state
    payload:  'on'
  action:
  - event: button_pushed
    event_data: {"source": "lars2"}

Which is the right way to do it? One of these or a third way?

I’m not sure if this post has been included in a release yet

It is in the newest release. And for the most part it is not typically required. My use case was an MQTT message that needed more parsing than could really be done in yaml so was better to pass the whole thing to an appdaemon app.

Thanks to both of you.

Yes that is what I am talking about. I have external events (pushed buttons in this case that enter HA through different protocols) which I want to convert into HA-events. I have a strong background in event processing and erlang and it feels wrong for me to convert events into state just to then react to the state changes which are again events.

OK so there are people who get my idea.
Sadly my python knowledge is next to zero so I do not completely understand the source.

One more question: Are

event_data:
  state: false

and

event_data: {"state": false}

equivalent or two completely different things?

Hint: A yes would make me happy, otherwise I still didn’t get the concept.

How are you going to react to these events? Are you using appdaemon?

And no, they are not the same. First one you are sending a python dict, second one sends a string that contains some json. So, I have an automation like this:

- alias: jarvis_hotword_toggle
  trigger:
    platform: mqtt
    topic: hermes/hotword/#
  action:
    event: JARVIS_MQTT
    event_data_template:
      topic: '{{ trigger.topic }}'
      payload: '{{ trigger.payload }}'

And this is what I receive in appdaemon. You can see topic shows up as a python dict (single quotes) and payload is also a dict member which contains a json formatted string.

jarvis_core: _event_listener: {'topic': 'hermes/hotword/toggleOff', 'payload': '{"siteId":"default","sessionId":"54c9f048-785b-4c0c-902c-69d95278893b"}'}

In my app, I can use standard python dict methods to get the values

        if 'hermes/hotword/' in data.get('topic', ''):
            hotword_data = {'toggle': data['topic'].split('toggle')[-1],
                            'payload': data.get('payload')}
            self.listening(hotword_data)

So payload at this point still is just a string that contains json which I can then get at like this

        if data.get('payload'):
            data = json.loads(data.get('payload', data))

And now data equals a python dict which I can get with something like data.get(‘sessionId’)

Does that clear anything up, or just muddy the waters. Python dicts look a lot like json but they aren’t the same.

Thank you very much for the code!
The automation code I will probably copy into my configurations with very little adjustments.

At the moment I am suffering of analysis paralysis because I seem to think to much about how to structure my system and my configurations.

I hope I will get out of this state soon. At the moment I am designing the structure of the mqtt topic hierarchy.