Scripting issue - Unexpected error for call_service

I have been trying to configure an automation to switch on a light with different settings dependent on the time of day, I use a PIR to detect motion and this should then trigger a script accordingly.

The Automation:

- id: 'PIR01On'
  alias: Tuya Test
  description: ''
  trigger:
  - entity_id: switch.pir01_tasmota 
    from: unavailable
    platform: state
    to: 'on'
  condition: []
  action:
    service_template: >
      '{% if now().hour > 20 %} script.bathdim
      {% elif now().hour < 5 %} script.bathpurple
      {% else %} script.bathbrightwhite {% endif %}'
'''

The Script(s):
'''
'set_bathlight_purple':
  alias: bathpurple
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.bath_tasmota
      brightness: 64
      white_value: 200
      kelvin: 3000

'set_bathlight_bright_white':
  alias: bathbrightwhite
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.bath_tasmota
      brightness: 200
      white_value: 200
      kelvin: 6000

'set_bathlight_dim_white':
  alias: bathdim
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.bath_tasmota
      brightness: 64
      white_value: 200
      kelvin: 3000

bath purple and bathdim are the same simply because I have been playing with the script to try and find out where the error comes from. It was changing the colour of the light bulb.

The scripts work on their own however the automation reports an error. The error log reports:

Tuya Test: Error executing script. Unexpected error for call_service at pos 1: Template rendered invalid service: ' script.bathbrightwhite '
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 115, in async_prepare_call_from_config
    domain_service = cv.service(domain_service)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 411, in service
    raise vol.Invalid(f"Service {value} does not match format <domain>.<name>")
voluptuous.error.Invalid: Service ' script.bathbrightwhite ' does not match format <domain>.<name>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 153, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 656, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 252, in _prep_call_service_step
    return async_prepare_call_from_config(self._hass, self._action, self._variables)
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 123, in async_prepare_call_from_config
    ) from ex
homeassistant.exceptions.HomeAssistantError: Template rendered invalid service: ' script.bathbrightwhite '

I am really stuck on this thing. Any ideas guys?

No quotes round multi-line templates.

  action:
    service_template: >
      {% if now().hour > 20 %} script.bathdim
      {% elif now().hour < 5 %} script.bathpurple
      {% else %} script.bathbrightwhite {% endif %}

Perfect, thanks very much.

I do find sometimes that the documentation is confusing as to whether you add quotes or not etc. I guess it will all start to fall into place soon, I hope.

Looks like you have a good grasp of the syntax. Like anything it just takes a bit of time and repetition for it to sink in.

I find this indentation aids readability, but it’s up to your personal preference:

action:
  service_template: >
    {% if now().hour > 20 %}
      script.bathdim
    {% elif now().hour < 5 %}
      script.bathpurple
    {% else %}
      script.bathbrightwhite
    {% endif %}

Further to what Tom said, you do know that this will only trigger from (as it says) ‘unavailable’ to ‘on’ ? Why not just to: ‘on’ ???

1 Like

Hi Mutt,

Sorry for the long delay. I missed your reply. I ended up ditching the unit and I am now gonna try a zigbee bridge because it has more functionality anyway.

I think the “unavailable” was put in by the Home Assistant automations generator.