Listening to Hue Events

Hi, been using node red for some years now but I've become stuck.
Basically I had a flow using Hue Magic hue light node to turn on some lights in the garden if motion detected (I use the function node to set the payload with the brightness and colour) and then automatically turn off after 5 minutes of no motion. Here is the flow in question:

[
    {
        "id": "3b65ef53ef04260d",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c7af6971269d3a96",
        "type": "api-current-state",
        "z": "3b65ef53ef04260d",
        "name": "Lux Level <= 5",
        "server": "819762069142e0b9",
        "version": 3,
        "outputs": 2,
        "halt_if": "5",
        "halt_if_type": "num",
        "halt_if_compare": "lte",
        "entity_id": "sensor.patio_sensor_illuminance",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 320,
        "y": 200,
        "wires": [
            [
                "ae7e8ac258fd7685"
            ],
            []
        ]
    },
    {
        "id": "ae7e8ac258fd7685",
        "type": "function",
        "z": "3b65ef53ef04260d",
        "name": "Patio Brightness",
        "func": "\nvar brightness = Number();\nvar setColour = String();\nvar currentDate = new Date();\nvar currentTime = (currentDate.getHours()<10?'0':'') + currentDate.getHours() +\":\" +\n                    (currentDate.getMinutes()<10?'0':'') + currentDate.getMinutes()\n                  \nvar isNightTime = currentTime > \"22:45\" || currentTime < \"06:00\";                  \n    \n    if (!isNightTime)\n    {\n        brightness = 40\n    \n    }\n    else\n    {\n        brightness = 5\n    \n    }\n    \n    msg.payload = {\"on\":true, \n                   \"brightness\":brightness};\n    \n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 260,
        "wires": [
            [
                "4f1a5b41ce60acd0"
            ]
        ]
    },
    {
        "id": "8e20ebba20455d71",
        "type": "stoptimer",
        "z": "3b65ef53ef04260d",
        "duration": "3",
        "units": "Minute",
        "payloadtype": "bool",
        "payloadval": "false",
        "name": "Turn off Patio lights after 3 minutes",
        "x": 920,
        "y": 380,
        "wires": [
            [
                "a088b31fe5ad7514"
            ],
            []
        ],
        "outputLabels": [
            "off",
            ""
        ]
    },
    {
        "id": "4f1a5b41ce60acd0",
        "type": "hue-light",
        "z": "3b65ef53ef04260d",
        "name": "PL1",
        "bridge": "bffaf6c.ff5b208",
        "lightid": "c1b99a5a-4ab3-4d5f-ba60-5b9a06be7c55",
        "colornamer": true,
        "skipevents": false,
        "initevents": false,
        "x": 710,
        "y": 320,
        "wires": [
            [
                "8e20ebba20455d71"
            ]
        ]
    },
    {
        "id": "9721278e6ad47bcc",
        "type": "server-state-changed",
        "z": "3b65ef53ef04260d",
        "name": "Patio Sensor",
        "server": "819762069142e0b9",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "binary_sensor.patio_sensor_motion",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "habool",
        "haltifstate": "true",
        "halt_if_type": "bool",
        "halt_if_compare": "is",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": "0.5",
        "forType": "num",
        "forUnits": "seconds",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 130,
        "y": 120,
        "wires": [
            [
                "c7af6971269d3a96"
            ],
            []
        ]
    },
    {
        "id": "a088b31fe5ad7514",
        "type": "api-call-service",
        "z": "3b65ef53ef04260d",
        "name": "Turn off Patio lights ",
        "server": "819762069142e0b9",
        "version": 5,
        "debugenabled": true,
        "domain": "light",
        "service": "turn_off",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "light.pl1"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 1170,
        "y": 460,
        "wires": [
            []
        ]
    },
    {
        "id": "819762069142e0b9",
        "type": "server",
        "name": "Home Assistant",
        "version": 4,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": "at: ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "h23",
        "statusTimeFormat": "h:m"
    },
    {
        "id": "bffaf6c.ff5b208",
        "type": "hue-bridge",
        "name": "Philips hue",
        "bridge": "192.168.1.91",
        "key": "xxxxxx",
        "worker": "1",
        "disableupdates": false
    }
]

But due to some issues I've decided to go down the Hue API V2 route and managed to get a flow working to turn on a light. But I still need to turn the lights off automatically after a certain time. So I was thinking of using philipshue-events node by yadomi.

I thought there would be a simple way of listening to the events and match the light guid with the on state as per this example:

{
  "creationtime": "2021-10-18T17:04:55Z",
  "data": [
    {
      "id": "e706416a-8c92-46ef-8589-3453f3235b13",
      "on": { "on": true },
      "owner": {
        "rid": "3f4ac4e9-d67a-4dbd-8a16-5ea7e373f281",
        "rtype": "device"
      },
      "type": "light"
    }
  ],
  "id": "9de116fc-5fd2-4b74-8414-0f30cb2cbe04",
  "type": "update"
}

My thought process would be if the id = the lights guid and the on state = true I could then start the timer using the stoptimer node and then call the Hue APi again to turn off the light.

I have 3 lights so I would need 3 timers (maybe 2), I wrote a small bit of Js in the function node which is included in this flow:

[
    {
        "id": "988c6b12.5820a8",
        "type": "tab",
        "label": "test 4",
        "disabled": false,
        "info": ""
    },
    {
        "id": "eff1e6728336513a",
        "type": "function",
        "z": "988c6b12.5820a8",
        "name": "HUE API V2",
        "func": "// you can copy the user key from the hue config node out of Huemagic\nvar user = \"\";\n\n\nif(msg.endpoint === undefined)\n msg.url = \"https://192.168.1.91/clip/v2/resource\";\nelse if(msg.topic === undefined)\n msg.url = \"https://192.168.1.91/clip/v2/resource/\" + msg.endpoint;\nelse\n msg.url = \"https://192.168.1.91/clip/v2/resource/\" + msg.endpoint + \"/\" + msg.topic;\n\nmsg.headers = {};\n\nmsg.headers['hue-application-key'] = user;\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['Accept'] = 'application/json';\n\nif(msg.payload != undefined)\n msg.headers['ContentLength'] = JSON.stringify(msg.payload).length;\n\nif(msg.verb === undefined)\n msg.method = \"get\";\nelse\n msg.method = msg.verb;\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1130,
        "y": 120,
        "wires": [
            [
                "eeebf175d527703f"
            ]
        ]
    },
    {
        "id": "eeebf175d527703f",
        "type": "http request",
        "z": "988c6b12.5820a8",
        "name": "https request HUE API V2",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "4bc31af9bd03bbdc",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 1370,
        "y": 120,
        "wires": [
            [
                "a4afb3c3cccfbbd9"
            ]
        ]
    },
    {
        "id": "c56c22aea1a2f7b3",
        "type": "api-current-state",
        "z": "988c6b12.5820a8",
        "name": "Lux Level <= 10",
        "server": "819762069142e0b9",
        "version": 3,
        "outputs": 2,
        "halt_if": "400",
        "halt_if_type": "num",
        "halt_if_compare": "lte",
        "entity_id": "sensor.shed_interior_sensor_illuminance",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 320,
        "y": 120,
        "wires": [
            [
                "be8ca08cb4d303aa"
            ],
            []
        ]
    },
    {
        "id": "ad536497a95e358f",
        "type": "server-state-changed",
        "z": "988c6b12.5820a8",
        "name": "Mancave motion",
        "server": "819762069142e0b9",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "binary_sensor.shed_interior_sensor_motion",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "habool",
        "haltifstate": "true",
        "halt_if_type": "bool",
        "halt_if_compare": "is",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": "0.1",
        "forType": "num",
        "forUnits": "seconds",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [],
        "x": 100,
        "y": 120,
        "wires": [
            [
                "c56c22aea1a2f7b3",
                "be8ca08cb4d303aa"
            ],
            []
        ]
    },
    {
        "id": "bea7e0e8ddaa9864",
        "type": "change",
        "z": "988c6b12.5820a8",
        "name": "Shed light on",
        "rules": [
            {
                "t": "set",
                "p": "endpoint",
                "pt": "msg",
                "to": "light",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "e7514da3-6846-4f41-b2de-0b8981c28a76",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"on\":{\"on\":true},\"dimming\":{\"brightness\":50},\"dynamics\":{\"duration\":1000},\"color\":{\"xy\":{\"x\":0.3,\"y\":0.3}}}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "verb",
                "pt": "msg",
                "to": "put",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 790,
        "y": 120,
        "wires": [
            [
                "eff1e6728336513a"
            ]
        ]
    },
    {
        "id": "be8ca08cb4d303aa",
        "type": "function",
        "z": "988c6b12.5820a8",
        "name": "Mancave Brightness",
        "func": "var brightness = Number();\nvar setColourX = Number();\nvar setColourY = Number();\n\nvar currentDate = new Date();\nvar currentTime = (currentDate.getHours() < 10 ? '0' : '') + currentDate.getHours() + \":\" +\n(currentDate.getMinutes() < 10 ? '0' : '') + currentDate.getMinutes()\n\nbrightness = 40\nsetColourX = 0.3\nsetColourY = 0.3\n\nmsg.payload = {\n    \"on\": {\n        \"on\": true\n    },\n    \"dimming\": {\n        \"brightness\": 50\n    },\n    \"dynamics\": {\n        \"duration\": 1000\n    },\n    \"color\": {\n        \"xy\": {\n            \"x\": 0.3,\n            \"y\": 0.3\n        }\n    }\n};\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 580,
        "y": 120,
        "wires": [
            [
                "bea7e0e8ddaa9864"
            ]
        ]
    },
    {
        "id": "a8167b5772523342",
        "type": "philipshue-events",
        "z": "988c6b12.5820a8",
        "bridge": "f397567560dcf393",
        "x": 100,
        "y": 200,
        "wires": [
            [
                "40a462c02de10305"
            ]
        ]
    },
    {
        "id": "83e910890883354a",
        "type": "switch",
        "z": "988c6b12.5820a8",
        "name": "",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "e7514da3-6846-4f41-b2de-0b8981c28a76",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 470,
        "y": 200,
        "wires": [
            [
                "0e55037c8d554695"
            ],
            []
        ]
    },
    {
        "id": "4e354bc74817855f",
        "type": "change",
        "z": "988c6b12.5820a8",
        "name": "Shed light off",
        "rules": [
            {
                "t": "set",
                "p": "endpoint",
                "pt": "msg",
                "to": "light",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "e7514da3-6846-4f41-b2de-0b8981c28a76",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"on\":{\"on\":false}}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "verb",
                "pt": "msg",
                "to": "put",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 200,
        "wires": [
            [
                "eff1e6728336513a"
            ]
        ]
    },
    {
        "id": "0e55037c8d554695",
        "type": "stoptimer",
        "z": "988c6b12.5820a8",
        "duration": "20",
        "units": "Second",
        "payloadtype": "bool",
        "payloadval": "false",
        "name": "Turn off Shed Lights after 3 minutes",
        "x": 760,
        "y": 200,
        "wires": [
            [
                "4e354bc74817855f"
            ],
            []
        ],
        "outputLabels": [
            "off",
            ""
        ]
    },
    {
        "id": "40a462c02de10305",
        "type": "function",
        "z": "988c6b12.5820a8",
        "name": "function 2",
        "func": "\n\n\nif( msg.payload.data[0].id == 'e7514da3-6846-4f41-b2de-0b8981c28a76'\n   && msg.payload.data[0].on == true ){\n     msg.topic = 'e7514da3-6846-4f41-b2de-0b8981c28a76'\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 200,
        "wires": [
            [
                "83e910890883354a"
            ]
        ]
    },
    {
        "id": "a4afb3c3cccfbbd9",
        "type": "debug",
        "z": "988c6b12.5820a8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1590,
        "y": 120,
        "wires": []
    },
    {
        "id": "4bc31af9bd03bbdc",
        "type": "tls-config",
        "name": "",
        "cert": "",
        "key": "",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "servername": "",
        "verifyservercert": false
    },
    {
        "id": "819762069142e0b9",
        "type": "server",
        "name": "Home Assistant",
        "version": 4,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": "at: ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "h23",
        "statusTimeFormat": "h:m"
    },
    {
        "id": "f397567560dcf393",
        "type": "philipshue-events-config",
        "address": "192.168.1.91",
        "applicationkey": ""
    }
]

Any help would be appreciated

Thanks
Darren

After some trial an error I have come up with this function which interrogates the event from the Hue Hub and returns the ID, I might just change the function to evaluate msg.payload.data[0].on.on and return the ID into the Topic if True, The Switch node will then take care of the rest .

const manCaveLight = 'ID of light';
const patioLight = ''ID of light';
const shedLed = ''ID of light;
const gardenLed = 'ID of light';


if( msg.payload.data[0].id === manCaveLight
   && msg.payload.data[0].on.on === true ){
     msg.topic = manCaveLight
}
else if (msg.payload.data[0].id === patioLight
   && msg.payload.data[0].on.on === true){
     msg.topic = patioLight
}
else if (msg.payload.data[0].id === shedLed
   && msg.payload.data[0].on.on === true){
     msg.topic = shedLed
       
   }
else if (msg.payload.data[0].id === gardenLed
   && msg.payload.data[0].on.on === true){
     msg.topic = gardenLed
       
   }
   
   
return msg;

Here is my flow:

Hi. Here is an example of same thing using a lookup table. Hope it gives you some ideas

[{"id":"7b563836.da0618","type":"inject","z":"9a86d59e88442f2c","name":"garden","props":[{"p":"msg.payload.data[0].on.on","v":"true","vt":"bool"},{"p":"payload.data[0].id","v":"123456789011","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":200,"wires":[["11373201.a198e6"]]},{"id":"11373201.a198e6","type":"function","z":"9a86d59e88442f2c","name":"","func":"const lookup = {\n    \"ID of light1\": \"manCaveLight\",\n    \"ID of light2\": \"patioLight\",\n    \"ID of light3\": \"shedLed\",\n    \"123456789011\": \"gardenLed\"\n};\n\nif(msg.payload.data[0].on.on === true){\n    msg.topic = lookup[msg.payload.data[0].id];\n}else{\n    msg = null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":200,"wires":[["75bd5896.d0bad"]]},{"id":"3a75696b.88a646","type":"inject","z":"9a86d59e88442f2c","name":"cave","props":[{"p":"msg.payload.data[0].on.on","v":"true","vt":"bool"},{"p":"payload.data[0].id","v":"ID of light1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":240,"wires":[["11373201.a198e6"]]},{"id":"75bd5896.d0bad","type":"debug","z":"9a86d59e88442f2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":200,"wires":[]}]
const lookup = {
    "ID of light1": "manCaveLight",
    "ID of light2": "patioLight",
    "ID of light3": "shedLed",
    "123456789011": "gardenLed"
};

if(msg.payload.data[0].on.on === true){
    msg.topic = lookup[msg.payload.data[0].id];
}else{
    msg = null;
}
return msg;

Only messages with ..on === true will be passed on all others return a null message.

Thank you E1cid,

Didn't know you could declare a constant that way, certainly makes the code more readable. Good call on nullifying the message.

Has given me some additional insights for sure

Thanks again for your input.