Is there have a way to get entity's state in a function node?

Is there a way to get state in function node?
I need get several entity state and check if it reach the specified condition,
it’s a repetitive work to drag a new state , new judging function ,and combine then.that’s all the ways i can think of for now.
I would like use javascript to get state and check it.
Could anyone can gime a way to do this?please.
And how can i get all variables in one flow ? to find some function hidden and unmastered.

Try this:

[{"id":"fcb8501ba4e0c02b","type":"function","z":"be03f9025a1f137a","name":"function 1","func":"msg.sun = global.get('homeassistant').homeAssistant.states[\"sun.sun\"];\nmsg.sun_state = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].state;\nmsg.sun_attr_next_dawn = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].attributes.next_dawn;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":375,"y":75,"wires":[["36f148efe227cbac"]]}]

You can find the values by opening up the Context Data in the upper right icon menu and then refresh the Global section.

1 Like

TYSM,WallyR
It works,really nice.

And Could you teach me more,about how to use the other node in function node by scripts.
For example: http request node, how to use it in function node.

I think I would use the http request node as it is and then just use the function node to do the computation on the result.

okay,i see,but sometimes, we should call service ,send request and get state in a one judging condition, need lots of line connected,a little bit messy
anyway ,thanks

I know, but a function node is only javascript and I do not know if javascript actually have that functionality at all.
Nodes can be made dynamic by putting values in msg.objects that match does names the next node needs. That is my best proposal to optimize it.

1 Like

Putting everything in a function node half defeats the power of Node Red by hiding away what is going on. Furthermore, it makes more of your code synchronous, blocking other nodes from running. Node Red removes the complexity of handling asynchronous calls for you - if you put everything in a function node, you’re going to have to deal with it yourself, further complicating your code.

If you’ve got a lot of entities you could put them in a group (in HA) and then use the “Get Entities” node and get them all (using “in group”) with just one node. You could also filter the group in the same node just to get the “on” entities (for example).

1 Like

I somewhat agree on that.
A function node can be too complex and too complicated, but an user made function node is nothing more than what all the other nodes are in your palette and some of them are pretty complex too.

If you can describe the function of the node in the name, then I would say it is good enough.

1 Like

nice,i can’t agree more,
How to monitor the status of a entity?
for example:
when a light turn on , i do something for it.
i use a inject ,1 times/s to trigger state node, that method maybe a little bit stupid,and reaction slowly

Maybe post what you’ve got (picture plus code). You shouldn’t need an inject node to monitor an entity - either just a “events: state” or “trigger: state” node, depending on your needs.

1 Like