# Problem to display state from device

**URL:** <https://discourse.nodered.org/t/problem-to-display-state-from-device/22484>\
**Category:** General\
**Created:** [2 March 2020 08:48 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484 "2020-03-02T08:48:32Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)\
**Post date:** [4 March 2020 06:40 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/22 "2020-03-04T06:40:09Z")

</div>

Hi kuema,

Currently I had created a global variable as "global.glo" = 1. And if I want to get this global variable by using a `switch` node or `change` node. Do you have any idea on how to do it? I can't find any example for getting global variable with these nodes. 😹

Thanks & Regards.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 March 2020 06:45 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/23 "2020-03-04T06:45:36Z")

</div>

Have a look at the docs, specifically the "Using context in a flow" section.

[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

The [user guide](https://nodered.org/docs/user-guide/) and the [Cookbook](https://cookbook.nodered.org/) are always worth reading. Most of the concepts are explained there nicely. 😅

---

<div class="post-metadata">

**Author:** ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)\
**Post date:** [4 March 2020 06:55 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/24 "2020-03-04T06:55:03Z")

</div>

> [@kuema](#):
>
> [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

This link only show how to set and delete context in a flow. Doesn't teach how to read or get from flow or global. 🤣

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 March 2020 06:57 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/25 "2020-03-04T06:57:07Z")

</div>

Works the same way. You can use a change node to assign the context value to a message property.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42f7707952b61351fe5501dce38da1fbae702479.png)

---

<div class="post-metadata">

**Author:** ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)\
**Post date:** [4 March 2020 07:06 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/26 "2020-03-04T07:06:39Z")

</div>

Sorry, I don't know whether this way correct to read or not. 😢

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/feb911b8ea6fcde575891600426495dddb77b259.png)

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 March 2020 07:08 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/27 "2020-03-04T07:08:25Z")

</div>

Use "Set" to assign one property value to another (as in the screenshot I provided).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2020 08:45 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/28 "2020-03-04T08:45:51Z")

</div>

The example posted by @kuema sets msg.payload to current value of the global variable, so effectively it reads the global into msg.payload.  
Also, in a Function node you can use `global.get()` to read a global into a variable.

---

<div class="post-metadata">

**Author:** ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)\
**Post date:** [4 March 2020 09:12 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/29 "2020-03-04T09:12:14Z")

</div>

Thank you Colin for the info, I tried to get from a function node and it's work. 😍

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2020 09:54 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/30 "2020-03-04T09:54:44Z")

</div>

The link posted earlier links to [the writing functions page](https://nodered.org/docs/user-guide/writing-functions) which explains how to use context in a function node in more detail

---

<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:** [18 March 2020 10:05 UTC](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484/31 "2020-03-18T10:05:47Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/problem-to-display-state-from-device/22484.md?page=1)
