Should we rewrite this rgb template in 117?

repost from Heads up! Upcoming breaking change in the Template integration

moving to the future in 117 with the native types in templates, trying to figure out where this will affect the config, would this be made easier now ?

    sequence:
      service: light.turn_on
      data:
        entity_id: >
          light.{{['frontdoor','dining_corner','symfonisk']|random}}
        rgb_color: ['{{(range(0,255)|random)}}',
                    '{{(range(0,255)|random)}}',
                    '{{(range(0,255)|random)}}']
        brightness: '{{(range(50,250)|random)}}'
        transition: '{{(range(1,3)|random)}}'

There have been numerous posts on the rgb values in fact being made a string before/up to now, and the above seems to work just as before.

Maybe this will work?

        rgb_color: >
          {{ (range(0,255)|random, range(0,255)|random, range(0,255)|random) | list }}

Without the terminating list filter, the result is a tuple. Iā€™m not sure if thatā€™s understood by rgb_color.

ā€¦ came back to say that my suggestion isnā€™t exactly a giant improvement over your original template ā€¦ just fewer braces.

actually:

 rgb_color: >
          {{ [range(0,255) | random, range(0,255) | random, range(0,255) | random] }}

Will it be possible to produce a list containing a variable number of items?

For rgb_color we know in advance that there will be only three items. However, Iā€™ve seen requests, predominantly for robot vacuum integrations, where the quantity of items is not fixed and varies depending on the situation (number of zones/rooms to clean). I believe one of them even uses a format that is a list of lists.

While collection types are now natively supported, you are talking about generating a list in Jinja2 templating on the fly. That requires mutability, which currently isnā€™t available in the sandbox environment the templates run in. However, I can see the added value, now native types are a thing. Definitely something to look into for the future.

1 Like

ok, thanks! and had marked this already to be solved, going from

to:

script:
# https://community.home-assistant.io/t/should-we-rewrite-this-rgb-template-in-117/238160
  lights_partying_on:
    alias: Lights partying on
    mode: restart
    sequence:
      service: light.turn_on
      data:
        entity_id: >
          light.{{['frontdoor','dining_corner','symfonisk','kist','serre_chair',
                   'lounge_chair_long','home_theater','world_map']|random}}
        rgb_color: >
          {{ [range(0,255)|random, range(0,255)|random, range(0,255)|random] }}
        brightness: >
          {{range(50,250)|random}}
        transition: >
          {{range(1,3)|random}}

which would be very nice.

unfortunately, I see this in the logs:

   return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 340, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 338, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: None for dictionary value @ data['rgb_color']

I do still have the legacy_templates: true in my configuration.yaml. Isnā€™t that compatible? Would have thought it to be able to use both legacy and new style templates? HA 117.0b5

No, that wonā€™t work. You are running with legacy_templates, which doesnā€™t support native types.

You can use the ā€œoldā€ style (for 99% ish) in the new template setting. However, you can of course not use the new language features in the old engine (if that was possible, this whole upgrade was not needed :stuck_out_tongue: )

hah, yes, feared as muchā€¦

but, before I can do that, I have to find a way to solve this:

and this:

both of which I posted in the GitHub Issue.

if you have a spare moment, might I ask you to hop over there and have your eagle eye have a look please?

Ill restart without the legacy now and see whether the solutions we tried before will work now with 117.0b5 being live.
thanks!

All those issues are addressed in b5, at least, currently there are not related GitHub issues open to my knowledge

well, weā€™re not there yet. Not only wont the rest_commands get there values to the Hue hub (in the link above) the rgb template here causes this error now:

Logger: homeassistant.components.automation.party_mode
Source: core.py:1402 
Integration: Automation (documentation, issues) 
First occurred: 10:45:03 AM (6 occurrences) 
Last logged: 10:46:56 AM

Party mode: Repeat at step 3: Error executing script. Unexpected error for call_service at pos 1: __init__() missing 1 required positional argument: 'render_result'
Party mode: Error executing script. Unexpected error for repeat at pos 3: __init__() missing 1 required positional argument: 'render_result'
While executing automation automation.party_mode
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in _async_call_service_step
    await self._async_run_long_action(service_task)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 422, in _async_run_long_action
    long_task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1448, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1483, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 215, in service_handler
    await script_entity.async_turn_on(
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 320, in async_turn_on
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1010, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 245, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 460, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1402, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 218, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 336, in _exec
    v = func(v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 338, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 338, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 818, in __call__
    v = type(v)(schema(x) for x, schema in zip(v, self._schemas))
TypeError: __init__() missing 1 required positional argument: 'render_result'

the repeat is this, calling the script:

      - repeat:
          sequence:
             - service: script.lights_partying_on
             - delay:
                  seconds: 3

meaning all of the previously mentioned issues are still openā€¦

btw the ā€˜oldā€™ rgb template

        rgb_color: ['{{(range(0,255)|random)}}',
                    '{{(range(0,255)|random)}}',
                    '{{(range(0,255)|random)}}']

does work with the new template engineā€¦

Ill raise an issue on this, to fully analyze the impact

and this has been fixed by Frenck in https://github.com/home-assistant/core/pull/42488

great! letā€™s party: