How to use a function call in a function?

I’m new at this this so I assume this is a very simple, possibly dumb, question? I would, however, be grateful for some assistance because I can’t seem to understand why the code below doesn’t work.

This doesn’t work:

def test_function():
	string = 'in a function'
	hass.services.call('notify', 'notify', { 'message': string })
	
test_function()	

This does:

string = 'not in a function'
hass.services.call('notify', 'notify', { 'message': string })

Why is that? how do I fix it?

I assume you’re talking about a script in python_scripts, yes?

I found the “sandbox” these are run in is very restrictive. Even some very basic things don’t work right or even at all. I think I had similar issues. E.g., some functions worked but others didn’t for no apparent reason. So this doesn’t completely surprise me.

FWIW, I don’t see any obvious reason why what you tried didn’t work.

Wait a sec… How about try this:

def test_function():
    global hass
    string = 'in a function'
    hass.services.call('notify', 'notify', { 'message': string })
	
test_function()