Node Red - using attributes with changing names

Hey,
I try to use an attribute of an entity, but the name of the attribute changes because it used a date/time as name.
With a ‘current state’ Node I get the wished attribute using JSONATA. But I need to switch the ‘2023-01-25 10:00:38’ into a variable (e.g. msg.lasttime) because I can get the equal time stamp somewhere else.


Is this possible or is there another way to do this?

It is most likely possible, but we probably need some more info, like what is the actually source text like and what is the complete debug message from the current state node you use?

This is the entity with the attributes:
image

This is the result of the status node (marked):
image

So you just want to have msg.payload switch to msg.lasttime?
Use a “Change” node with either the rule “Set” for copy or the rule “Move” for move.

No, that wouldn’t be difficult.
I need this information for further automations:


But the marked timestamp changes after the vacuum cleaner has cleaned.
I get this timestamp into e.g. msg.lasttime. But how can i paste that in the marked spot?

I thought it might be possible to nest it, but I have not succeeded yet and I have to leave soon.
My idea was something like this:

$entity().attributes.($entity().state).completed

It should be possible.

I solved it with a function node:
msg.uhrzeit = “2023-01-25 10:03:38”

msg.reinigung = global.get('homeassistant').homeAssistant.states["sensor.niles_cleaning_history"].attributes[msg.uhrzeit].completed;
return msg;

You appear to be trying to get a key value from an object, but where the key has to be a variable. JSONata should be able to solve this quite easily.

Assuming that msg.uhzeit going into the current-state node contains the time (key) of the cleaning cycle you are interested in

$lookup($entity().attributes, uhrzeit).completed

should return the Boolean value required. The function $lookup(object, key) takes the given entity attribute object and uses the value in uhrzeit as the lookup-key.

Alternatively, if you don’t have a variable with the key of interest, then it is possible to get the most recent key, without knowing what it is, and to check that.

$lookup($entity().attributes, $keys($entity().attributes)^(>$)[0]).completed

will get all the keys from the attribute object, sort them into descending order, take the most recent and use that to lookup the value.

If you want to visually tidy this JSONata statement, then

(
  $att:=$entity().attributes;
  $lookup($att, $keys($att)^(>$)[0]).completed
)

should work.
I have tested this as best I can, so it may not work but that is not down to any lack of what JSONata can do.

Thank you so much :blush:

That works fine:
$lookup($entity().attributes, uhrzeit).completed

The second solution gives no result:
$lookup($entity().attributes, $keys($entity().attributes)^(>$)[0]).completed

I don’t have access to your data - you only provide a picture - so it is difficult to test fully. The second solution attempts to pick out the most recent attribute, and from this the ‘completed’ field. It does work for me under my testing, but will not work if there is no attribute value, or if the most recent attribute does not have ‘completed’ field set. In this case it would require a little more code to deal with that possibility.