Current state attributes

Hi, I tried to get it to work but the results from searching didn’t work and I need your help.

I want to trigger by the attribute of an entity. For my knowledge, the easy option is “current state node”.

The entity is
sensor.pixel_7_pro_active_notification_count

The attribute is
android.title_net.dinglisch.android.taskerm_5921033617: Home

I tried using JSONata in current state node with this syntax
$entity().attributes[“android.title_net.dinglisch.android.taskerm_5921033617”] = Home

and also

$entity().attributes[“android.title_net.dinglisch.android.taskerm_5921033617”] = “Home”

Non of the options worked for me in current state node.
What did I miss here?

In switch node it’s working (with the first option)

It is difficult to see what you are trying to do, and I can’t tell exactly where your problem lies.

The ‘current state’ node is triggered by a change in the state (and/or attributes), so you can’t trigger by the attribute itself (but you can test and work on the attribute using JSONata in the output properties). Do you mean the ‘trigger’ node?

Your use of [“foo.bar baz”] field reference might be incorrect - this works for JavaScript when the JSON field name contains spaces or special characters such as ‘.’

In JSONata (if that is indeed what you are using) to reference such fields requires back-ticks.

top_level.`foo.bar baz`

I would like to suggest that you post a copy of your flow, as well as a copy of the entity object. This would help see which nodes you are using, how you are using them, which UI fields, what is in them, and what your real data looks like.

Without knowing this it is very much guesswork! :slight_smile:

I want the flow to be triggered when I get a specific sms to my android.

The state of that entity is just count the number of unread messages, and the attributes contain the content of the messages.

Each application represented by one attribute and under this attribute (as a value) I see the content of the msgs.

In this case, the entity is
sensor.pixel_7_pro_active_notification_count

the attribute is
android.title_net.dinglisch.android.taskerm_5921033617

and the value of the first msg is
Home

If you have 3 messages under this attribute, it will look like this
Darling don’t forget to bring mike!|Home|Hi Biscuit, how r you?

I need to trigger a flow based on the “Home” msg from this attribute

Do you have the ‘Last Notification’ sensor enabled in the app?
Using this sensor rather than the notification count would simplify your solution.

Yes it’s enabled, but the state is always “unknown”

OK - starting to make a little more sense (it would still be MUCH easier if you posted a copy of your data object, however…)

And, if you already have it ‘working’ in a switch node, then posting your code would help. $entity() does not work in a switch node, so you must be doing something else…

Entity (sensor.pixel_7_pro_active_notification_count) state is a count of messages.

Entity (_) .attributes is an object, each object holds messages.

So, the attribute android.title_net.dinglisch.android.taskerm_5921033617 can be one or more messages. I can’t see any need to escape the usual JSON path reference, as long as taskerm_5921033617 remains static.

What I still don’t know is, is the value in this attribute an array [“Darling don’t forget to bring mike!”, “Home”, “Hi Biscuit, how r you?”] or a string

“Darling don’t forget to bring mike!|Home|Hi Biscuit, how r you?”

If (a big if) the entity value is a composite string separated by ‘|’ then

(
    $value:=$entity().android.title_net.dinglisch.android.taskerm_5921033617;
    true in $split($value, "|").$contains($,"Home")
)

as JSONata can be used to test for “Home” being contained within one (or more) of the messages in the message list string.

If you want to test for exactly that the message is “Home” and nothing more, that requires a little more code.

You can then use this JSONata, either in a trigger node as the trigger conditional test, or in an event state node, in the output message section, to generate a Boolean true/false that can then be used in a switch node to manage the flow.

This is how it look like in HA, look for “Home|Toggle Private DNS When On VPN” there are 2 lines.
taskerm_5921033617 is static if needed, the only thing that changed is the messages, Home and Toggle Private DNS When On VPN in this case but you can see the messages are separated with |

This is working for some reason, but it’s not a trigger…

Ah - that helps!

So the the attribute you want is just one long string with lots of ‘.’ in it, and you have to use the back-tick escape reference. Yes that part should now work.

Now for the node:

The ‘current state’ node will trigger when the current state (or attribute) changes.

The ‘if state’ test will normally test on the state value only.

However, if you set the ‘if state’ to use JSONata, then it will ‘trigger’ if the given JSONata expression returns ‘true’.

The expression you are using will return true if the entity attribute value is equal to “Home”, hence the node should trigger as required.

This should work for you, although the expression may have to be changed if the attribute value is “Some Message|Home” where the ‘home’ is further down the string.

$contains($entity().attribtues.`android.title_net.dinglisch.android.taskerm_2147483647`, "Home")

would be a broader test just looking to see if the attribute value contains ‘Home’ anywhere in the string.

If your attribute key ‘android.title_net.dinglisch.andorid.taskerm_id’ keeps on changing the id value, then more code will be required.

Edit
Bit tricky to write (and impossible to test) but if you have attribute names that vary, the following should work

(
    $object:=$entity().attributes;
    $k:=$keys($object)[$contains($,"android.title_net.dinglisch.android")];
    true in $k.($contains($lookup($object, $),"Home"));
)

This is another way to deal with object fields with strange characters in them.
The JSONata code gets the attribute as an object, then gets an array of the object keys, selecting from this only the keys with the front part of the name and ignoring the taskerm_ bit that changes.
Then goes through the array of keys, looking up the values, looking to see if the value contains ‘Home’ - thus giving an array of true/false for each value.
Finally the code looks to see if true is in the array (if at least one of the attribute fields of interest contains ‘Home’), and the code block then returns true or false and can be used in the JSONata test.

Thank you for your help.

It’s working with “current node” only, but it’s not triggered, I need something to inject it to “wake up” the “current node”

Any option to make it work with the trigger node?

Go to settings, sensor, click on last notifications. Then sensor settings, this will bring up a allow list, look for the app and check the box. You nee to set the sensor up, even though it’s enabled it’s not listening for anything. Don’t use the disable allow list option it will let everything through and eat away at your battery.

E. Once it is setup, Home will be in the state position.

Usually this sort of task is quite simple, but with JSON object keys with ‘.’ in the name it becomes more of a challenge.

To get any flow going requires a trigger. There are three main ways of doing this (there may be others too…).

  • Use an inject node to start a flow every 2-3 minutes. Use a current state node to pick up the entity, and use JSONata in the conditional test. We (kind of) know that this works. The JSONata can get at the attributes and do the test.

  • Use a trigger node. These nodes will fire every time the defined entity/attributes changes, and it is possible to add conditions into the node directly. It is possible to add a condition for ‘this entity’ and for ‘property’ is… test. In theory we can select the attribute property, but there is no facility for using JSONata as a test, only for using JSONata to create the test-against value.

  • Use an event: state node to trigger and fire every time the state/attributes change. This node has an ‘If state’ condition test, and the ability to set the test to JSONata. This will accept the code we have been trying out. If the JSONata expression returns true, then a message will be returned from the upper exit every time the state / attribute changes. If false, the message will return from the lower exit.

Try using the event: state node. If you can get this to fire every time there is a change, and if you can get the JSONata code to correctly test the attribute, then it should be possible to put it all together!

I think the event state is working, I will try for the next few days.
Thank you :slight_smile:

Now it’s working, I will try it, I just not sure how it will work if I will get 2 messages or more at the same time… I don’t know how it will enter HA and how node red will get it (which msg it will show as last from the “q” of messages)

If you look at the attributes it is no longer an array, It’s what you’d expect from a typical sensor. Set the last notification to an event state and leave the state empty. Then with a switch node split the messages by their app.

image

image

It’s working perfect, both methods are working I will see which is best suited for the bike alarm