Service Template

hello,
First of all I would like to ask if is possible to do service template from UI. If the answer is yes, then please share some examples.

if not, would you please tell me how to achieve it as I keep getting errors?

service_template: > 
	{% if is_state('media_player.sofita_tv', 'on') %} 
	script.turn_on 
	{% endif %}
entity_id: script.1576285368449

please provide an example for scripts.yaml
what I want to achieve is when tv is on, run a script.

If you want a script to run when the TV/media player turns on, make an automation with the trigger being the media player being turned on and the action being the script. Did I understand you correctly?

1 Like

Look at the service_template you’ve created and ask yourself what happens when the media_player is not on? What result does the template produce?

The answer is it produces nothing. That means it assigns nothing to service_template and will cause an error because Home Assistant needs the name of a valid service.

In your example, there is probably no other service that you want to run when the media_player is off. Therefor you need to move this test (Is the media_player on?) out of the service_template and into either the condition or, if feasible, into the trigger (as suggested by Tediore).

Ideally, you want the automation to determine if it should, or should not, run the service before it gets to the action section.

In addition, you can call a script like this:

  service: script.1576285368449