Using get entities filtered on item in attributes.tags[]

I’m trying to find one or more entities (which are schedules from the HACS scheduler component) based on their tag.
The schedules are switch entities with an attributes.tags property.
The tags is a list or array.
How can I use JSONATA in the get-entities node to filter on a specific tag

The expression I use is
$[attributes.tags[$contains(“Comfort temperatuur”)]]

Which works on my test example:

[
    {
        "entity_id": "switch.schedule_zondag",
        "state": "on",
        "attributes": {
            "weekdays": [
                "sun"
            ],
            "timeslots": [
                "00:00:00 - 06:30:00",
                "06:30:00 - 09:10:00",
                "09:10:00 - 15:50:00",
                "15:50:00 - 22:30:00",
                "22:30:00 - 00:00:00"
            ],
            "entities": [
                "input_number.cv_comforttemp"
            ],
            "actions": [
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 16
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 20
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 19
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 19.5
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 16
                    }
                }
            ],
            "current_slot": 2,
            "next_slot": 3,
            "next_trigger": "2024-01-14T15:50:00+01:00",
            "tags": [
                "Comfort temperatuur",
                "Thermostaat"
            ],
            "icon": "mdi:calendar-clock",
            "friendly_name": "Zondag "
        },
        "last_changed": "2024-01-14T08:11:00.043Z",
        "last_updated": "2024-01-14T08:11:00.043Z",
        "context": {
            "id": "01HM3GC9JBJ8F09HAKC1HGZ3JW",
            "parent_id": null,
            "user_id": null
        },
        "timeSinceChangedMs": 14045834
    },
    {
        "entity_id": "switch.schedule_bls",
        "state": "on",
        "attributes": {
            "weekdays": [
                "sun"
            ],
            "timeslots": [
                "00:00:00 - 06:30:00",
                "06:30:00 - 09:10:00",
                "09:10:00 - 15:50:00",
                "15:50:00 - 22:30:00",
                "22:30:00 - 00:00:00"
            ],
            "entities": [
                "input_number.cv_comforttemp"
            ],
            "actions": [
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 16
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 20
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 19
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 19.5
                    }
                },
                {
                    "service": "input_number.set_value",
                    "data": {
                        "value": 16
                    }
                }
            ],
            "current_slot": 2,
            "next_slot": 3,
            "next_trigger": "2024-01-14T15:50:00+01:00",
            "tags": [
                "Thermostaat"
            ],
            "icon": "mdi:calendar-clock",
            "friendly_name": "Zondag "
        },
        "last_changed": "2024-01-14T08:11:00.043Z",
        "last_updated": "2024-01-14T08:11:00.043Z",
        "context": {
            "id": "01HM3GC9JBJ8F09HAKC1HGZ3JW",
            "parent_id": null,
            "user_id": null
        },
        "timeSinceChangedMs": 14045834
    }
]

Set your get entities node - property selector option to J: for JSONata.

Then all you need is an expression that is evaluated for each entity, and must return true for any that you wish to select.

This expression, for each entity, using $entity() to get the entity details, then looks for attributes.array. When found this will be [something], and hence the conditional test returns a Boolean true only when the entity has an attribute field called array.

$entity().attributes.array!=[]

This gives me all my entities with an array attribute.
You probably just need

$entity().attributes.tags[$contains("Comfort temperaturr")] != []

The $entity().attributes.tags[$contains(“Comfort temperaturr”)] should return an array of your entities that contain tag attributes with the given name, so testing against an empty array should return a Boolean result.

1 Like

Yes… thank you…
So where my train of thinking derailed was that the JSONATA expression should return true or false.

So the correct answer should be (you made a typo… but I understood your solution)

$entity().attributes.tags[$contains(“Comfort temperatuur”)] !=

And explained by chatGPT (sry… but it has it uses…)

Certainly! The expression $entity().attributes.tags[$contains("Comfort temperatuur")] != [] is checking if the array attributes.tags within the result of $entity() (which seems to represent an entity with attributes) contains the string “Comfort temperatuur”.

Let me break it down:

  • $entity(): This seems to represent a function or operation that returns an entity object.

  • .attributes.tags: It accesses the “tags” property within the “attributes” property of the entity.

  • [$contains("Comfort temperatuur")]: This checks if the array (in this case, the “tags” array) contains the string “Comfort temperatuur” using the $contains function.

  • != []: This part checks if the result of the above filter operation is not an empty array.

So, the entire expression is checking whether the “tags” array of the entity’s attributes contains “Comfort temperatuur” and the result is not an empty array. If the condition is true, it means that “Comfort temperatuur” is present in the “tags” array of the entity’s attributes and the array is not empty.

The documentation does explain the difference when using JSONata as the selector, although this fine detail can be easily missed

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/jsonata.html

$entity() is one of the special WebSocket node functions - they only work inside these nodes. $entity() specifically returns an object with details of the entity of interest. In most nodes this is the one that has just triggered the node or been selected by the node. Here, the JSONata expression is applied to each entity in turn, hence $entity().state or $entity().attributes will return, for each entity in HA, details that can be used.

There is a JSONata $boolean() function that would also do the trick

https://docs.jsonata.org/boolean-functions#boolean

so the following should work too

$boolean($entity().attributes.tags[$contains("Comfort temperatuur")])