Using Node-RED IMAP email payload as an iOS notification

I use Node-RED in Home Assistant (Hass.io running in Docker on Ubuntu 64). My current project is to move all messages -- texts, emails, iOS notifications, etc. generated by the various components in my system -- to iOS notifications only.

My Leviton OmniPro II is configured to send all of its notifications to a dedicated Gmail address. I'm able to retrieve those emails in a Node-RED flow and would like to now send the payload on to a Call Service node that notifies my iOS device. Here are the results from the Node-RED email node:

[{"id":"16a60954.5e121f","type":"api-call-service","z":"d5b4d090.56c5e","name":"Notify iOS","server":"36944218.4145ce","version":1,"service_domain":"notify","service":"ios_grant_lewis_iphone","entityId":"","data":"{\"message\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":510,"y":120,"wires":[[]]},{"id":"5c57e0e9.9bba5","type":"debug","z":"d5b4d090.56c5e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":490,"y":80,"wires":[]},{"id":"e1f4beff.b431f8","type":"e-mail in","z":"d5b4d090.56c5e","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"port":"993","box":"INBOX","disposition":"Read","criteria":"UNSEEN","repeat":"30","fetch":"auto","inputs":0,"x":100,"y":80,"wires":[["5c57e0e9.9bba5","16a60954.5e121f"]]},{"id":"36944218.4145ce","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

As the above shows, there's something about the payload that's causing an API error. I've exhausted every avenue I can find -- change nodes, function nodes, etc -- trying to solve the problem, all without success. Any insight woudl be very much appreciated.

This is an error coming from home assistant, not node-red.
Somewhere in your yaml configuration (email?) you either have a typo or misconfigured something.

I send notifications to my iPhone multiple times a day:

[{"id":"9df28333.e5e6","type":"inject","z":"3750e6ca.390e4a","name":"ON","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1300,"wires":[["1fc44014.376d28"]]},{"id":"1fc44014.376d28","type":"api-call-service","z":"3750e6ca.390e4a","name":"Notify Grant","server":"36944218.4145ce","version":1,"service_domain":"notify","service":"ios_grant_lewis_iphone","entityId":"","data":"{\"message\":\"Test iOS notification from Node-RED.\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":250,"y":1300,"wires":[[]]},{"id":"36944218.4145ce","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Configuration is automatic. When a device connects to HA using the HA app for iOS, it appears an entity automatically. As long as the user permits notifications in the app, then a simple call service to that entity is all it takes.

The only difference in this case is that the payload I'm trying to send is generated, not static. So it's the payload that's the problem.

It doesn’t look like it’s the payload, as the one you show shows a simple string.

Try changing your debug node to display the entire message

Incidentally, the same thing happens with this scenario:

  • create a sensor in yaml to retrieve emails
  • retrieve the results of that sensor with a Current State node
  • use the data.attributes.body portion of the payload as an iOS notification

yaml:

  - platform: imap_email_content
    server: imap.gmail.com
    port: 993
    username: !secret gmail_notify_username
    password: !secret gmail_notify_password
    folder: INBOX

[{"id":"fb30e5a0.396e48","type":"api-current-state","z":"d5b4d090.56c5e","name":"IMAP Sensor","server":"36944218.4145ce","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.3560lewishome_gmail_com","state_type":"str","state_location":"","override_payload":"msg","entity_location":"","override_data":"msg","blockInputOverrides":false,"x":150,"y":140,"wires":[["2bdd6ba0.cf6424"]]},{"id":"4d92336a.b017e4","type":"inject","z":"d5b4d090.56c5e","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":80,"wires":[["fb30e5a0.396e48"]]},{"id":"5c57e0e9.9bba5","type":"debug","z":"d5b4d090.56c5e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":140,"wires":[]},{"id":"2bdd6ba0.cf6424","type":"change","z":"d5b4d090.56c5e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.attributes.body","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":140,"wires":[["aa29eceb.773758","5c57e0e9.9bba5"]]},{"id":"aa29eceb.773758","type":"api-call-service","z":"d5b4d090.56c5e","name":"Notify iOS","server":"36944218.4145ce","version":1,"service_domain":"notify","service":"ios_grant_lewis_iphone","entityId":"","data":"{\"message\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":550,"y":180,"wires":[[]]},{"id":"36944218.4145ce","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

So again, it seems there's something about the payload that's causing the error.

The HA node rejects the data, it is homeassistant.

Change your debug node to display "complete msg object"

Thanks to both @bakman2 and @ukmoose for the replies.

Here's the revision:

[{"id":"aa29eceb.773758","type":"api-call-service","z":"d5b4d090.56c5e","name":"Notify iOS","server":"36944218.4145ce","version":1,"service_domain":"notify","service":"ios_grant_lewis_iphone","entityId":"","data":"{\"title\":\"Update\",\"message\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":370,"y":160,"wires":[[]]},{"id":"b0821272.332b38","type":"debug","z":"d5b4d090.56c5e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":350,"y":100,"wires":[]},{"id":"d8f0441d.a55758","type":"e-mail in","z":"d5b4d090.56c5e","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"port":"993","box":"INBOX","disposition":"Read","criteria":"UNSEEN","repeat":"30","fetch":"auto","inputs":0,"x":80,"y":100,"wires":[["b0821272.332b38","aa29eceb.773758"]]},{"id":"36944218.4145ce","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

In this case the path appears to be just payload, right?

You set msg.payload to msg.data.attributes.body in the change node, but there is no msg.data.x in msg.payload.

  • use the data.attributes.body portion of the payload as an iOS notification

Perhaps you configured the change node in reverse; do you want to set a property .data ? If so, change the change node. (set msg.data.x to msg.payload) The change node can be confusing if you work with payload properties. The hint is: set, meaning - a new property.

My apologies. Trying to save time I inadvertently used an old screen shot with new code. I had been experimenting (unsuccessfully) with the change node while tinkering with the IMAP yaml process. I removed it in later attempts since it didn't seem to be helping. It was in that IMAP process that the path I mentioned appeared.

HOWEVER... in the process of creating a new example to use for this reply, I may have fixed whatever goof that was causing the problem.

I just received an iOS notification of the test message I sent, so you were right -- it evidently WAS something I'd mistyped.

I'm a little embarassed, but thank you for the hand-holding.

1 Like