Help with template in script (should work?)

hi! I would like to use a template inside a script. The script turns on (and off) a hue lamp in the hallway. I’ve been reading this forum and tried to debug it myself with no luck. #1 works; the others don’t :slight_smile:

  alias: "Turn on hal_beneden and set timer"
  sequence:
    # Cancel ev. old timers
    - service: script.turn_off
      data:
         entity_id: script.timer_off1
    - service: light.turn_on
      data:
        entity_id: light.hal
#1        brightness: '200'
        brightness: '{% if now().hour > 20 %}150{% elif now().hour < 8 %}150{% elif now().hour < 6 %}50{% else %}255{% e$
#3        brightness: >-
#          {% if now().hour > 20 %}
#             150
#          {% elif now().hour < 8 %}
#             150
#          {% elif now().hour < 6 %}
#             50
#          {% else %}
#             255
#          {% endif %}
    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.timer_off1

Error with #1:

    Traceback (most recent call last):
      File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/core.py", line 1130, in _safe_execute
        await self._execute_service(handler, service_call)
    [..]
      File "/usr/local/lib/python3.6/asyncio/tasks.py", line 304, in wait
        raise ValueError('Set of coroutines/Futures is empty.')
    ValueError: Set of coroutines/Futures is empty. 

Error with #3:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/core.py", line 1130, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/core.py", line 1143, in _execute_service
    await handler.func(service_call)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/core.py", line 1101, in async_call
    processed_data = handler.schema(service_data)
  File "/srv/homeassistant/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/srv/homeassistant/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/srv/homeassistant/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected int for dictionary value @ data['brightness']

Suggestions?

I think you need to use data_template instead of data. So something like:

    - service: light.turn_on
      entity_id: light.hal
      data_template:
        brightness: >-
          {% if now().hour > 20 %}
             150
          {% elif now().hour < 8 %}
             150
          {% elif now().hour < 6 %}
             50
          {% else %}
             255
          {% endif %}

Or perhaps

    - service: light.turn_on
      data:
        entity_id: light.hal
      data_template:
        brightness: >-
          {% if now().hour > 20 %}
             150
          {% elif now().hour < 8 %}
             150
          {% elif now().hour < 6 %}
             50
          {% else %}
             255
          {% endif %}

I’m not sure which, but I think one of the two ways is correct.

Thanks for the tip; this works for me.

timed_switch1:
  alias: "Turn on hal_beneden and set timer"
  sequence:
    # Cancel ev. old timers
    - service: script.turn_off
      data:
         entity_id: script.timer_off1
    - service: light.turn_on
      data:
        entity_id: light.hal
#        brightness: '200'
      data_template:
        brightness: >-
          {% if now().hour > 20 %}
             150
          {% elif now().hour < 8 %}
             150
          {% elif now().hour > 23 %}
             50
          {% elif now().hour < 6 %}
             50
          {% else %}
             255
          {% endif %}
    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.timer_off1