Stuck with Notify Rest

I’m trying to get my owntracks mqtt notification to be forwarded to the Nextcloud app Phone Track but am having a heck of a time getting the notify.rest component to work.

It first was complaining message wasn’t defined even though it’s not mentioned in the doc, after confirming it wouldn’t matter if I had a junk field I just threw in test to test it. Then it was complaining that I had extra keys not allowed in @ data

I feel like I’m missing something obvious.

configuration.yml

notify:
  - name: track_james
    platform: rest
    resource: "https://example.tld/apps/phonetrack/logGet/################################/dugite"
    method: GET

automation.yml

- id: '1526541171994'
  alias: Track Dugite
  trigger:
    platform: mqtt
    topic: owntracks/dugite/mobile
  action:
    service: notify.track_dugite
    data_template:
     message: "test"
     lat: "{{ trigger.payload_json.lat }}"
     lon: "{{ trigger.payload_json.lon }}"
     bat: "{{ trigger.payload_json.batt }}"
     timestamp: "{{ trigger.payload_json.tst }}"

Log

Invalid service data for notify.track_dugite: extra keys not allowed @ data['bat']. Got '52'
extra keys not allowed @ data['lat']. Got '##'
extra keys not allowed @ data['lon']. Got '##'
extra keys not allowed @ data['timestamp']. Got '##'

What I expected get url should look like: https://example.tld/apps/phonetrack/logGet/################################/dugite?message=test&bat=52&lat=##&lon=##&timestamp=##

I know very little about this but should your notify template be indented two spaces?

data_template:
  message: "test"
  lat: "{{ trigger.payload_json.lat }}"
  lon: "{{ trigger.payload_json.lon }}"
  bat: "{{ trigger.payload_json.batt }}"
  timestamp: "{{ trigger.payload_json.tst }}"

No double spaces don’t change anything. I already use double spaces in other configs without issue

What I mean is, the code you provided is indented by one space. It should probably be indented by two spaces as per my example.

Provided they are all indented by the same amount, it doesn’t matter how many spaces there are.

From my tests it’s as @gpbenton said

I also tried

data:
  message: "test"
  data_template:
    timestamp: ...

No dice

what does your payload look like?

It’s just an owntracks payload. {"_type":"location", "lat":##, "lon":## and you can see it’s read correctly in the log it just isn’t passing it as a url parameter as I expected it to.

have you tried just getting the payload without using payload_json? just using payload?

That’s not the problem. If i don’t use paylod_json the error extra keys not allowed @ data['bat']. Got '52' becoms extra keys not allowed @ data['bat']. Got ''

Right, but from what I’ve read, you just want the url to pass through. So if that’s the case, treat the payload as a string and feed it through to the message.

Your other option is to just make the URL, you have the format concatenate the string and pass it through to the message.

2 Likes

You might be right that could be the way around my issue. I just wish i could get the arbitrary parameters to work properly.

data_template (Optional): Template dictionary of extra parameters to send to the resource.

Lookin at the rest.py it looks like it should append extra data but I just can’t figure out the config to do that

I gave up on the notify.rest, just couldn’t get it to work ended up going with shell command and wget

configuration.yml

shell_command:
  save_track: /bin/sh -c '/usr/bin/wget "{{url}}" -O /dev/null'

automation.yml

- id: '1526541171994'
  alias: Track Dugite
  trigger:
    platform: mqtt
    topic: owntracks/dugite/mobile
  action:
    service: shell_command.save_track
    data_template:
       url: "https://example.tld/apps/phonetrack/logGet/################################/dugite?lat={{ trigger.payload_json.lat|string }}&lon={{ trigger.payload_json.lon|string }}&bat={{ trigger.payload_json.batt|string }}&timestamp={{ trigger.payload_json.tst|string }}"

This was my final attempt with notify.rest:

- id: '1526541171994'
  alias: Track Dugite
  trigger:
    platform: mqtt
    topic: owntracks/dugite/mobile
  action:
    service: notify.track_dugite
    data_template:
     message: "test&lat={{ trigger.payload_json.lat|string }}&lon={{ trigger.payload_json.lon|string }}&bat={{ trigger.payload_json.batt|string }}&timestamp={{ trigger.payload_json.tst|string }}"

*Edit: added /bin/sh -c because of this

1 Like

After struggling with it myself, I figured out how the Restful Notification works. Extra parameters (as called by the doc) should be declared in the entity definition, not when calling the service. Unfortunately the rest platform does not seem to support extended data functionality when calling the notify service, so we only have title, message and target available. In a similar case like yours, one could feed the incoming data into an MQTT sensor with attributes and trigger notifications when that sensor changes state.

configuration.yaml

sensor:
  - platform: mqtt
    name: "Location James"
    state_topic: "owntracks/dugite/mobile"
    json_attributes_topic: "owntracks/dugite/mobile"

notify:
  - name: track_james
    platform: rest
    resource: "https://example.tld/apps/phonetrack/logGet/################################/dugite"
    method: GET
    data_template:
      lat: "{{ state_attr('sensor.location_james', 'latitude') }}"
      lon: "{{ state_attr('sensor.location_james', 'longitude') }}"
      bat: "{{ state_attr('sensor.location_james', 'battery') }}"
      timestamp: "{{ state_attr('sensor.location_james', 'timestamp') }}"

automations.yaml

- alias: Track Dugite
  trigger:
    platform: state
    entity_id: sensor.location_james
  action:
    service: notify.track_james
    data_template:
      message: "Received new position"

As an alternative one could also use the title, message or target service parameters to pass data to the notification directly, but that involves some jinja trickery (there are more than 3 parameters needed for this notification).

configuration.yaml

notify:
  - name: track_james
    platform: rest
    resource: "https://example.tld/apps/phonetrack/logGet/################################/dugite"
    method: GET
    data_template:
      lat: "{{ title.split('|')[0] }}"
      lon: "{{ title.split('|')[1] }}"
      bat: "{{ title.split('|')[2] }}"
      timestamp: "{{ title.split('|')[3] }}"

automations.yaml

- alias: Track Dugite
  trigger:
    platform: mqtt
    topic: owntracks/dugite/mobile
  action:
    service: notify.track_james
    data_template:
      message: "test"
      title: "{{ trigger.payload_json.lat|string }}|{{ trigger.payload_json.lon|string }}|{{ trigger.payload_json.batt|string }}|{{ trigger.payload_json.tst|string }}"
1 Like

Hi Petro or anyone else,

Sorry, I know this is an old post but I just came across this and I am in a similar situation as the original poster.

Would anyone be able to further elaborate on Petro’s suggestion of the URL and how to format concatenate the string to pass it through to the message? Maybe with an example of how it would be written? Would really appreciate that! Thank you!

UPDATE: I went with the shell command approach mentioned by dugite-code in the end. That seems to be the most straightforward way.

1 Like