# IKEA motion sensor

**URL:** <https://discourse.nodered.org/t/ikea-motion-sensor/20781>\
**Category:** General\
**Created:** [23 January 2020 17:20 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781 "2020-01-23T17:20:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [23 January 2020 17:20 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/1 "2020-01-23T17:20:38Z")

</div>

I have a Ikea motion sensor the output from which I can access in Node-Red using zigbee2mqtt.

23/01/2020, 17:05:34[node: 475f9e2.959efe](http://192.168.5.81:1880/#)

zigbee2mqtt/pirsensor : msg.payload : Object

object  
occupancy: false  
linkquality: 73  
battery: 60

I am having difficulty using a Switch Node to process the true/false output from the sensor.  
I have tried using a Split Node which delivers 3 separate outputs, occupancy (true or false), Linkquality and Battery. It is the first of these I am trying to isolate to switch a light on.

zigbee2mqtt/pirsensor : msg : Object

object

topic: "zigbee2mqtt/pirsensor"  
payload: false  
qos: 0  
retain: false  
parts: object  
id: "e0ad1f30.f1d67"  
type: "object"  
key: "occupancy"  
index: 0  
count: 3  
key: "occupancy"  
\_msgid: "ef727a8a.9ba4b8"

The Switch Node is set to detect either true or false but currently doesn't respond.  
The MQTT In node is set to a parsed JSON object.  
I have tried copying the path etc but no success.  
Any help would be most appreciated.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 January 2020 17:22 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/2 "2020-01-23T17:22:01Z")

</div>

Are you sure that the input is actually a boolean and not a string?

---

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [23 January 2020 19:44 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/3 "2020-01-23T19:44:48Z")

</div>

Maybe it is, here is the raw output -

23/01/2020, 19:40:23[node: 475f9e2.959efe](http://192.168.5.81:1880/#)  
zigbee2mqtt/pirsensor : msg.payload : string[48]  
"{"occupancy":true,"linkquality":63,"battery":60}"

and after the Split Node

23/01/2020, 19:40:23[node: 76c2c6d3.07b2d8](http://192.168.5.81:1880/#)  
zigbee2mqtt/pirsensor : msg : Object  
object  
topic: "zigbee2mqtt/pirsensor"  
payload: "{"occupancy":true,"linkquality":63,"battery":60}"  
qos: 0  
retain: false  
parts: object  
id: "e2e451b2.400f5"  
type: "string"  
ch: "↵"  
index: 0  
count: 1  
\_msgid: "ad771a69.9b2268"

I'm still unable to process this output.  
Any thoughts?

BriAN

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2020 21:46 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/4 "2020-01-23T21:46:56Z")

</div>

> [@BrianD](#):
>
> zigbee2mqtt/pirsensor : msg.payload : string[48]  
> "{"occupancy":true,"linkquality":63,"battery":60}"

Feed that into a JSON node (or if it is coming from mqtt then tell the mqtt node to parse it as a JSON string) and feed that into a debug node. You should see a javascript object in the payload, and msg.payload.occupancy should be true or false. If you feed that through a Change node set to Move msg.payload.occupance To msg.payload then that should set the payload true or false, check that is the case with a debug node.  
Now what _exactly_ do you want to do with that? You mention a Switch node, do you mean a ui\_switch node? If so then you should be able to feed what you have straight into the switch. If it doesn't work show us how you have configured the switch node.

---

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [24 January 2020 10:22 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/5 "2020-01-24T10:22:38Z")

</div>

Thanks Colin the payload is now either true or false. I am using this payload to connect to a MQTT out node which publishes to a sonoff switch with tasmota firmware. I wanted to learn how to use the Trigger node to switch the sonoff switch. The mqtt message has to be either 0/1 or ON OFF. To do this I now have to change true/false to either 0/1 or ON OFF. Have I got that right?

---

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [24 January 2020 11:27 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/6 "2020-01-24T11:27:08Z")

</div>

This works -

[  
{  
"id": "4b9083e5.db1c44",  
"type": "mqtt in",  
"z": "44ed0376.b98684",  
"name": "",  
"topic": "zigbee2mqtt/pirsensor",  
"qos": "0",  
"datatype": "json",  
"broker": "af63855f.695e08",  
"x": 140,  
"y": 200,  
"wires": [  
[  
"e7bc37c1.7f8e18"  
]  
]  
},  
{  
"id": "eb9f6878.f02b28",  
"type": "mqtt out",  
"z": "44ed0376.b98684",  
"name": "",  
"topic": "sonofftest/cmnd/POWER",  
"qos": "",  
"retain": "",  
"broker": "a18c05f4.86c9d",  
"x": 1270,  
"y": 160,  
"wires":   
},  
{  
"id": "87e24825.fb009",  
"type": "trigger",  
"z": "44ed0376.b98684",  
"op1": "",  
"op2": "OFF",  
"op1type": "nul",  
"op2type": "str",  
"duration": "10",  
"extend": false,  
"units": "s",  
"reset": "ON",  
"bytopic": "all",  
"name": "",  
"x": 1030,  
"y": 260,  
"wires": [  
[  
"eb9f6878.f02b28"  
]  
]  
},  
{  
"id": "e7bc37c1.7f8e18",  
"type": "change",  
"z": "44ed0376.b98684",  
"name": "",  
"rules": [  
{  
"t": "move",  
"p": "payload.occupancy",  
"pt": "msg",  
"to": "payload",  
"tot": "msg"  
}  
],  
"action": "",  
"property": "",  
"from": "",  
"to": "",  
"reg": false,  
"x": 430,  
"y": 200,  
"wires": [  
[  
"838fdc96.c1b3d8"  
]  
]  
},  
{  
"id": "e5c02581.c68a98",  
"type": "change",  
"z": "44ed0376.b98684",  
"name": "",  
"rules": [  
{  
"t": "set",  
"p": "payload",  
"pt": "msg",  
"to": "ON",  
"tot": "str"  
}  
],  
"action": "",  
"property": "",  
"from": "",  
"to": "",  
"reg": false,  
"x": 820,  
"y": 160,  
"wires": [  
[  
"eb9f6878.f02b28",  
"d9aac502.1d4988"  
]  
]  
},  
{  
"id": "838fdc96.c1b3d8",  
"type": "switch",  
"z": "44ed0376.b98684",  
"name": "",  
"property": "payload",  
"propertyType": "msg",  
"rules": [  
{  
"t": "true"  
},  
{  
"t": "false"  
}  
],  
"checkall": "true",  
"repair": false,  
"outputs": 2,  
"x": 650,  
"y": 200,  
"wires": [  
[  
"e5c02581.c68a98",  
"4304d57f.842d04"  
],  
[  
"f1bf5b71.0a5dd",  
"4304d57f.842d04"  
]  
]  
},  
{  
"id": "d9aac502.1d4988",  
"type": "debug",  
"z": "44ed0376.b98684",  
"name": "",  
"active": true,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "false",  
"x": 1010,  
"y": 200,  
"wires":   
},  
{  
"id": "4304d57f.842d04",  
"type": "debug",  
"z": "44ed0376.b98684",  
"name": "",  
"active": true,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "false",  
"x": 810,  
"y": 200,  
"wires":   
},  
{  
"id": "f1bf5b71.0a5dd",  
"type": "change",  
"z": "44ed0376.b98684",  
"name": "",  
"rules": [  
{  
"t": "set",  
"p": "payload",  
"pt": "msg",  
"to": "OFF",  
"tot": "str"  
}  
],  
"action": "",  
"property": "",  
"from": "",  
"to": "",  
"reg": false,  
"x": 820,  
"y": 240,  
"wires": [  
[  
"87e24825.fb009",  
"d9aac502.1d4988"  
]  
]  
},  
{  
"id": "af63855f.695e08",  
"type": "mqtt-broker",  
"z": "",  
"name": "MQTT",  
"broker": "192.168.5.81",  
"port": "1883",  
"clientid": "",  
"usetls": false,  
"compatmode": true,  
"keepalive": "60",  
"cleansession": true,  
"birthTopic": "",  
"birthQos": "0",  
"birthRetain": "true",  
"birthPayload": "",  
"closeTopic": "",  
"closeQos": "0",  
"closeRetain": "false",  
"closePayload": "",  
"willTopic": "",  
"willQos": "0",  
"willRetain": "false",  
"willPayload": ""  
},  
{  
"id": "a18c05f4.86c9d",  
"type": "mqtt-broker",  
"z": "",  
"name": "MQTT",  
"broker": "192.168.5.81",  
"port": "1883",  
"clientid": "",  
"usetls": false,  
"compatmode": true,  
"keepalive": "60",  
"cleansession": true,  
"birthTopic": "",  
"birthQos": "0",  
"birthRetain": "true",  
"birthPayload": "",  
"closeTopic": "",  
"closeQos": "0",  
"closeRetain": "false",  
"closePayload": "",  
"willTopic": "",  
"willQos": "0",  
"willRetain": "false",  
"willPayload": ""  
}  
]

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2020 15:26 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/7 "2020-01-24T15:26:11Z")

</div>

You can do all that in one go using a Change node configured like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcb58f72a9f86319b8f6c4d942cb122efd074912.png)  
That JSONata expression tests msg.payload.occupancy and if it is true sets the payload to ON, otherwise it sets it to OFF.

For future reference when you posting a flow do it as described in this post so that it is importable.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [24 January 2020 15:53 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/8 "2020-01-24T15:53:24Z")

</div>

Many thanks Colin. I set this up to learn how to use the Trigger node.  
Your elegant solution solves the problem without a Trigger node.  
Point taken with sending code.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2020 16:06 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/9 "2020-01-24T16:06:09Z")

</div>

Could you edit your previous post so that the flow is importable? I can't understand why you would use a Trigger node and would be interested to see. I can see why you would use a Switch node but not a Trigger.

---

<div class="post-metadata">

**Author:** ![BrianD](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrianD](https://discourse.nodered.org/u/BrianD)\
**Post date:** [25 January 2020 19:31 UTC](https://discourse.nodered.org/t/ikea-motion-sensor/20781/10 "2020-01-25T19:31:13Z")

</div>

Hi Colin here is my previous attempt.

```auto
[
    {
        "id": "94725b84.0e0c7",
        "type": "mqtt in",
        "z": "44ed0376.b98684",
        "name": "",
        "topic": "zigbee2mqtt/pirsensor",
        "qos": "0",
        "datatype": "json",
        "broker": "af63855f.695e08",
        "x": 180,
        "y": 260,
        "wires": [
            [
                "81b51117.2432e"
            ]
        ]
    },
    {
        "id": "81b51117.2432e",
        "type": "change",
        "z": "44ed0376.b98684",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload.occupancy",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 260,
        "wires": [
            [
                "ad778b7f.1525e8"
            ]
        ]
    },
    {
        "id": "ad778b7f.1525e8",
        "type": "switch",
        "z": "44ed0376.b98684",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 690,
        "y": 260,
        "wires": [
            [
                "7e61cd22.a997e4"
            ],
            [
                "8f46b678.6417a"
            ]
        ]
    },
    {
        "id": "7e61cd22.a997e4",
        "type": "change",
        "z": "44ed0376.b98684",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "ON",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 880,
        "y": 240,
        "wires": [
            [
                "e6f210f9.632e88"
            ]
        ]
    },
    {
        "id": "8f46b678.6417a",
        "type": "change",
        "z": "44ed0376.b98684",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "OFF",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 880,
        "y": 280,
        "wires": [
            [
                "47756ef8.22bb3"
            ]
        ]
    },
    {
        "id": "47756ef8.22bb3",
        "type": "trigger",
        "z": "44ed0376.b98684",
        "op1": "",
        "op2": "OFF",
        "op1type": "nul",
        "op2type": "str",
        "duration": "15",
        "extend": false,
        "units": "s",
        "reset": "ON",
        "bytopic": "all",
        "name": "",
        "x": 1090,
        "y": 280,
        "wires": [
            [
                "e6f210f9.632e88"
            ]
        ]
    },
    {
        "id": "e6f210f9.632e88",
        "type": "mqtt out",
        "z": "44ed0376.b98684",
        "name": "",
        "topic": "sonofftest/cmnd/POWER",
        "qos": "",
        "retain": "",
        "broker": "a18c05f4.86c9d",
        "x": 1290,
        "y": 240,
        "wires": []
    },
    {
        "id": "af63855f.695e08",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT",
        "broker": "192.168.5.81",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "true",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": ""
    },
    {
        "id": "a18c05f4.86c9d",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT",
        "broker": "192.168.5.81",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "true",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": ""
    }
]

```
