Trying to template light color_name

Hi all, I’ve been trying for hours to get this automation to work, This is as far as I’ve got, when I check the config, the spinner just spins.

I’m trying to get the light to be either red or green depending on the bin (Recycle)

I just can’t seem to get there.

- action:
  - delay: 00:00:05
  - service: light.turn_on
    entity_id: light.kitchen_rgb_strip
    data_template:
      color_name: >
        {% if states.sensor.waste_collection_bin.state == "Recycle"  %}
          green
        {% else %}
          red
        {% endif %}
  - delay: 00:00:05
  - service: light.turn_on
    data:
      entity_id: light.kitchen_rgb_strip
      color_name: white
  alias: Flash kitchen lights Bins
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template: '{% set start_date = as_timestamp(states.calendar.bins.attributes.start_time) %}
                       {% set date = as_timestamp(now())  %}
                       {%set diff = start_date -date %}
                       {{ diff<86400 }}'  # compare diff in seconds, will match conditionwhen true
    - condition: time
      after: '18:00:00'
      before: '09:00:00'
  id: '1728051588987'
  trigger:
    entity_id: group.kitchen_motion_sensors
    platform: state
    to: 'on'

Thanks.

Several edits later, the complete automation appears.

Please post the entire automation.

Ensure the desired state value of sensor.waste_collection_bin is spelled correctly. For example, go to Developer Tools > States and check if the sensor’s state is Recycle or recycle.


EDIT

OK, there was a lot more to that automation than you initially posted …

I’ve edited the original post, but now I’ll explain a little better.

The whole script is noob (I manage), also it worked fine until I changed the light.turn_on part. The template is good, it works fine, however I think it’s my formatting, where does the data_template go and the entity_id?

Why is everything in that automation so terribly out of order?

Automations created with the Automation Editor usually sort everything in alphabetical order (which isn’t ideal but that’s another issue) but this isn’t even that.

It seems to be missing:

action:

I create my automations Frankenstyle, I get a little from here and a little from there :wink:

Ah
- action:
is the (missing) first line I neglected to Copy.

Here’s a first pass at de-Frankenizing it (includes the missing action: statement).

- id: '1728051588987'
  alias: Flash kitchen lights Bins
  trigger:
    entity_id: group.kitchen_motion_sensors
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: >
      {% set start_date = as_timestamp(states.calendar.bins.attributes.start_time) %}
      {{ (start_date - now().timestamp()) < 86400 }}
  - condition: time
    after: '18:00:00'
    before: '09:00:00'
  action:
  - delay: 00:00:05
  - service: light.turn_on
    entity_id: light.kitchen_rgb_strip
    data_template:
      color_name: >
        {{ 'green' if states.sensor.waste_collection_bin.state == 'Recycle' else 'red' }}
  - delay: 00:00:05
  - service: light.turn_on
    data:
      entity_id: light.kitchen_rgb_strip
      color_name: white

What the hell have you done to her!!

Just kidding, I’ll give it a bash.

The Configuration validation tool won’t finish, just spins. I’ll restart the system and see what happens.

Well, I tried giving it a restart, but I got a large error in the log and no restart.

SyntaxError: invalid syntax
2020-03-26 00:40:45 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall homeassistant.restart (c:c1af35782661429db367bdc7950d662e)>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1242, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/src/homeassistant/homeassistant/core.py", line 1257, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/hassio/__init__.py", line 264, in async_handle_core_service
    errors = await conf_util.async_check_ha_config_file(hass)
  File "/usr/src/homeassistant/homeassistant/config.py", line 804, in async_check_ha_config_file
    res = await check_config.async_check_ha_config_file(hass)
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 121, in async_check_ha_config_file
    component = integration.get_component()
  File "/usr/src/homeassistant/homeassistant/loader.py", line 268, in get_component
    cache[self.domain] = importlib.import_module(self.pkg_path)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 860, in get_code
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/plex_assistant/__init__.py", line 76
    if aliases
             ^
SyntaxError: invalid syntax```

After copy-pasting the new automation, did you run Configuration Check? Answer: Yes, you did but it took longer than expected so you chose to restart Home Assistant.

It’s not necessary to restart Home Assistant to reload automations. Just use Configuration > Server Controls > Reload Automations.

The error message is mentioning something that doesn’t even exist in the automation posted above. It appears to be complaining about something in one of your custom components called plex_assistant.

Yeah, sorry when I said noob, I meant sh*t coder, I’ve been using HA for a few years, so have experience.

I ran the Configuration Checker, but it won’t ‘compete’ just spins, and I forgot about the reload Automations.

I see the error is in relation to the plex assistant that is in development, however it doesn’t show up if I remove the above automation.

Oh, ar*e. there’s nothing wrong with it, it just so happens that I’ve spent the last few hours trying to fix a problem with this automation when the problem was with a very recently updated plug-in.

Sorry to have wasted your time.

Just to rub it in, I’ve gone to my HACS page and there is an update for plex assistant.

Well now you know where the real problem lies (and you have a de-Frankensteinized automation).

And I thank you for that :slight_smile: