Multiple trigger-based sensors using JSON data from different webhooks

I could successfully create a trigger-based sensor where the trigger is a webhook. Here’s how it looks like (almost the same as the documentation example).

configuration.yaml:

# ...

template: !include templates.yaml

# ...

templates.yaml:

triggers:
  - trigger: webhook
    webhook_id: my-super-secret-webhook-1

sensor:
  - name: "Webhook Temperature 1"
    unique_id: "webhook_temperature_1"
    state: "{{ trigger.json.temperature }}"
    unit_of_measurement: °C

That works, but now I’d like to register a different webhook which would set the value of a different sensor. I’d guess it would be something like this:

triggers:
  - trigger: webhook
    webhook_id: my-super-secret-webhook-1
  - trigger: webhook
    webhook_id: my-super-secret-webhook-2

sensor:
  - name: "Webhook Temperature 1"
    unique_id: "webhook_temperature_1"
    state: "{{ trigger.json.temperature }}"
    unit_of_measurement: °C
  - name: "Webhook Temperature 2"
    unique_id: "webhook_temperature_2"
    state: "{{ trigger.json.temperature }}"
    unit_of_measurement: °C

but then clearly I’ll need a way to identify those webhooks, so first sensor would set its state using JSON data from the first webhook, and second sensor would use the data from second webhook.

At the moment if I send the following request:

$ curl -X POST \
    -H "Content-Type: application/json" \
    -d '{"temperature":5}' \
    https://MY-HOME-ASSISTANT.HOST:8123/api/webhook/my-super-secret-webhook-1 # or my-super-secret-webhook-2

then both sensors get updated:

And I could not figure that out - how can I use not just trigger.json.temperature but a different TRIGGER-ID-OR-SOMETHING.json.temperature for every sensor?

I reckon, I could use one common webhook for setting the states of all of my theoretical 9000+ sensors, by making it so that instead of a “direct” temperature value I’d have a dictionary, and every sensor would look for its own key and keep the previous state if JSON data does not contain that key. So that’s one workaround.

Alternatively, I’d be fine with doing this via automations instead of trigger-based sensors, as different automations can be triggered by different webhooks, which is great, but for the love of god I couldn’t find how can one set the state of a template sensor from an automation.

The Home Assistant version that I am using at the moment is 2025.5.3.

If you don’t want both sensors triggered by both triggers, split them up completely:

- triggers:
    - trigger: webhook
      webhook_id: my-super-secret-webhook-1
  sensor:
    - name: "Webhook Temperature 1"
      unique_id: "webhook_temperature_1"
      state: "{{ trigger.json.temperature }}"
      unit_of_measurement: °C
- triggers:
    - trigger: webhook
      webhook_id: my-super-secret-webhook-2
  sensor:
    - name: "Webhook Temperature 2"
      unique_id: "webhook_temperature_2"
      state: "{{ trigger.json.temperature }}"
      unit_of_measurement: °C

Edit: if you want to update a template sensor from an automation, have the automation fire custom events (and set up the trigger for the trigger-based template sensor as that custom event)

1 Like

split them up completely

That gave me configuration errors:

Error loading /config/configuration.yaml: while parsing a block collection
  in "/config/templates.yaml", line 1, column 1
expected <block end>, but found '?'
  in "/config/templates.yaml", line 23, column 1

but then I realized that I need to make the rest of the file to follow the same pattern, as what I had before was malformed(?). So for example before I had:

binary_sensor:
  - name: "Some thing"
    unique_id: "some_thing"
  - ...

and now I needed to do:

- binary_sensor:
  - name: "Some thing"
    unique_id: "some_thing"
  - ...

and the same for other sections.

Anyway, your solution worked, and I think I tried something like this during my research, but obviously it didn’t fit my incorrect(?) templates.yaml format. And now triggering either of those webhooks updates just one sensor - exactly what I wanted to achieve. Thank you!

set up the trigger for the trigger-based template sensor as that custom event

I had that working too, but there I also had the same problem of different sensors getting the same trigger.event.data.state. Now that I now about splitting triggers, I can of course go this way too, but webhook triggers are quicker to set-up. Automations would make sense only if I could set the sensor value directly in their actions, not via events.

What you had before wasn’t technically malformed, which is why it was working. You had each top-level entry (e.g. triggers: or binary_sensor:) as a key of a single dict instead of an item in a list of separate dicts. This works as long as every top-level entry is unique. But having them as entries in one dict means you can’t have any duplicate keys, and therefore prevents you from doing what you wanted to do.

Preceding the key with a dash makes it an item in a list. A list can have duplicate items. A dict can’t have duplicate keys.

I consider this the Bible for how yaml works in HA:
http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

1 Like

Right! That makes sense, thank you for taking the time to explain the difference.
Not a lot of YAML experience on my side, admittedly. Will certainly study the bible.

1 Like