# Using values from entities directly in function nodes

**URL:** <https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267>\
**Category:** General\
**Created:** [11 July 2021 12:22 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267 "2021-07-11T12:22:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 12:22 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/1 "2021-07-11T12:22:42Z")

</div>

Hi there,  
I´m again struggling with my "Home Assistant" integration in Node Red which actually provides global variables for each entity of the system. For what reason ever, all values are provided formated as a string which makes it really hard to use when you need to operate with numeric dates (temperature, humidity etc.). Therefore, I´m looking for a way to directly get the numeric state of certain entities within a function node to operate with them further. Anybody that has done that before and maybe give an example how to achieve this?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 July 2021 18:47 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/2 "2021-07-11T18:47:39Z")

</div>

Can you show the actual data?

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 19:00 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/3 "2021-07-11T19:00:27Z")

</div>

@zenofmud - You mean of the global variables from Home Assistant?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 July 2021 19:03 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/4 "2021-07-11T19:03:43Z")

</div>

Of the data going into the function node. Make sure to set the debug node to display the complete msg object.

It would also be helpful to see the function node.

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 19:14 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/5 "2021-07-11T19:14:05Z")

</div>

Alright, maybe I should be more clear of the scenario I want to cover:

1. Motion sensor A detects motion (events state node)
2. Based on this I want to create a function node that checks the illumination entity of sensor B to decide whether to switch on the light or not.

So for this I want to know how to read the state of certain entities from within a function node.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 July 2021 19:23 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/6 "2021-07-11T19:23:56Z")

</div>

I'm gong to assume you have someway that you are durrently getting the information from sensor B. What I would do it store the value received from sensor B in a flow variable. Then when sensor A generates a msg, you could check the state of the flow variable (you could use a `switch` node) and determine if you should to turn on the light or not.

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 19:27 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/7 "2021-07-11T19:27:49Z")

</div>

Yes I know I could use a certain state node, but if you are operating with multiple sensors u need a lot of variables which I think is quite some overhead. So is there really no way to read values from an entity within a function node ?!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 July 2021 19:50 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/8 "2021-07-11T19:50:30Z")

</div>

What version of NR are you using?  
As of release 1.3.0 the `function` node allows you to load external modules. So if you have a node module that will read the senson you could investigate that.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 July 2021 20:03 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/9 "2021-07-11T20:03:09Z")

</div>

You can convert the values with a change node

 ![Screenshot 2021-07-11 at 21.59.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/263a92683530f95a7f1d0b82a4d8e8efe5187500.jpeg)

I am using a similar situation (although I dont use home assistant, just everything in NR).

The ambient light sensor continuously updates a global variable and whenever the motion sensor is triggered, i use a switch node that checks the variable and either blocks or passes the message

 ![Screenshot 2021-07-11 at 22.02.36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5fc4bd3e003de36874dd0a2099b8c9ca0115fdec.jpeg)

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 20:13 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/10 "2021-07-11T20:13:43Z")

</div>

Thanks @bakman2 - Still this again is a solution that makes use of global variables, which I actually would like to avoid as I probably would need to constantly update about 20 global variables to use in different flows. But maybe that´s just the way it has to be once there is no possibility to directly read a value of an entity from within a function node.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 July 2021 20:22 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/11 "2021-07-11T20:22:00Z")

</div>

If you receive messages from multiple sensors at the same time (or grouped by number of messages or in a certain timeframe) you can do it, if not, you will need to keep track of the values as messages pass through the flow from a to z.

Note that node-red is not a “state” tracker, that is exactly what home assistant does.

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [11 July 2021 22:02 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/12 "2021-07-11T22:02:54Z")

</div>

Yeah, I guess u r right - it should actually be Home Assistant storing the data properly, but fails miserably doing so. I guess I have to get back using global/ flow variables for keeping track of entities states myself then 😒

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 July 2021 02:04 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/13 "2021-07-12T02:04:40Z")

</div>

So what version of the Home Assistant Webhooks add-in are you using ?

Are you using a fullcopy of NR - or the copy from HACS?

Have you checked on the Home Assistant forum re this ?

If you look at the Ultimate Boolean node i think it will allow you to do what you want i.e. line up multiple entities that are outputting states and you can then feed them into the Ultimate Boolean node

Have a look here

> **[node-red-contrib-boolean-logic-ultimate](https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate)**
>
> A set of Node-RED enhanced boolean logic node, flow interruption node, blinker node, invert node, filter node, with persisten values after reboot and more.

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 July 2021 02:19 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/14 "2021-07-12T02:19:20Z")

</div>

This Blog post might help you also

> **[Basic Node-Red Flows for Automating Lighting with Home Assistant - DIY Futurism](https://diyfuturism.com/index.php/2017/12/14/basic-node-red-flows-for-automating-lighting-with-home-assistant/)**
>
> How to automate smart light bulbs with Node-Red and Home Assistant based on motion sensors and presence.

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 September 2021 02:19 UTC](https://discourse.nodered.org/t/using-values-from-entities-directly-in-function-nodes/48267/15 "2021-09-10T02:19:47Z")

</div>

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