Rest sensor - variable in payload

Hello all,

I have been struggling for months now to get this REST sensor working, but finally admitted my defeat and opted to reach out for help.

My municipality (or better said: the outsourced service provider) provides an online calendar with garbage collection dates. I was able to simulate the POST request that their website makes to an API in HA and get a valid reply back. The issue is that the POST request needs an ‘start date’ and ‘end date’ to fetch the results. This results in a requirement to POST a date, which changes with every (daily) call. The yaml looks like this:

- platform: rest
    name: vuilophaal
    resource: https://wasteprod2api.ximmio.com/api/GetCalendar
    method: POST
    json_attributes:
      - dataList
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json
    payload: '{"companyCode":"<SOME PII INFO>","startDate":"2020-11-19","endDate":"2021-12-31","community":"Lisse","uniqueAddressID":"<SOME PII"}'
    value_template: 'OK'
  - platform: template
    sensors:
      gft:
        friendly_name: 'GFT-bak'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophaal','dataList')[0].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}
      papier:
        friendly_name: 'Papier-container'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophaal','dataList')[1].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}
      pmd:
        friendly_name: 'PMD-container'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophaal','dataList')[2].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}

I have tried numerous options to enter the ‘start date’ with a variable, being today’s date, but only get a valid reply back if I enter a ‘fixed’ date.

Having tried for months, admitting defeat (:)), I hope that someone is able to push me into the right direction.

Thank you!

Did you ever find a solution to this? Ie: Dynamic time in the rest payload ?

Hi Kristian,

No, I went another path by using command_line to post the request:

sensor:
  - platform: command_line  
    command: "curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: Mozilla/5.0' -d '{\"companyCode\":\"<<SOME PI DATA>>\",\"startDate\":\" {{ as_timestamp(now()) | timestamp_custom('%Y-%m-%d') }} \",\"endDate\":\"2021-12-31\",\"community\":\"Lisse\",\"uniqueAddressID\":\"<<SOME PI DATA>>\"}' https://wasteprod2api.ximmio.com/api/GetCalendar"
    name: vuilophalen
    json_attributes:
      - dataList
    value_template: 'OK'
    scan_interval: 86400
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
  - platform: template
    sensors:
      gft:
        friendly_name: 'GFT-bak'
        icon_template: mdi:delete-empty
        value_template: "{{ state_attr('sensor.vuilophalen','dataList')[0].pickupDates[0] }}"
      papier:
        friendly_name: 'Papier-container'
        icon_template: mdi:delete-empty
        value_template: "{{ state_attr('sensor.vuilophalen','dataList')[1].pickupDates[0] }}"
      pmd:
        friendly_name: 'PMD-container'
        icon_template: mdi:delete-empty
        value_template: "{{ state_attr('sensor.vuilophalen','dataList')[2].pickupDates[0] }}"
      gftfriendly:
        friendly_name: 'GFT-bak'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophalen','dataList')[0].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}
      papierfriendly:
        friendly_name: 'Papier-bak'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophalen','dataList')[1].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}
      pmdfriendly:
        friendly_name: 'PMD-container'
        icon_template: mdi:delete-empty
        value_template: >
          {% set current = state_attr('sensor.vuilophalen','dataList')[2].pickupDates[0] | string %}
          {{ as_timestamp(strptime(current, "['%Y-%m-%dT%H:%M:%S']")) | timestamp_custom("%d %B %Y") }}

Above code shows how it fixed it - feel free to use it :-).
Didn’t spend too much time to make the end date dynamic too: just change it once a year.

Good luck!

2 Likes

Woah strungling too with this, curl command line just solved it all. thx for helping.