I am sending some json data to HA via http post. I use the /api/event/event_name method so that an event is triggered when the data arrives. I have used the developer tools to listen for the event and it does show up with the appropriate data. I then created an automation that uses the event as a trigger with an action that calls a python function where I intend to act on the data. My question is, how do i pass the event data to the python script?
What does the event data look like? I’m guessing, at the very least, you’ll want to use "{{ trigger.event.data.XYZ }}", replacing XYZ as appropriate.
Parsing a JSON string inside a python_script might not be too easy. (I.e., I don’t think the json package is available.) If there are multiple pieces of data in the JSON string you might want to break them out in the automation instead.
Thanks for your reply. Yes, I ended up building three templates to pull out the data I needed and send it to the script. I wasn’t too familiar with the templating engine but it’s not bad once you use it a little. The biggest obstacle was making sure I validated the data was actually in the json data before i tried to send it. These sensors send frames on a regular cadence but only some of them actually have the temperature/humidity fields populated.
remember you can either use a real template format (like I suggest above , between {{ }} ) in which you can either use a factual template, or, like in this case, a string. You can also use:
sequence:
service: python_script.hello
data:
name: Passed name
all in all, I think you should simply change data_template to data:
name = data.get("name")
logger.error("Hello %s", name)
hass.bus.fire(name, {"wow": "from a Python script!"})
remember also that the log_level needs to be adequate for the logger to be seen. My default log_level is ‘error’, so I changed it to that level in the Python script