#Check if an entity was a certain state i...

1 messages ยท Page 1 of 1 (latest)

peak current
#

Could you describe the issue you are trying to solve? You can make a function node with multiple outputs and basically write your own switch node without much code.

topaz mirage
#

I'm trying to check if several lights have been on in the last X minutes. If any have been on then it needs to do something.

Because I'm checking several lights I'm using the include function in the Get History node with a regex pattern to find all the lights, so it's slightly different to the docs but I would expect the logic to be the same.

#

The light state can be found in payload.state[1] but the switch node isn't passing it through when I ask it to evaluate payload.state.

peak current
#

I do not think that is something the switch node can do. If you are only looking for on state, then something similar to this would work:

const states = msg.payload.state

for (let state of states) {
    if (state == 'on') {
        msg.payload = state
        return msg;
    }
}

Without knowing the exact payload it is hard to demo it correctly for you. Editing the above in a function node will give you the result you want.

topaz mirage
#

Thank you ๐Ÿ™‚ will try that

#

"TypeError: states is not iterable" error with that function node. I'll show you some examples of the payload to make it clearer

#

So for every light the history node is producing a payload that looks like this

#

The change node should be checking them all for the on state, but because it could be in any of the arrays it doesn't seem to be working.

All Get History payloads are nestled in arrays, so I'm not seeing why this doesn't work but the example in the docs does

#

Ahh I think I see why. if you use the is function of get history instead of the include, it outputs each bit of history as a sep. payload not in arrays, so you can use a change node

#

I'm thinking best option would be to use a get entities node to pull a list of lights and feed it into the get history node (that way I can still use is). Can you use a payload value for the entity id in the get history node?

peak current
#

According to the documentation, it does not seem that the get_history node can take an array. You would need to feed them in individually from the get_entities node.

#

And it is the payload that was iterable. So changing to the below should work better.

const payload = msg.payload

for (let value of payload) {
    if (value.state == 'on') {
        msg.payload = value.state
        return msg;
    }
}
topaz mirage
#

Thank you will try that. just to confirm, I can't just put something like {{payload.entity_id}} as the entity id in the get history node?

#

As in a dynamic value

peak current
#

You can send it as a dynamic value as you have above, but it has to be a single value, not an array.

topaz mirage
#

Yeah that's fine, what I was thinking of is to use a get entities node to spit out all the entity IDs as separate payloads and feed them into Get History.

#

Just trying to avoid code because I can't code, so trying to keep it maintainable lol

peak current
#

I understand, completely. I was in the same boat not long ago. If you need any more help on this, let me know and I will try to assist.

topaz mirage
#

Thank you, I really appreciate it.

topaz mirage
#

Any idea what I'm missing? it's just pulling every single entity

#

(zone.home was set to string format, ignore the format of it in the screenshot)

peak current
#

So a few things. You leave the history node entity id blank and pass it as msg.payload.entity_id. So in your change node, change msg.test โ†’ msg.payload.entity_id. Though when using a inject node, you will have to delete the current msg.payload unless it is already an object. Like so:

topaz mirage
#

Ahhh I see, it wants it inputted in a specific format! Little confusing how some nodes handle this in different ways

peak current
#

It definitely can be. It is usually in the documentation how these nodes expect the input override options.