Function help with comparing two items

I'm not sure if this is possible, but i'm using the function below I grabbed from someone that turns off/on a lamp through Alexa commands on/off. There is a option to dim/brighten in the Alexa command. I put a debug node and it changes in the percentage field (screenshot below) when looking at the payload. The function i'm using looks like it stores it in var b and I want to compare the percentage coming in from the payload has changed, if it has I want to change the percentage of the light.

Is this possible?

// Enter your Light entity and if used the Effect Name
var light = "light.printer_lamp";
var effect = "Cool white"; 

const cs = global.get('homeassistant').homeAssistant.states[light].state; //current state
const cc = global.get('homeassistant').homeAssistant.states[light].attributes.rgb_color; //current color
const cb = global.get('homeassistant').homeAssistant.states[light].attributes.brightness; //current brightness

var s = context.get('s'); // These lines retrieve the lables 's', 'c' & 'b' 
var c = context.get('c'); // from memory and apply thier values to the
var b = context.get('b'); // variables s, c & b used to retun the Light state

if (msg.payload === "off" && s == "off") {
    msg.payload =  {"service": "turn_on", data:{"entity_id": light, "rgb_color": c,"brightness": b, "effect":"none"}};
    msg.payload =  {"service": "turn_off", data:{"entity_id": light}};
    }

if (msg.payload === "off") {
    msg.payload =  {"service": "turn_off", data:{"entity_id": light}}; //"rgb_color": c,
    }

if (msg.payload === "on") {
    context.set('s', cs); // These line save the values of cs, cc & cb
    context.set('c', cc); // and save them in memory with lables:
    context.set('b', cb); // 's', 'c' & 'b' 
    
    // 'Alarm state' will be RED & 100% Brighness - Adjust this line to suit your needs
    msg.payload =  {"service": "turn_on",data:{"entity_id": light, "effect":effect}};
    }

return msg
return msg;

image

Hi @CAL37

Barely anyone here uses HA - most are Die Hard Node RED users :smile: , so we are not going to know what you receive, what it expects, or how it interacts with Node RED.

With that said, I'll tackle this in the way Node RED works
if the incoming brightness is stored in some path, example; msg.payload.brightness

and you want to compare it with the stored value.

... Original code above these lines

var s = context.get('s'); 
var c = context.get('c');
var b = context.get('b');

if(msg.payload.brightness !== b){
    b = <your new value>; /* update value */
    context.set('b',b);   /* commit value to context so the next execution will use the updated value */
}

... Remaining code continues

@marcus-j-davies Thank you again! I'm also a die hard fan also and all of my automations are in NodeRed. I do need to learn java so I can expand on what I can do here.

1 Like

learn JavaScript not Java two different languages.

As already pointed out, it is JavaScript. However, you have elected to do all this in a function when most (if not all of it) could/should have been done visually with no-code nodes.

What I'm trying to say is, there are many tasks (this code included) that typically doesn't need any JavaScript at all.

1 Like

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