How to parse Node Red Get entities payload into separate variables (messages) for processing

I’m trying to read a bunch of sensors via the get entities nodes in nodeRED. THis is working fine problem is trying to parse them out into separate messages for processing.

I want to read light , motion , temperature data and do some logic for controlling other devices.

I know I need to use a function, key is i need help getting the syntax to break the array of data into individual (fixed) messages like temp, lx, motion, state.

I would appreciate any help.

I’m new myself so forgive me if I make any mistakes.

I’m using a sequence of an inject, function and debug nodes to get the states of all my entities. Inside the function node I have this code:

var states = global.get('homeassistant').homeAssistant.states;
msg.payload=states;
return msg;

After that, from the debug messages tab I search for the entity I want to control and then use the “copy path” and “copy value” buttons to get what I want and store it in a variable. For example:

var Floor_Lamp_bright = states['light.floor_lamp'].attributes.brightness >= 128;

After that it’s a matter of if-else if statements.

Hey lex it seem a bit long winded, since current state outputed to a debug node set to full msg will give you that without having to grab all the state your doing in the var state
No

Current state node will get you the state of one entity only, as far as I understand it. This way you are getting the current states of all entities and you can combine them in your if statements as you see fit.

For example, if it’s between midnight and dawn and you’re home and all the lights are off and the floor lamp is on and below 40% brightness or off and there’s movement in a certain pir sensor, set the brightness at 40% and at red color (that is actually my nightlight sequence for the times I get up in the middle of the night and want a glass of water or want to go to the bathroom).

Thanks Alex I think this will work albeit pulling all the data and not being able to use the other entity get .

Amazing thing about NODE-RED you can do staff many different ways.

Harry,
I could use your method as well, however I’m looking to get many variables at once.

How many ?
you do know you can create fields as you go within the object
so you can retain the info as you go
below is an example

this is the object i end up with

msg : Object
object
_msgid: "cb44a4ef.29e048"
topic: ""
payload: 1577072227839
light-state: "5.84"
light: object
entity_id: "sensor.bedroom_sensor_light_level"
state: "5.84"
attributes: object
last_changed: "2019-12-23T03:35:12.044087+00:00"
last_updated: "2019-12-23T03:35:12.044087+00:00"
context: object
timeSinceChangedMs: 115797
temp-state: "21.13"
temp: object
entity_id: "sensor.bedroom_sensor_temperature"
state: "21.13"
attributes: object
last_changed: "2019-12-23T03:29:48.039843+00:00"
last_updated: "2019-12-23T03:29:48.039843+00:00"
context: object
timeSinceChangedMs: 439804
motion-state: "off"
motion: object
entity_id: "binary_sensor.bedroom_sensor_motion"
state: "off"
attributes: object
last_changed: "2019-12-22T23:58:37.054153+00:00"
last_updated: "2019-12-22T23:58:37.054153+00:00"
context: object
timeSinceChangedMs: 13110790