Converting yaml automation to node-red

Hi,

I am looking to translate the following yaml automation to node-red:

  • id: motion_notification
    alias: Motion Notification
    action:
    • service: notify.po_parents
      data_template:
      data:
      title: ‘HomeAssistant:’
      priority: 1
      message: >
      {{ trigger.to_state.attributes.friendly_name }} has detected movement
      condition:
    • condition: state
      entity_id: input_boolean.motion_notifications
      state: ‘on’
      trigger:
    • entity_id: binary_sensor.living_room_pir_livingroom, binary_sensor.hallway_pir_hallway
      platform: state
      to: ‘on’

I have come up with the following so far:

[{“id”:“ae9bd860.008a08”,“type”:“server-state-changed”,“z”:“96acd1d9.4ecc9”,“name”:“Motion Sensors”,“server”:“38f6340d.e8cf4c”,“entityidfilter”:“^binary_sensor\.\w+pir\w+”,“entityidfiltertype”:“regex”,“haltifstate”:“”,“x”:88.5,“y”:293,“wires”:[[“fe642cb8.4cb5a”,“543926d8.2a2658”]]},{“id”:“fe642cb8.4cb5a”,“type”:“debug”,“z”:“96acd1d9.4ecc9”,“name”:“”,“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“x”:826.5,“y”:181,“wires”:},{“id”:“543926d8.2a2658”,“type”:“switch”,“z”:“96acd1d9.4ecc9”,“name”:“”,“property”:“payload”,“propertyType”:“msg”,“rules”:[{“t”:“eq”,“v”:“on”,“vt”:“str”},{“t”:“eq”,“v”:“off”,“vt”:“str”}],“checkall”:“true”,“repair”:false,“outputs”:2,“x”:283.5,“y”:291,“wires”:[[“1d83c025.14333”],]},{“id”:“1d83c025.14333”,“type”:“api-current-state”,“z”:“96acd1d9.4ecc9”,“name”:“Tracking Motion”,“server”:“38f6340d.e8cf4c”,“halt_if”:“off”,“override_topic”:true,“override_payload”:true,“entity_id”:“input_boolean.motion_notifications”,“x”:455.5,“y”:286,“wires”:[[“d70b838a.25963”,“fe642cb8.4cb5a”]]},{“id”:“d2aa6688.471f98”,“type”:“api-call-service”,“z”:“96acd1d9.4ecc9”,“name”:“Notify Parents”,“server”:“38f6340d.e8cf4c”,“service_domain”:“notify”,“service”:“po_parents”,“data”:“{}”,“mergecontext”:“”,“x”:931.5,“y”:283,“wires”:[]},{“id”:“d70b838a.25963”,“type”:“function”,“z”:“96acd1d9.4ecc9”,“name”:“Format Message”,“func”:“var newmsg = {payload:'"title":"homeassistant", "priority":"1", "message": ’ + msg.data.attributes.friendly_name + ‘"has detected movement"’};\nreturn newmsg;”,“outputs”:1,“noerr”:0,“x”:686.5,“y”:284,“wires”:[[“d2aa6688.471f98”,“fe642cb8.4cb5a”]]},{“id”:“38f6340d.e8cf4c”,“type”:“server”,“z”:“”,“name”:“HomeAssistant”,“url”:“http://192.168.1.190:8123”,“pass”:“xxxxx”}]

The regex is matching events from all my pir zwave sensors. I then want to check if motion_sensor alerting is switched on. Plan is to then send a notification (via Pushover) including the friendly name of the sensor that has been triggered.

The friendly name that gets captured is of course the friendly name of the “input_boolean.motion_notification” as that is the last payload I generated. I’ve lost the motion sensor payload along the way.

This is a long way from working and I could use some advice?

Looks like the paste of your code has replaced the quotes with ones that make it invalid JSON, so I can’t import your code.

If you’re finding that a payload is being overwritten by a subsequent one you have a few options.

  • Use a change node to move the payload somewhere else (eg. msg.otherPayload).
  • Have parallel messages and then use a join node to combine the two payloads into key/value pairs based on topic.
  • Have a separate flow that runs on NR startup and also on change of your friendly name and store it in a flow variable. Then you can just reference the flow variable and not have to load a new payload.

here is a pastebin: https://pastebin.com/7xWKfJFB

This is how I would do it. A separate flow responds to changes to your input boolean and stores it in flow.tracking. Then you can include a simple extra switch node based on the value in flow.tracking. Therefore your payload from the sensor is untouched. The inject node is there to get the initial value every time Node Red starts. This may no longer be necessary with persistent context. I also often put a 15 minute repeat on it as extra insurance, in case the event node doesn’t fire.

I’ve just installed node-red. Is there not a way to import yaml files?

2 Likes

Node Red uses node-specific JSON, not YAML. And a lot of the JSON is about how nodes are connected together and where they appear on the screen, which is implied or doesn’t exist in YAML.

I think people with a lot of YAML would have to slowly migrate to Node Red piecemeal, and may not end up moving everything across. Luckily I installed Node Red after only a couple of days of writing YAML.

There is no option (to my knowledge) that is converting yaml to Node-Red and I would recommend not to apply the same yaml logic in Node-Red. Usually there are much simpler ways in NR to realize an automation. Just spend some time to think about what your automation is suppose to do (not how you did it in yaml).

I have re-developed all my yaml into Node-Red flows and they have exceeded the original capabilities several times :slight_smile: