Scene activation and storage in NodeRED

I'm a long time user of NodeRED but have used it for quite simple things in my home automation so far and now I'm trying to do something more complex, than I can handle, therefore some help is needed.

So the thing I want implement in Nodered is a specific scene activation in some room when I press some button. E.g. all my devices in the room are controlled via MQTT, e.g. to switch the light on I just send MQTT message with at topic "/myhome/Living_Light" and payload "ON". Same is for dimmers and rollershutters, where the payload can be some number. Totally it can be up to 20 devices, which might be involved in the scene. So basically I want to store the latest state of device and activate it on demand. The idea is to have an "agnostic" system e.g. without explicit specifying of topics to be monitored/activated.

So basically what I want to do:

  • during normal operation I want to monitor all topic/payload commands, which were used in the room
  • once I issue "Store" command, these pairs should be stored in some variable(or multiple variables) in flow context
  • then once I activate a scene, NodeRED should inject these topic/payloads into MQTT.

Currently I'm struggling with first topic - e.g how to create a full list of topic:payload, which would be automatically updated once new message arrives. Any Ideas welcome.

So the third part is relatively easy

If I have a JSON object of this kind:

    "DimmedLight_Living": "0",
    "Shutter_Living_Center_Left_Lamella": "0",
    "Shutter_Living_Left": "DOWN"

e.g. where each topic is saved with it's value, then I can split it to individual messages using SPLIT node and post it via MQTT out node.

Now the question is how to create this array from incoming messages. Basically I need a flow, which would check if arrived message already exist in this array as a property and if i should include a new message into this array or update an existing property with new value.

With Objects you can update a property like this;

// Set whatever the default should be
let savedValues = context.get('savedValues') ?? {DimmedLight_Living: 0, Shutter_Living_Center_Left_Lamella:  0, Shutter_Living_Left:  "DOWN"}

let {DimmedLight_Living, Shutter_Living_Center_Left_Lamella, Shutter_Living_Left} = savedValues

// For example
if (msg.topic === 'light') DimmedLight_Living = msg.payload
// etc

// This will either maintain an existing value or update it
savedValues = {...savedValues, DimmedLight_Living: DimmedLight_Living}
// etc

context.set('savedvalues', savedValues)

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