# Help with some conditionals

**URL:** <https://discourse.nodered.org/t/help-with-some-conditionals/50460>\
**Category:** General\
**Created:** [31 August 2021 20:04 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460 "2021-08-31T20:04:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![muddro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muddro1/32/38671_2.png) [@muddro1](https://discourse.nodered.org/u/muddro1)\
**Post date:** [31 August 2021 20:04 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/1 "2021-08-31T20:04:00Z")

</div>

I am trying to set up a flow that sets an input\_select in home assistant in the most efficient manner. Basically, any time someone's status changes I want it to evaluate the group status. I cannot use the group function because found something more complex than simple home or not\_home.

Each person has there own input\_select that has the options of Home, Just Arrived, Just Left, Away, and Extended Away. I created another input\_select for the group that has the same options. How I want that input\_select to behave is that it has the status of the group: ie if P1 is Home and P2 is Away, it will select Home, if P1 and P2 are Away, it will show Away. So the order of precedence is as its listed.

My first node is tied to the entity ID of `^input_select\..*$` so that way anytime one of the person's individual input\_select's changes, it will kick off the flow. But I am at a loss on how to continue this. Any help would be appreciated.

---

<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:** [31 August 2021 20:24 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/2 "2021-08-31T20:24:49Z")

</div>

I understand most of the words in your question, but I have no idea what it is actually about, hence I suspect that it may be more of a Home Assistant question rather than node red. If that is the case then you may be better asking in a HA forum.

---

<div class="post-metadata">

**Author:** ![muddro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muddro1/32/38671_2.png) [@muddro1](https://discourse.nodered.org/u/muddro1)\
**Post date:** [31 August 2021 20:58 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/3 "2021-08-31T20:58:51Z")

</div>

All good. I think it was more a javascript questions in the end. I think I may have pieced together a solution:

```auto
var states = global.get('homeassistant.homeAssistant.states');
var person1= states["input_select.status_person1"].state;
var person2= states["input_select.status_person2"].state;

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}

var dict = {
    "Home": 1,
    "Just Arrived": 2,
    "Just Left": 3,
    "Away": 4,
    "Extended Away": 5
}

person1 = dict[person1];
person2 = dict[person2];

msg.payload = Math.min(person2,person1);
msg.payload = getKeyByValue(dict,msg.payload);
return msg;

```

---

<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:** [1 September 2021 08:22 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/4 "2021-09-01T08:22:04Z")

</div>

I am not sure I fully understand what that code is doing, but I think something like this might be more efficient

```auto
let dict = [
    "Home",
    "Just Arrived",
    "Just Left",
    "Away",
    "Extended Away"
]

```

then  
`msg.payload = dict[msg.payload-1]`

---

<div class="post-metadata">

**Author:** ![muddro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muddro1/32/38671_2.png) [@muddro1](https://discourse.nodered.org/u/muddro1)\
**Post date:** [1 September 2021 15:18 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/5 "2021-09-01T15:18:41Z")

</div>

Don't think that would work as I need to compare the "value" of two different persons status, returning the one with the lower value as it takes priority. But I may be wrong. The purpose is to set a status for the home overall based on locations of individuals within the home. Other automations depend if we are both away, for instance, instead of one home and one away. I did condense the code though:

```auto
//Assigns value to a location status
var dict = {
    "Home": 1,
    "Just Arrived": 2,
    "Just Left": 3,
    "Away": 4,
    "Extended Away": 5
}

//function to be able to get the key based on the value
function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}

//grab Home Assistant States and assign a value to a person based on their location status
var states = global.get('homeassistant.homeAssistant.states');
var P1 = dict[states["input_select.status_P1"].state];
var P2 = dict[states["input_select.status_P2"].state];

//Find who's location status will be returned based on the value of their status
msg.payload = Math.min(P1, P2);
msg.payload = getKeyByValue(dict,msg.payload);
return msg;

```

---

<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:** [1 September 2021 16:08 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/6 "2021-09-01T16:08:12Z")

</div>

OK. As I said, I don't fully understand what your input data are and what result you want out.

---

<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:** [31 October 2021 16:08 UTC](https://discourse.nodered.org/t/help-with-some-conditionals/50460/7 "2021-10-31T16:08:28Z")

</div>

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