Assign parsed value from webhook to mult sensors

I’m trying to setup a webhook that sends a string of data wrapped in json - the automation looks like its getting called via the Logbook entry:

  1. I’m not assigning the values correctly into a sensor
  2. it’s only processing the first parse i.e. [20] and skipping [6a]

Data:
‘{“payload”: “[20]15.50[6a]18.00[5e]15.31[26]20.87”}’

When I send the data I see this in the Logbook:

Executed: January 23, 2024 at 9:09:11 PM
Result:
params:
  domain: template
  service: create
  service_data:
    template: 59.9
  target:
    entity_id:
      - sensor.temp20
running_script: false

Here’s my yaml:

template:
  - sensor:
      - name: Temp 20
        unique_id: temp20
        entity_id: temp20
        unit_of_measurement: "°F"
  - sensor:
      - name: Temp 6a
        unique_id: temp6a
        entity_id: temp6a
        unit_of_measurement: "°F"

automation:
  - id: parse_and_convert_webhook_payload
    alias: "Parse and Convert Webhook Payload"
    trigger:
      platform: webhook
      webhook_id: your_webhook_id  # Replace with your actual webhook ID
    action:
      - service: template.create
        data:
          template: >-
            {% if trigger.json.payload | regex_search('\[20\]([\d.]+)') %}
              {{ (trigger.json.payload | regex_findall_index('\[20\]([\d.]+)') | float * 9/5 + 32) | round(2) }}
            {% endif %}
        entity_id: sensor.temp20

      - service: template.create
        data:
          template: >-
            {% if trigger.json.payload | regex_search('\[6a\]([\d.]+)') %}
              {{ (trigger.json.payload | regex_findall_index('\[6a\]([\d.]+)') | float * 9/5 + 32) | round(2) }}
            {% endif %}
        entity_id: sensor.temp6a

Are you are using a custom integration? template.create is not a normal, core service. Unless I am misunderstanding your goal, it seems like you are essentially rebuilding trigger-based Template sensors.

For core Template sensors, entity_id is not a valid key, and the state key is required…

I had tried to use chatgpt after trying many things on my own - and have ended up no better and more confused - lol

you are correct it’s service: template.create isn’t correct

I’m looking to take my webhook payload parse it, convert from c to f and save the results into a sensor.

Okay, then you want trigger-based template sensors…

template:
  - trigger:
      - platform: webhook
        webhook_id: your_webhook_id 
    sensor:
      - name: Temp 20
        unique_id: temp_20
        unit_of_measurement: "°F"
        state: |
            {% if trigger.json.payload | regex_search('\[20\]([\d.]+)') %}
              {{ (trigger.json.payload | regex_findall_index('\[20\]([\d.]+)') | float * 9/5 + 32) | round(2) }}
            {% else %}
              {{ this.state }}
            {% endif %}
      - name: Temp 6A
        unique_id: temp_6A
        unit_of_measurement: "°F"
        state: |
            {% if trigger.json.payload | regex_search('\[6a\]([\d.]+)') %}
              {{ (trigger.json.payload | regex_findall_index('\[6a\]([\d.]+)') | float * 9/5 + 32) | round(2) }}
            {% else %}
              {{ this.state }}
            {% endif %}
1 Like

That worked - thanks so much for the help

1 Like