Javascript in general: Dynamic Object Literals

I'm way above my head here in javascript but why does this not put neither the "tidsPunkt" nor the msg.payload into rommet.profil[dag] ?

    if (rommet.hasOwnProperty("profil") === false) {
        rommet.profil = {};
    }

    var returnedObject = Object.assign(rommet.profil, {[dag]: {[tidsPunkt]: msg.payload}});

Norwegian dictionary:
Rommet = the room
Dag = day
Tidspunkt = time
Profil = profile

Via an RBE node I’m trying to save every new setpoint of several rooms in a profile object from another (KNX RF) system. (I’m also rounding the times to nearest quarter of an hour).

Why? Because I’m going to ramp the collection of points according to room heating coefficient so my main system is not taken by surprise each time a setpoint suddenly jumps or falls.

Added a check for the nested 'dag' (Day) property too. I really thought Object.assign would merge (Add object) if nested differently as well.

Where does rommet come from ?

To add a new object (i assume that is what you want, not sure as there is context missing) you don't need to use Object.assign. Object.assign wil clone the object, unless that is what you want.

There is a node to emulate a ramping thermostat. that includes hysteresis, perhaps that could help ?

rommet is copied from part of the nested context data.

I guess Object.assign is not needed (certainly not the cloning part) as it didn't do what I thought with regards to merging. Went back to rommet.profil[dag][tidsPunkt] = msg.payload after checking for existence of all levels of nesting.

I am actually going to use that contrib-node, but I want to set the profiles dynamically from that other heating system (on a weekly delay obviously).

but I want to set the profiles dynamically

It supports dynamic profiles.

Regardless whether you have used this approach or not... for the sake of completeness:

In your code example, you need to remove the [] brackets, then it will work as expected.

Object.assign(rommet.profil, {dag: {tidsPunkt: msg.payload}});

And note that the return value of Object.assign() is the object in rommet.profil, not rommet.

1 Like

Yes, I know. I garble my sentences in English sometimes. I meant that I want automatically retrieve and dynamically store new setpoints in different daily profiles per room in that node vs just manually copying over setpoints to that node.