# How to call API on event based

**URL:** <https://discourse.nodered.org/t/how-to-call-api-on-event-based/64998>\
**Category:** Hardware\
**Tags:** http-request\
**Created:** [12 July 2022 10:04 UTC](https://discourse.nodered.org/t/how-to-call-api-on-event-based/64998 "2022-07-12T10:04:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_98](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@Rahul\_98](https://discourse.nodered.org/u/Rahul_98)\
**Post date:** [12 July 2022 10:04 UTC](https://discourse.nodered.org/t/how-to-call-api-on-event-based/64998/1 "2022-07-12T10:04:40Z")

</div>

Hello!  
I want to send tag data to API based on condition ..when the tag value changes from (true) to  
(false) in device the API should send data .we should write a script for this configuration or there are any other way to solve this problem??

```auto
    {
        "id": "f3bf0ccb5a652f28",
        "type": "change",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Timestamp",
                "pt": "msg",
                "to": "$moment($now()).format(\"YYYY-MM-DD HH:mm\")\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "headers.Content-Type",
                "pt": "msg",
                "to": "application/json",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1020,
        "y": 280,
        "wires": [
            [
                "424eb14a3cd5dff7",
                "0ded6faf4af883f6"
            ]
        ]
    },
    {
        "id": "424eb14a3cd5dff7",
        "type": "debug",
        "z": "1934b69fd66fe8ff",
        "name": "Check msg.TagID and msg.dateTime ",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1310,
        "y": 280,
        "wires": []
    },
    {
        "id": "d902e4f47b6a71a3",
        "type": "function",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "func": "msg.payload={\n\"Machinekey\":1,\n\"Count\":1,\n\"Value\":msg.payload\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1000,
        "y": 360,
        "wires": [
            [
                "393ea5c67de87342",
                "f6440b4e10076b01"
            ]
        ]
    },
    {
        "id": "7001736f19b5dcf6",
        "type": "change",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Timestamp",
                "pt": "msg",
                "to": "$moment($now()).format(\"YYYY-MM-DD HH:mm\")\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "headers.Content-Type",
                "pt": "msg",
                "to": "application/json",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1020,
        "y": 440,
        "wires": [
            [
                "f6440b4e10076b01",
                "393ea5c67de87342"
            ]
        ]
    },
    {
        "id": "393ea5c67de87342",
        "type": "http request",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://192.168.1.189:1003/api/Home/AddMachineData?Machinekey={{MachineKey}}&Parameter&Count=7&Value=98&Timestamp={{Timestamp}}",
        "tls": "",
        "persist": true,
        "proxy": "",
        "authType": "bearer",
        "senderr": false,
        "x": 1190,
        "y": 360,
        "wires": [
            [
                "21aefa8841fc842d"
            ]
        ]
    },
    {
        "id": "f6440b4e10076b01",
        "type": "debug",
        "z": "1934b69fd66fe8ff",
        "name": "Check msg.TagID and msg.dateTime ",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1310,
        "y": 440,
        "wires": []
    },
    {
        "id": "21aefa8841fc842d",
        "type": "debug",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1370,
        "y": 360,
        "wires": []
    },
    {
        "id": "8567d0a3e704bcf0",
        "type": "switch",
        "z": "1934b69fd66fe8ff",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 850,
        "y": 400,
        "wires": [
            [
                "d902e4f47b6a71a3",
                "7001736f19b5dcf6"
            ]
        ]
    }
]

```

_post edited to add ``` around the code_

---

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [12 July 2022 11:09 UTC](https://discourse.nodered.org/t/how-to-call-api-on-event-based/64998/2 "2022-07-12T11:09:53Z")

</div>

I think your problem is similar to this one:

> [@Falling edge detection with JSON payload](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920):
>
> I have some sensors that are set up to send their data via MQTT to NR in JSON format. When they detect, they appear similar to: [image] I want to send an email notification when the battery drops below the warn threshold. Basically, when a battery is new, it reports as battery\_ok=1. When it gets below whatever Acurite determines warning level, it switches to battery\_ok=0. I can detect this with a falling edge node easily by using a change node to break the battery\_ok value out, but then I …

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 September 2022 11:10 UTC](https://discourse.nodered.org/t/how-to-call-api-on-event-based/64998/3 "2022-09-10T11:10:45Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
