# Changing state of input\_number in Function Node - is it possible?

**URL:** https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724
**Category:** General
**Created:** [19 August 2020 12:16 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724 "2020-08-19T12:16:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Boja](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@Boja](https://discourse.nodered.org/u/Boja)
#### Post date: [19 August 2020 12:16 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/1 "2020-08-19T12:16:42Z")

</div>

One can read states with global.get ('homeassistant').homeAssistant.states['input\_number.xyz'].state;  
but it doesn't seem to have any effect if I use global.set.

Is it at all possible to change these values from a function Node ?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 August 2020 12:23 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/2 "2020-08-19T12:23:26Z")

</div>

are you doing global.set correctly?

`global.set("key", value)`

so in your case...

```auto
var ha = global.get('homeassistant');
var oldValue = ha.homeAssistant.states['input_number.xyz'].state;
ha.homeAssistant.states['input_number.xyz'].state = 77; //change something
ha.homeAssistant.states.something_else = "a new string value"; //change something_else
global.set('homeassistant', ha);//store it

```

NOTE:

- I did not check for existence of properties (so errors can occur)
- I know nothing about HA so setting that state value might have side affects.
- As `homeassistant` is an object, calls to `global.set('homeassistant', ha)` are not strictly necessary - but are definitely good practice (as `homeassistant` is an object it will be modified by reference, you actually modify the global instance - but if you persist context to file, these wont happen unless you call `global.set()`) In a nutshell, always call global.set after modifying the value.
- I am assuming `homeAssistant.homeAssistant.states['input_number.xyz'].state` is a valid path (as per your post) - though i suspect there are one too many `homeAssistant`s - but you would need to show us the actual content of the global context object to be sure.

---

<div class="post-metadata">

### Author: ![Boja](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@Boja](https://discourse.nodered.org/u/Boja)
#### Post date: [19 August 2020 12:44 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/3 "2020-08-19T12:44:35Z")

</div>

> [@Boja](#):
>
> are you doing global.set correctly?

Well I have tried using it like this:  
global.set ('homeassistant').homeAssistant.states['input\_number.xyz'].state, 1234);  
and it looked like nothing happened to the value. The old value is still the same if read afterwards.

And yes, ('homeassistant').homeAssistant.states['input\_number.xyz'].state is a valid path and the state can be read without any problems.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 August 2020 12:51 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/4 "2020-08-19T12:51:47Z")

</div>

> [@Boja](#):
>
> Well I have tried using it like this:  
> global.set ('homeassistant').homeAssistant.states['input\_number.xyz'].state, 1234);

That definitely wont work.

The equivelant is this...  
`global.get('homeassistant').homeAssistant.states['input_number.xyz'].state = 1234;`

This works because `get` gets the object & as it as a by reference affair, the sub properties are accessible & settable.

---

<div class="post-metadata">

### Author: ![Boja](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@Boja](https://discourse.nodered.org/u/Boja)
#### Post date: [19 August 2020 13:02 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/5 "2020-08-19T13:02:11Z")

</div>

It works, great !  
I think I have to dig into this a little deeper, I would probably never have solved this problem without you help.  
I saw you comment on side affects and have to make sure I really understand what effects is has.

Thanks Steve-Mcl, you have really made my day.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 August 2020 13:08 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/6 "2020-08-19T13:08:26Z")

</div>

> [@Boja](#):
>
> Thanks Steve-Mcl, you have really made my day.

You're welcome.

Please note, the "correct node-red way" to do this is...

```auto
var ha = global.get('homeassistant');
ha.homeAssistant.states['input_number.xyz'].state = 1234;
global.set('homeassistant', ha);

```

---

<div class="post-metadata">

### Author: ![Boja](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@Boja](https://discourse.nodered.org/u/Boja)
#### Post date: [19 August 2020 13:18 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/7 "2020-08-19T13:18:13Z")

</div>

Noted.

Doing it this way is also much more inline with what I intend to do (setting many states).

---

<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: [2 September 2020 13:18 UTC](https://discourse.nodered.org/t/changing-state-of-input-number-in-function-node-is-it-possible/31724/8 "2020-09-02T13:18:14Z")

</div>

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