Read string from file for notification

Hello, I’m trying to move an automation from YAML to Node-RED.

The automation greets me when i come back home, the YAML is something like this:

  condition:
  - condition: state
    entity_id: device_tracker.mybt
    state: home
  action:
  - data:
      title: '!include welcomeback.yaml'
    service: notify.myphone

in the data field of the service node component, how do I tell homeassistent to read the ‘welcomeback.yaml’ file instead of sending me the ‘!include welcomeback.yaml’ as title?
I’ve formatted the json this way but… does’nt work

{
    "message": "test",
    "title": "!include welcomeback.yaml"
}

What do you have in the welcomback.yaml file? is it something changes dynamically?
if it’s just a fixed string, why don’t you just pass it in the msg.payload.data.title?
otherwise, you can use “file in” node to read the content of the file.

2 Likes

This won’t work at all in NodeRed because you are calling a service outside of HA (notify_myphone). In a native HA automation, the automations are parsed as YAML internally, so your !include will work fine there. But, in NodeRed, you are passing a call to a service, so there’s no YAML parsing going on when the service is called (aside from Jinja templating).

What you’ll need to do is grab whatever is in the welcomeback.yaml file and translate it to JSON or replicate those actions in NodeRed.

1 Like

It’s an array of string:

 >
      {{ [
      "Hello",
      "Hello again",
      "Hi"
      ] | random }}

Something like this?

I use a random and function nodes for this:

1 Like

great! Thank you :slight_smile:

1 Like

here is another way with jsonata, without function node.

image

[{"id":"9f9b3b92.602638","type":"change","z":"de9989cc.589d18","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$shuffle(payload)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":260,"wires":[["f0c2d47f.f37f38"]]},{"id":"f0c2d47f.f37f38","type":"debug","z":"de9989cc.589d18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":260,"wires":[]},{"id":"20ed7b87.043ad4","type":"inject","z":"de9989cc.589d18","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Hi\",\"Hello\",\"Hi Again\"]","payloadType":"json","x":270,"y":260,"wires":[["9f9b3b92.602638"]]}]
2 Likes

But… how do I send the shuffled msg.payload to the service node that call HA service to send notification? I have something like this.

And then, in the ‘data’ field of the service node that call the notification service, how should i use the msg.payload?

Thanks again

ok here is a more complete flow. this is randomizing the title but keeping the message fixed. you can play around, just leave the data box in service call node empty, the message coming from change node will tell the title and message…

[{"id":"9f9b3b92.602638","type":"change","z":"de9989cc.589d18","name":"","rules":[{"t":"set","p":"messages","pt":"msg","to":"[\"Hi\",\"Hello\",\"Hi Again\"]","tot":"json"},{"t":"set","p":"payload.data.message","pt":"msg","to":"something i need to notify you about","tot":"str"},{"t":"set","p":"payload.data.title","pt":"msg","to":"$shuffle(messages)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":260,"wires":[["3e3d6163.89796e"]]},{"id":"20ed7b87.043ad4","type":"inject","z":"de9989cc.589d18","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":330,"y":260,"wires":[["9f9b3b92.602638"]]},{"id":"3e3d6163.89796e","type":"api-call-service","z":"de9989cc.589d18","name":"","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_pixel_3","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":790,"y":260,"wires":[[]]},{"id":"e447d17a.16a64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]
2 Likes

Thanks @serkank, maybe i misspelled my previous message (sorry ab ut my english).
Your flow work flawlessly, I experimented changing notification platform (telegram/html5) and everything is perfect.

I’m noob to node-red and I just don’t understand where to connect the output from the ‘state node’ in your flow.

if you can explain in more details what is the flow, i can send a better flow.

are you trying to send a notification when the light turns on?

Hi serkank, I’m just experimenting with node red. In this node I’m trying to get a notification when a light turn on or off, here is the flow.

[{"id":"36bc9e5b.8ab942","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d103c0ea.e6883","type":"change","z":"36bc9e5b.8ab942","name":"","rules":[{"t":"set","p":"messages","pt":"msg","to":"[\"Hi\",\"Hello\",\"Hi Again\"]","tot":"json"},{"t":"set","p":"payload.data.message","pt":"msg","to":"something i need to notify you about","tot":"str"},{"t":"set","p":"payload.data.title","pt":"msg","to":"$shuffle(messages)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["5e4a5218.ba5c0c"]]},{"id":"d948f2df.8e808","type":"inject","z":"36bc9e5b.8ab942","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":130,"y":120,"wires":[["d103c0ea.e6883"]]},{"id":"5e4a5218.ba5c0c","type":"api-call-service","z":"36bc9e5b.8ab942","name":"","server":"cc0674b6.29706","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_kerbin_xs","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":180,"wires":[[]]},{"id":"85b61fee.193e18","type":"server-state-changed","z":"36bc9e5b.8ab942","name":"check light status","server":"cc0674b6.29706","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.c1","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":100,"y":220,"wires":[["d103c0ea.e6883"],["d103c0ea.e6883"]]},{"id":"cc0674b6.29706","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false}]

Now the problem is that if I manually trigger the inject node, the notification is sent without problem, if i turn on or of the light I have this error:

{"domain":"notify","service":"mobile_app_kerbin_xs","data":{"message":"something i need to notify you about","title":"Hi Again"}}
Cannot set property of non-object type: payload.data.message

the trigger node is more useful and gives you more flexibility.
this flow sends a notification every time the state of the light changes (on / off). I added a new rule to change node, to clear the payload before passing it to service call.

[{"id":"a154e0da.6af27","type":"change","z":"110b2c7b.9890f4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"messages","pt":"msg","to":"[\"Hi\",\"Hello\",\"Hi Again\"]","tot":"json"},{"t":"set","p":"payload.data.message","pt":"msg","to":"something i need to notify you about","tot":"str"},{"t":"set","p":"payload.data.title","pt":"msg","to":"$shuffle(messages)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":300,"wires":[["4e748bf3.5bcc44"]]},{"id":"4e748bf3.5bcc44","type":"api-call-service","z":"110b2c7b.9890f4","name":"","server":"e447d17a.16a64","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_pixel_3","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":650,"y":300,"wires":[[]]},{"id":"49614b91.7a7e24","type":"trigger-state","z":"110b2c7b.9890f4","name":"","server":"e447d17a.16a64","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"light.study1","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":130,"y":300,"wires":[["a154e0da.6af27"],[]]},{"id":"e447d17a.16a64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

if you want to change the message depending on the state, (i.e. tell me it’s on or off), then just throw a switch node there. also you can change send custom outputs from the trigger node, that’s another way to alter notification message depending on the light’s state.

1 Like

Thanks :slight_smile: