Help with some conditionals

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.

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.

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

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;

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

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

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

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:

//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;

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

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