Script template unexplained failure

Hi there people.

I am struggling with a script that is not executing correctly, despite looking correct to me.

So background - the values are being injected by Node-RED, I originally designed this process to run as multiple steps in a flow, but it made more sense to make one script and then just call that.

Anyway, the HA script YAML:

script:
  hue_activate_scene_and_brightness:
    sequence:
      - service: hue.hue_activate_scene
        data_template:
          group_name: "{{ group_name }}"
          scene_name: "{{ scene_name }}" 
      - delay:
          seconds: 2
      - service: system_log.write
        data_template:
          message: >
            Setting brightness for 
            {{ entity_id }} to 
            {{ state_attr("{{ entity_id }}", 'brightness') | int}}
      - service: light.turn_on    
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: >
            {{ state_attr("{{ entity_id }}", 'brightness') | int}}

(The system_log.write is only in there for debug purposes.)

The reason for this is that I am using Ikea bulbs which do not reliably change colour and brightness, so I am using a script to change the Hue scene, checking what the lights think their brightness should be and then re-setting the brightness.

Running the automation, I get the following in the logs:

2019-10-18 13:59:39 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Running script
2019-10-18 13:59:39 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service
2019-10-18 13:59:39 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step delay 0:00:02
2019-10-18 13:59:42 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service
2019-10-18 13:59:42 ERROR (MainThread) [homeassistant.components.system_log.external] Setting brightness for  light.hallway to  0
2019-10-18 13:59:42 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service

So the first part runs and sets the scene correctly, if I increase the delay to 30 seconds I can see the scene change and the light comes on.

The second part then fails to evaluate correctly and returns 0 so switches the lights back off again. If I remove the:
| int

it returns

None

instead and so throws an error (as expected):

2019-10-18 13:58:29 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Running script
2019-10-18 13:58:29 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service
2019-10-18 13:58:29 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step delay 0:00:02
2019-10-18 13:58:32 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service
2019-10-18 13:58:32 ERROR (MainThread) [homeassistant.components.system_log.external] Setting brightness for  light.hallway to  None
2019-10-18 13:58:32 INFO (MainThread) [homeassistant.helpers.script] Script hue_activate_scene_and_brightness: Executing step call service
2019-10-18 13:58:32 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 181, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 265, in _handle_action
    await self._actions[_determine_action(action)](action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 348, in _async_call_service
    context=context,
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 97, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1211, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected int for dictionary value @ data['brightness']

If I switch the light on and run the template in the GUI, I get the brightness returned as expected:

So it appears that the syntax is all fine.

My gut tells me this is a syntactical error in the template, but I have compared it my other automations and templates as well as example code online and it looks fine. Despite many, many tweaks I cannot seem to get the correct response and I am still scratching my head.

Is anybody able to offer and insights?

Thanks in advance

You dont need the second set of braces inside the template:

So try just:

      - service: light.turn_on    
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: >
            {{ state_attr(entity_id, 'brightness') | int}}
1 Like

That did it, thanks a lot! I knew it would be a simple syntax error but didn’t change that as it goes against all other examples I saw online.

1 Like