Passing values between steps in automations?

I want to have one step in an automation generate a time-limited unique value (Specifically S3 presigned URL) and then pass this to the next step in the automation (to do something with that freshly-generated URL).

Is this possible? Would I need to create a service that takes the relevant input and generate the URL… but then what? How can that service store it and pass it on to the next step?

Do I need to invoke an external Python script to handle this?

I’ve seen some posts say services don’t return responses. I’ve seen you can’t import external libraries when running an embedded Python script. Just looking for ideas on how to achieve the above. Thanks

https://www.home-assistant.io/docs/scripts/#variables

Ok those show how to use variables in subsequent blocks in the same script.

Does anybody know how to actually set the value of the variable with a service? In python I could say something like :slight_smile:

def function (some parameter):
Val = Do.something()
Return Val

Variable = function(x)

But I’ve read services don’t return values. Can I call a services from an automation with any string as a variable parameter and then will that string accept the value of whatever I return? Thanks

I don’t think there is such a service. You could use an input_text:

action:
  - service: input_text.set_value
    target:
      entity_id: input_texy.my_url_storage
    data:
      value: "https://www.something.com"

You can template the value if needed.

I have an automation that takes a camera image and uploads it to an external destination working.

I’d like to name the file with a unique guid-like file name.

My automation has multiple action blocks – one calling the camera service to take the snapshot and another calling the uploader service.

Is there a way to generate a random value to use as the file name but have that same value used across multiple action blocks in the same automation invocation?

Create a variable and set its value to the current timestamp.

...
action:
  - variables:
      file: "/somewhere/whatever/image_{{ now().timestamp() | int(0) }}.jpg"
  - service: notify.persistent_notification
    data:
      title: "Here is the value of the variable named file"
      message: "{{ file }}"

For example:

/somewhere/whatever/image_1660860780.jpg

The output of {{ now().timestamp() | int(0) }} isn’t random (it’s the number of seconds since the Unix Epoch) but it is unique.

In the python code I ended up sending an event back to Home Assistant with the URL value. I then have another automation that triggers from that event for further processing.