Use local variables in scripts

Is there a way to set a variable in a script and use this value later on?

I have a doorbell with a button that executes an automation in hass. Currently, this automation executes the following shell_command script:


import time
import urllib.request
from pushbullet import Pushbullet

PUSHBULLET_API = 'o.****************************'
WEBCAM_URL = '*****************************'
IMAGE_PATH = '/config/www/'
IMAGE_URL = 'https://dnsnameofmyhass:8123/local/'

# Construct filename
filename = 'doorbell-%s.jpg' % time.strftime('%Y%m%d-%H%M%S')

# Download image from camera
urllib.request.urlretrieve(WEBCAM_URL, IMAGE_PATH + filename)

# Push image to android
pb = Pushbullet(PUSHBULLET_API)
channel = pb.channels[0]
push = channel.push_file(file_url=IMAGE_URL + filename, file_name=filename, file_type="image/jpeg", body="Deurbel")

I basically generate a unique filename (since I want to keep a history of all images), store a picture from the camera to it, and send the url of this picture to pushbullet.

Now, I want to use the new camera.snapshot service, and use the pushbullet platform itself to send the message, but I don’t know to generate the unique filename.

I can store them to snapshot.jpg and always use this, but then all previous messages will be the same image. This makes pushbullet show the same image when the doorbell rang twice.

Any idea’s?

for your filename, how about using the time the automation was triggered: states.automation.automation_name.attributes.last_triggered.

Then you can save this text in an input_text entity if you want to reuse later? Or save in a text file?