JSON target name extraction help

My endpoint receives multiple JSON packets. Each from different temp sensors. I can’t change the way in which the endpoint receives this data.

Each specific JSON packet looks like this:

{
  "event": {
    "eventId": "c5rs364aa6gu3disj5ng",
    "targetName": "projects/c2utvmhgmd8000fkmo90/devices/bjeis9e7gpvg00cjoapg",
    "eventType": "temperature",
    "data": {
      "temperature": {
        "value": 45.9,
        "updateTime": "2021-10-26T08:50:24.504000Z",
        "samples": [
          {
            "value": 45.9,
            "sampleTime": "2021-10-26T08:50:24.504000Z"
          }
        ]
      }
    },
    "timestamp": "2021-10-26T08:50:24.504000Z"
  },
  "labels": {}
}

I can use in HA with success:

sensor:
      - name: "Water_Heater"
        state: "{{ trigger.json.event.data.temperature['value'] }}"
        unit_of_measurement: °C

However as each packet arrives I get sensor data from around 10 devices. I want to be able to filter based on the target name.

Can anybody please assist.

All help is appreciated.

I think you need to do something like:

    sensor:
      - name: "Water_Heater"
        state: >-
                  {% if trigger.json.event['TargetName']  == "projects/c2utvmhgmd8000fkmo90/devices/bjeis9e7gpvg00cjoapg" -%}
                         {{ trigger.json.event.data.temperature['value'] }}
                  {%- endif %}
        unit_of_measurement: °C

This means name has to be the above specified for it to set the value of this entity

thanks for looking at this much appreciated.

I have this in YAML:

    sensor:
      - name: "Water_Heater"
        state: >-
                  {% if trigger.json.event['targetName']  == "projects/c2utvmhgmd8000fkmo90/devices/bjeis9e7gpvg00cjoapg" -%}
                         {{ trigger.json.event.data.temperature['value'] }}
                  {%- endif %}
        unit_of_measurement: °C

And now I seem to be getting data from everything except the device in the if statement.

It’s not the history you’re looking at.
Before this change the same entity will keep the history of what it used to be.

But I just remembered that you probably need an else also.

{{ states(‘sensor.water_heater’) }}

Because otherwise it will probably return nothing/null/zero when it’s the wrong device and that is not what we want.

sorry bit of a noob when it comes to the syntax - something like this?

    sensor:
      - name: "Water_Heater"
        state: >-
                  {% if trigger.json.event['targetName']  == "projects/c2utvmhgmd8000fkmo90/devices/bjeis9e7gpvg00cjoapg" -%}
                         {{ trigger.json.event.data.temperature['value'] }}
                    {% else %}
                    {{ states(‘sensor.water_heater’) }}
                  {%- endif %}
        unit_of_measurement: °C

For those following on at home - This works perfectly:

    sensor:
      - name: "Water_Heater"
        state: >-
                  {% if trigger.json.event['targetName']  == "projects/c2utvmhgmd8000fkmo90/devices/bjeis9e7gpvg00cjoapg" -%}
                         {{ trigger.json.event.data.temperature['value'] }}
                    {% else %}
                        {{ states('sensor.water_heater') }}
                  {%- endif %}

Thank you @Hellis81 !!

1 Like