Saving a single value to context

{
  "192.168.1.10": {
      "last_on": 1622751269764,
      "last_off": 1622751272068
    },
    "192.16.1.13": {
      "last_on": 1622751772269,
      "last_off": 1622751774284
    }
}

Working demo updating the values of the objects properties...

[{"id":"4d40b13c.c8309","type":"function","z":"1bd2b382.4dd4f4","name":"setup demo data","func":"msg.payload = {\n  \"192.168.1.10\": {\n      \"last_on\": 1622751269764,\n      \"last_off\": 1622751272068\n    },\n    \"192.16.1.13\": {\n      \"last_on\": 1622751772269,\n      \"last_off\": 1622751774284\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":648,"y":256,"wires":[["2263910a.99b7ae"]]},{"id":"d96e022b.81642","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":412,"y":256,"wires":[["4d40b13c.c8309"]]},{"id":"2263910a.99b7ae","type":"change","z":"1bd2b382.4dd4f4","name":"","rules":[{"t":"set","p":"deviceMonitor","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":882,"y":256,"wires":[[]]},{"id":"22b4dbc7.70a144","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.10","payload":"on","payloadType":"str","x":432,"y":304,"wires":[["b241fd6f.3bc79"]]},{"id":"b241fd6f.3bc79","type":"function","z":"1bd2b382.4dd4f4","name":"update value","func":"\nvar deviceMonitor = flow.get(\"deviceMonitor\") || {};\nvar device = deviceMonitor[msg.topic];\nif(!device) {\n    deviceMonitor[msg.topic] = {\n        last_on: null,\n        last_off: null,\n    }\n    device = deviceMonitor[msg.topic]\n}\nswitch(msg.payload) {\n    case \"on\": \n        device.last_on = Date.now();\n        break;\n    case \"off\": \n        device.last_off = Date.now();\n        break;\n}\n\n//update flow context.\n//NOTE: This only sets the reference (doesnt re-create the object\n//      and is infact not even needed.  E.g.  if you comment the \n//      next line out, everything still works!\nflow.set(\"deviceMonitor\", deviceMonitor);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":646,"y":304,"wires":[[]]},{"id":"a9d4c512.088868","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.10","payload":"off","payloadType":"str","x":432,"y":352,"wires":[["b241fd6f.3bc79"]]},{"id":"aa35fac0.f475f8","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.11","payload":"on","payloadType":"str","x":432,"y":400,"wires":[["b241fd6f.3bc79"]]},{"id":"7f4af8f9.d842e8","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.11","payload":"off","payloadType":"str","x":432,"y":448,"wires":[["b241fd6f.3bc79"]]},{"id":"fbc0bb0c.1c9d98","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.99","payload":"on","payloadType":"str","x":432,"y":496,"wires":[["b241fd6f.3bc79"]]},{"id":"c176579b.3d9bd8","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"192.168.1.99","payload":"off","payloadType":"str","x":432,"y":544,"wires":[["b241fd6f.3bc79"]]}]
1 Like