How to check multiple entity conditions?

I am trying to move one automation into NodeRED for fun and learning purposes, but I cannot find the proper node, if it does exist, to check on multiple entity conditions. For example:

    - if:
        - condition: state
          entity_id: fan.office_fan
          state: "off"
        - condition: state
          entity_id: climate.home
          state: 'off'
        - condition: numeric_state
          entity_id: weather.home
          attribute: temperature
          above: 75
      then:
        - service: fan.turn_on
          data:
            percentage: 100
          target:
            entity_id: fan.office_fan

I have tried as follows and having the fan and AC off it does not find any entity probably because of the mix I am trying to do there:


what would you suggest me?

Three current state nodes in series or a single trigger-state node.

1 Like

Or a function node checking globals, but then it is a bit back to coding as in HA.

Here is an example on a function node that extracts a few sun values from the Global Context Data.

[{"id":"fcb8501ba4e0c02b","type":"function","z":"be03f9025a1f137a","name":"function 1","func":"msg.sun = global.get('homeassistant').homeAssistant.states[\"sun.sun\"];\nmsg.sun_state = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].state;\nmsg.sun_attr_next_dawn = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].attributes.next_dawn;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":375,"y":75,"wires":[["36f148efe227cbac"]]}]

You can find other values by opening up the Context Data in the upper right icon menu and then refresh the Global section.

@Kermit

by three current states nodes in series you mean the following?

The only problem with current-state is that it does not allow checking on other attributes and just state.

I have tried a single trigger-state node but it will not work for me since it keeps triggering itself and therefore the actions executed afterward will be executed all the time, am I wrong? For example, in the below trigger-state node you can see that it does not wait on me to inject a timestamp and it always debugs the information. I need something that will happen upon an event triggered on my side

I am open to ideas, I could be wrong or have something miss configured! I am stuck :expressionless:

The function node I posted is probably the best solution.
It extracts all the information you need, whether it be states or attributes.
in the current setup it will put the data into different msg.objects, but you can also do the checks in the function node itself if you wish for that.
Function nodes are javascript, so for code examples search for that in google, like “if then javascript”

And btw, always set your debug nodes to “complete messages” in the output field, if you do not do so already.

1 Like

Use can check against an attributes using JSONata.

1 Like

@Kermit For some reason, I can’t get this working. See this pic:

On the right side you will see the entity reporting the temperature is above 72 but for some reason the current_state is reporting it as false.

This is the JSON for this flow:

[
    {
        "id": "70d573ad10d4d31a",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "9c53dfbbdcaa5b40",
        "type": "inject",
        "z": "70d573ad10d4d31a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 380,
        "wires": [
            [
                "6347ad1c5c0474d1"
            ]
        ]
    },
    {
        "id": "6347ad1c5c0474d1",
        "type": "api-current-state",
        "z": "70d573ad10d4d31a",
        "name": "",
        "server": "e593dd3.052432",
        "version": 3,
        "outputs": 2,
        "halt_if": "$entity().attributes.attributes.temperature >= 72",
        "halt_if_type": "jsonata",
        "halt_if_compare": "jsonata",
        "entity_id": "weather.home",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 240,
        "y": 260,
        "wires": [
            [
                "1f96d51fcb670fd3"
            ],
            [
                "1653a7bafc993167"
            ]
        ]
    },
    {
        "id": "1f96d51fcb670fd3",
        "type": "debug",
        "z": "70d573ad10d4d31a",
        "name": "debug 14",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 400,
        "y": 160,
        "wires": []
    },
    {
        "id": "1653a7bafc993167",
        "type": "debug",
        "z": "70d573ad10d4d31a",
        "name": "debug 15",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 400,
        "y": 380,
        "wires": []
    },
    {
        "id": "e593dd3.052432",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

I have changed the debug node to debug the whole object and see … the temperature is at 78 why is the current_state still saying is false? I don’t get it. What I am missing and not understanding here?

You better post your flow too, because I see no current_state value in the things you posted from Node Red.

I just posted the flow here at least the part where I was playing with the entity

just found where my mistake was … in my flow I had:

"halt_if": "$entity().attributes.attributes.temperature >= 72",

it should be:

"halt_if": "$entity().attributes.temperature >= 72",

now is working as expected