Service Node - Append text to filename

I have a short flow to trigger a camera entity snapshot service and would like to append the date/time to the filename so it does not get overwritten everytime.
I used the Developer Tools to create the string and I see it working:

service: camera.snapshot
target:
  entity_id: camera.back_patio
data:
  filename: /share/Snapshot_{{ now().strftime("%Y%m%d%H%M%S") }}.jpg

I am now trying to move it to my flow in NodeRed, but for some reason, it is not working. I understand that the expression would need to change for JSON, but I have not been able to get it to work yet.
I even tried using {{payload}} instead of now()… but I get a blank string, so my filename is Snapshot_.jpg
This is my flow:
image
So the output of the inject node is msg.payload = timestamp.
But the service node does not seem to retrieve it and append it to the filename.
I also tried using $now(), etc…
This is my current JSONata expression:
"filename":"/share/{{ entity_id.name }}/Snapshot_{{ payload }}.jpg"


I am not familiar with JSON, so any help on the syntax or even on the approach would help. I just need to have a unique filename for each picture, and in chronological order, so cannot be random. I will be using the images saved to create a timelapse GIF (which I still have not figured out, but part of the learning journey).

Thanks,
H-

You have the data field as jsonata change the J to brackets for json.

1 Like

You have the data field as jsonata change the J to brackets for json.

thanks @Mikefila for the quick response.

I got the file working with the timestamp appended at the end. Now I lost access to the {{ entity_id.name }}.

So the test I did:
If I use jsonata, I get access to the entity_id.name and not the payload.
If I used js, I can use payload but not the entity_id.name

If I use json and check the box for Use Alternative Template… shown in the picture, I get access to entity_id.name, but not payload.
image

Looks like the two do not work together. Any thoughts?

Thanks,
H-

You have to use the message position, is the entity_id coming in msg.entity_id.name. You could also try triple brackets if it’s already coming in as msg.entity_id.name

It’s not coming in on a message. It is getting picked up from the box above it. So all contained within the node itself.

I’m not sure how to do that. If the entity_id is not dynamic and doesn’t change, why not use the actual name?

You can set up a call service to have a dynamic entity field. Meaning if I have multiple cameras and I use their motion events to call a snapshot. The motion event will have the camera entity and in the entity field of the call service node you can use {{msg.path.to.camera}} and then use it again in the data field.

E. If you’re goal is to get the timestamp, you will likely have to manipulate the message that you use as a trigger. Meaning the timestamp will likely be an attribute and have to be moved to the msg.payload position.

btw have you tried {{ entity_id }} or {{{ entity_id }}}

@Mikefila, your comment about the entity_id not being dynamic triggered me to go down a path which led to a solution. If the entity_id was not dynamic, then let me make it do and then use it…here is the solution. Thanks!

Tried the {{{ }}} and no luck. So I went off and created a workaround. Not sure if it is a workaround r the best way to do it, but it works.

OK, just to make sure I have it here in case someone else is trying for the same thing, let me show the details.

Here is what the flow looks like:

I used the Get entity Node to get the name and info into NodeRed, outside of the service node.
image

So now I have the entity object that I need.
In the function node, I retrieved the friendly name:

var newMsg = { "payload":   {
                            "entityId": msg.payload.attributes.friendly_name,
                            "time": Date().toString()
                            }
};
return newMsg;

I realized I wanted a timestamp format for the Date, so I added a Change node to replace time with timestamp.
Then in the service node, I created this filename path:

{
    "filename": "/share/{{ payload.entityId}}/Snapshot_{{ payload.entityId}}{{ payload.time }}.jpg"
}

The result looks like this:
image

So now I can trigger a picture every 5minutes with the inject node, or when a motion is detected.

Now on to the creating a GIF from all the files I save

H-

Just a little tip, you can also do this with a change node. You can also do simple math like payload*3600.

1 Like

Done…Worked. Thanks.
I was missing the Move in the Rule and was getting a Circular reference.
Learned something new!

1 Like

Can you share the node? thanks.