malosaa / PostNL_DHL_Tracker_HA

PostNL+DHL Track Trace for HomeAssistant
12 stars 0 forks source link

Issue with tracking #3

Open Fabian-Ser opened 1 year ago

Fabian-Ser commented 1 year ago

I added the configuration to my configuration.yml (https://github.com/malosaa/PostNL_DHL_Tracker_HA/blob/main/config/packages/rest/post_nl_tracker.yaml) and added the helpers. In the helpers I did add my postal code and tracking code and added the entities to my dashboard. Now my "Delivery time", "Identification", "Recipient", "Sender" is "not available".

When I inspect the raw data of "postnl_tracker" it shows all the correct data, also the "PostNL REST Resource" contains a valid URL with all the correct data.

Am I doing something wrong?

image image

Thanks!

malosaa commented 1 year ago

Hi,

Can you show me your configuration? as for me it still works normal.

what happens if u go to DEVELOPER TOOLS > TEMPLATE paste this in there:

{{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['names']['personName'] }}

Also be sure the data is available in your REST sensor, just double check it.

rest:
  - authentication: basic
    resource_template: "{{ states('sensor.postNL_rest_resource') }}"
    scan_interval: 80000
    sensor:
    - name: postnl_tracker
      unique_id: 854729072388394756
      value_template: 'ok'
      json_attributes:
        - "processed" 
        - "colli"

Here is my full config

https://github.com/malosaa/PostNL_DHL_Tracker_HA/blob/main/config/packages/rest/post_nl_tracker.yaml

Or there has been a typo in the lovelace code, be sure you doublecheck that too. The code is also available in my repo.

regards

Fabian-Ser commented 1 year ago

what happens if u go to DEVELOPER TOOLS > TEMPLATE paste this in there: {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['names']['personName'] }}

UndefinedError: None has no element Undefined image

Configuration: image

rest:
  - authentication: basic
    resource_template: "{{ states('sensor.postNL_rest_resource') }}"
    scan_interval: 80000
    sensor:
    - name: postnl_tracker
      unique_id: 854729072388394756
      value_template: 'ok'
      json_attributes:
        - "processed" 
        - "colli"

sensor:
  - platform: template
    sensors:

      postnl_rest_resource:
        friendly_name: "PostNL REST Resource"
        value_template: >-
          https://jouw.postnl.nl/track-and-trace/api/trackAndTrace/{{ states('input_text.postnl_tracking_code') }}-NL-{{ states('input_text.postnl_postal_code') }}?language=nl&D=NL

      postnl_identification:
        friendly_name: 'Identification'
        value_template: >
          {% set tracking_code = states('input_text.postnl_tracking_code') %}
          {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['identification'] }}

      postnl_recipient:
        friendly_name: 'Recipient'
        value_template: >
          {% set tracking_code = states('input_text.postnl_tracking_code') %}
          {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['names']['personName'] }}
        attribute_templates:
           streetname: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['address']['street'] }}
           postalCode: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['address']['postalCode'] }}
           town: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['address']['town'] }}
           country: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['recipient']['address']['country'] }}
      postnl_sender:
        friendly_name: 'Sender'
        value_template: >
          {% set tracking_code = states('input_text.postnl_tracking_code') %}
          {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['names']['personName'] }}
        attribute_templates:
           companyname: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['names']['companyName'] }}            
           streetname: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['address']['street'] }}
           postalCode: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['address']['postalCode'] }}
           town: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['address']['town'] }}
           country: >
             {% set tracking_code = states('input_text.postnl_tracking_code') %}
             {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['sender']['address']['country'] }}

      postnl_delivery_time:
        friendly_name: 'Delivery Time'
        value_template: >
          {% set tracking_code = states('input_text.postnl_tracking_code') %}
          {% set start_date_str = state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['start'].split('T')[0] %}
          {% set end_date_str = state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['end'].split('T')[0] %}
          {% set start_time_str = state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['start'].split('T')[1] | regex_replace('.[0-9]{2}\\+.*', '') | regex_replace('T', ' ') %}
          {% set end_time_str = state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['end'].split('T')[1] | regex_replace('.[0-9]{2}\\+.*', '') | regex_replace('T', ' ') %}
          {% set start_date = as_datetime(start_date_str) %}
          {% set end_date = as_datetime(end_date_str) %}
          {% set today = now().date() %}
          {% set yesterday = today - timedelta(days=1) %}
          {% set tomorrow = today + timedelta(days=1) %}
          {% set soon = today + timedelta(days=2) %}
          {% if start_date.date() == today %}
            {% set date_string = "Today" %}
          {% elif start_date.date() == tomorrow %}
            {% set date_string = "Tomorrow" %}
          {% elif start_date.date() == yesterday %}
            {% set date_string = "Yesterday" %}
          {% elif start_date.date() < soon %}
            {% set date_string = "Soon" %}
          {% else %}
            {% set date_string = start_date_str %}
          {% endif %}
          "{{ date_string }} between  {{ start_time_str }} and  {{ end_time_str }}"

        attribute_templates:
          date: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['start'].split('T')[0] }}
          start_time: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['start'].split('T')[1] | regex_replace(':[0-9]{2}.*$', '') }}
          end_time: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['eta']['end'].split('T')[1] | regex_replace(':[0-9]{2}.*$', '') }}
          isDelivered: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['isDelivered'] }}
          deliveryDate: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
              {% set delivery_date = state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['deliveryDate'] %}
              {% set delivery_datetime = as_timestamp(delivery_date) %}
              {% set today = as_timestamp(now()) %}
              {% set difference = today - delivery_datetime %}
              {% if difference < 0 %}
                "In the future"
              {% elif difference < 86400 %}
                "Today at {{ delivery_datetime | timestamp_custom('%H:%M') }}"
              {% elif difference < 172800 %}
                "Yesterday at {{ delivery_datetime | timestamp_custom('%H:%M') }}"
              {% else %}
                "{{ (today - delivery_datetime) // 86400 }} days ago at {{ delivery_datetime | timestamp_custom('%H:%M') }}"
              {% endif %}
          isDeliveryDateChanged: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['isDeliveryDateChanged'] }}
          isPickedUpAtRetailLocation: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['isPickedUpAtRetailLocation'] }}
          beforeFirstDeliveryAttempt: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['beforeFirstDeliveryAttempt'] }}
          isReturnShipment: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['isReturnShipment'] }}
          expectedReturnDate: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['expectedReturnDate'] }}
          isAtRetailLocation: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['isAtRetailLocation'] }}
          hasProofOfDelivery: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['hasProofOfDelivery'] }}
          statusMessage: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['statusPhase']['message'] }}
          statusRoute: >
            {% set tracking_code = states('input_text.postnl_tracking_code') %}
            {{ state_attr('sensor.postnl_tracker', 'colli')[tracking_code]['statusPhase']['route'] }}

Hope this is enough info for you :)

Tosko4 commented 1 year ago

it's normal that this shows undefined in there.

have you reloaded your entire yaml after making the helpers and submitting them to your code?