One Automation with multiple conditions for changing light color

With this automation I would like, if

  • Garage open -> light is red
  • garage closed -> light green
  • Garage unknown -> yellow.

In the logs I always get the error:



Logger: homeassistant.components.automation.alarm_garagentor
Source: core.py:1289
Integration: Automatisierung (documentation, issues)
First occurred: 13:01:12 (1 occurrences)
Last logged: 13:01:12
While executing automation automation.alarm_garagentor

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 426, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 985, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 239, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 247, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 454, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1289, 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 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 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 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['xy_color']


- id: '1602449066927'
  alias: Alarm Garagentor
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.garage_modtm_door_state
  condition: []
  action:
  - service: light.turn_on
    data_template:
      xy_color: >-
        {% if is_state('sensor.garage_modtm_door_state', 'unknown') %}
        [0.16,0.504]
        {% elif is_state('sensor.garage_modtm_door_state', 'closed') %}
        [0.16,0.504]
        {% elif is_state('sensor.garage_modtm_door_state', 'open') %}
        [0.16,0.504]
        {% endif %}
    entity_id: light.0x00158d0003912bec_light
  - delay: 00:00:10
  - service: light.turn_off
    data: {}
    entity_id: light.0x00158d0003912bec_light
  mode: restart

Try below simplified code:


  action:
  - service: light.turn_on
    data:
      color_name: "{% if is_state('sensor.garage_modtm_door_state', 'unknown') %}yellow{% elif is_state('sensor.garage_modtm_door_state', 'closed') %}green{% else %}red{% endif %}"
    entity_id: light.0x00158d0003912bec_light

With color name it works fine, but why not with xy or RGB in tempalte. If I use rgb or xy color without tempalte it works .

You can replace color_name with rgb_color and specify colour values instead of colour name in the template. It would look like as below for red:

rgb_color : "{... [255, 0, 0] ...}"

green would be [0, 255, 0] and yellow would be [255, 255, 0]. You placed same xy_color value for all 3 colours in your original ask so will not see colour changing. You also had one extra elif in your template.