Toogle value with a button

Hi all
I am trying to toogle a value too control my light. I have a payload with a value 0-100, and I want a button(Ikea) to toogle the value if it over 0, then set it to 0, and if it is 0 then set it to 40.
If I push the button, it always give me a "0"

[
    {
        "id": "f15e9b1906385ff2",
        "type": "function",
        "z": "72a01dcd34706a98",
        "name": "Toggle Brightness",
        "func": "var Value = msg.payload;\nvar Button = msg.payload.buttonevent;\nif (Button === 1002) {\n    if (Value === 0) {\n        Value = 40;\n    } else {\n        Value = 0;\n    }\n}\nmsg.payload = Value;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 550,
        "y": 600,
        "wires": [
            [
                "d273438d6b622696"
            ]
        ]
    },
    {
        "id": "d273438d6b622696",
        "type": "debug",
        "z": "72a01dcd34706a98",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 600,
        "wires": []
    },
    {
        "id": "58ea0f732023cf3c",
        "type": "deconz-input",
        "z": "72a01dcd34706a98",
        "name": "",
        "topic": "",
        "config_version": 2,
        "server": "4b1fba61.3934e4",
        "statustext": "",
        "statustext_type": "default",
        "search_type": "device",
        "device_list": [
            "sensors/uniqueid/0c:43:14:ff:fe:41:7e:94-01-1000"
        ],
        "device_name": "",
        "query": "{}",
        "outputs": 1,
        "output_rules": [
            {
                "type": "state",
                "format": "single",
                "output": "onupdate",
                "onstart": false,
                "payload": [
                    "__complete__"
                ],
                "uniqueId": "ui-id-6"
            }
        ],
        "x": 170,
        "y": 880,
        "wires": [
            [
                "f15e9b1906385ff2"
            ]
        ]
    },
    {
        "id": "5753bff37f112f1b",
        "type": "inject",
        "z": "72a01dcd34706a98",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 190,
        "y": 600,
        "wires": [
            [
                "f15e9b1906385ff2"
            ]
        ]
    },
    {
        "id": "80f5cd71a5f0c35c",
        "type": "inject",
        "z": "72a01dcd34706a98",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "50",
        "payloadType": "num",
        "x": 190,
        "y": 680,
        "wires": [
            [
                "f15e9b1906385ff2"
            ]
        ]
    },
    {
        "id": "85474c477996a68e",
        "type": "inject",
        "z": "72a01dcd34706a98",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "100",
        "payloadType": "num",
        "x": 190,
        "y": 760,
        "wires": [
            [
                "f15e9b1906385ff2"
            ]
        ]
    },
    {
        "id": "4b1fba61.3934e4",
        "type": "deconz-server",
        "name": "Phoscon-GW",
        "config_version": 1,
        "ip": "192.168.0.44",
        "port": "8080",
        "ws_port": "443",
        "secure": false,
        "polling": "3"
    }
]

I would probably use a join node a switch node and JSONata in a change node.
Maybe this example will help.

[{"id":"5753bff37f112f1b","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":130,"y":2120,"wires":[["27b6d2ad4a558679"]]},{"id":"27b6d2ad4a558679","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"value","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":2220,"wires":[["6649b7c38e6de477"]]},{"id":"80f5cd71a5f0c35c","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":130,"y":2200,"wires":[["27b6d2ad4a558679"]]},{"id":"85474c477996a68e","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":130,"y":2280,"wires":[["27b6d2ad4a558679"]]},{"id":"6649b7c38e6de477","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":2220,"wires":[["ef905c0a9f538f02"]]},{"id":"5216e4dd8914a206","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"deconz","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":2400,"wires":[["6649b7c38e6de477"]]},{"id":"ef905c0a9f538f02","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"deconz","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":590,"y":2220,"wires":[["ba9cbcca4715c2b9"]]},{"id":"65ad247087cb6cbb","type":"inject","z":"b9860b4b9de8c8da","name":"deconz","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":2400,"wires":[["5216e4dd8914a206"]]},{"id":"ba9cbcca4715c2b9","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.value = 0 ? 40 : 0","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":2220,"wires":[["d273438d6b622696"]]},{"id":"d273438d6b622696","type":"debug","z":"b9860b4b9de8c8da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":2120,"wires":[]}]

HI E1cid
Thanks, it works with a littel tweak.
I haved to send the payload back, so it toogle the value.

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