How to properly build a JSON payload and pass it to another node?

I will start by saying that this could be easily done by using functions and a bit of Javascript however I would like to stop spamming my WF with functions here and there just to get the payload.

I have this WF:

[
    {
        "id": "12953e295b7c1aca",
        "type": "tab",
        "label": "BATTERY_STATUS",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "79b3c19e1cfd66e4",
        "type": "inject",
        "z": "12953e295b7c1aca",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 420,
        "wires": [
            [
                "1dce0223737c4b6e"
            ]
        ]
    },
    {
        "id": "1dce0223737c4b6e",
        "type": "ha-get-entities",
        "z": "12953e295b7c1aca",
        "name": "Check on devices battery status",
        "server": "e593dd3.052432",
        "version": 0,
        "rules": [
            {
                "property": "entity_id",
                "logic": "in_group",
                "value": "group.devices_battery",
                "valueType": "str"
            },
            {
                "property": "state",
                "logic": "lte",
                "value": "30",
                "valueType": "num"
            }
        ],
        "output_type": "split",
        "output_empty_results": false,
        "output_location_type": "msg",
        "output_location": "payload",
        "output_results_count": 1,
        "x": 410,
        "y": 420,
        "wires": [
            [
                "39c870a360e33725"
            ]
        ]
    },
    {
        "id": "39c870a360e33725",
        "type": "api-render-template",
        "z": "12953e295b7c1aca",
        "name": "",
        "server": "e593dd3.052432",
        "version": 0,
        "template": "{\n  \"data\": {\n    \"title\": \"Garage Cover\",\n    \"message\": \"The {{ state_attr(msg.payload.entity_id, \",friendly_name\") }} charge is at {{ states(msg.payload.entity_id) }}%. Please replace!\",\n    \"data\": {\n      \"group\": \"HomeAssistantNotifications\",\n      \"push\": {\n        \"sound\": {\n          \"name\": \"GoToSleep_Haptic.caf\",\n          \"critical\": 1,\n          \"volume\": 1\n        }\n      }\n    }\n  }\n}\n",
        "resultsLocation": "payload",
        "resultsLocationType": "msg",
        "templateLocation": "",
        "templateLocationType": "none",
        "x": 640,
        "y": 420,
        "wires": [
            [
                "a587525efe5512df",
                "d3eb0e6f3039b8b7"
            ]
        ]
    },
    {
        "id": "a587525efe5512df",
        "type": "json",
        "z": "12953e295b7c1aca",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": true,
        "x": 810,
        "y": 420,
        "wires": [
            [
                "78813cdab8da6a26"
            ]
        ]
    },
    {
        "id": "78813cdab8da6a26",
        "type": "debug",
        "z": "12953e295b7c1aca",
        "name": "debug 11",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 360,
        "wires": []
    },
    {
        "id": "d3eb0e6f3039b8b7",
        "type": "debug",
        "z": "12953e295b7c1aca",
        "name": "debug 12",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 780,
        "y": 320,
        "wires": []
    },
    {
        "id": "e593dd3.052432",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

and I am trying to use a Render template node to get the data I need from the Get entities node like the friendly name and the state. I will use the resulting payload within a Call service node to notify all my devices. Right now I keep getting the following error:

“Error get-template: Request failed with status code 400”

But I am not sure what is wrong, can I get some ideas and/or help?

You can put the data inside the call-service and use $entities() to get the data

"message": "The " & $entities("payload.entity_id").attributes.friendly_name & " charge is at " &  $entities("payload.entity_id").state & "%. Please replace!",
1 Like

hmmm is not working for me:

“Call-service error. extra keys not allowed @ data[‘0’]”
Does the JSON look ok to you?

{
  "message": "The " & $entities("payload.entity_id").attributes.friendly_name & " charge is at " &  $entities("payload.entity_id").state & "%. Please replace!",
  "data": {
    "group": "HomeAssistantNotifications",
    "push": {
      "sound": {
        "name": "GoToSleep_Haptic.caf",
        "critical": 1,
        "volume": 1
      }
    }
  }
}

also, should I use JSON or J expression?
image

maybe that is the problem?

Ok, tried using J expression and it does not work for me:

I had put unneeded quotes in. Yes, the data field type should be set to J: Expression.

"message": "The " & $entities(payload.entity_id).attributes.friendly_name & " charge is at " &  $entities(payload.entity_id).state & "%. Please replace!",
1 Like