Simple Light Automation Not triggering?

Hi, I’m trying to create a simple light transition to change scenes depending on the time of the day. Does anyone know what I’m doing wrong?

  • id: transition_light
    alias: “Light transition”
    trigger:
    • platform: time
      minutes: ‘/5’
      seconds: 00
      condition:
    • condition: state
      entity_id: group.lights
      state: ‘on’
      action:
    • service: scene.turn_on
      data_template:
      entity_id: >
      {% if (float(as_timestamp(now)) > 18) and (float(as_timestamp(now)) < 19)%} # 6pm ~ 7pm
      scene.sunset
      {% elif (float(as_timestamp(now)) > 19) and (float(as_timestamp(now)) < 20)%} # 7pm ~ 8pm
      scene.feet_up
      {% elif (float(as_timestamp(now)) > 20) and (float(as_timestamp(now)) < 21)%} # 8pm ~ 9pm
      scene.Laila
      {% elif (float(as_timestamp(now)) > 21) and (float(as_timestamp(now)) < 22)%} # 9pm ~ 10pm
      scene.Jump
      {% elif (float(as_timestamp(now)) > 22) and (float(as_timestamp(now)) < 23)%} # 10pm ~ 11pm
      scene.relax
      {% elif (float(as_timestamp(now)) > 23) and (float(as_timestamp(now)) < 24)%} # 11pm ~ 12am
      scene.bedtime
      {% else %}
      scene.night_light
      {% endif %}

The error I’m seeing in the logs are:
2017-08-20 08:45:00 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/local/lib/python3.6/asyncio/tasks.py”, line 180, in _step
result = coro.send(None)
File “/usr/src/app/homeassistant/components/automation/init.py”, line 343, in async_trigger
yield from self._async_action(self.entity_id, variables)
File “/usr/src/app/homeassistant/components/automation/init.py”, line 433, in action
yield from script_obj.async_run(variables)
File “/usr/src/app/homeassistant/helpers/script.py”, line 151, in async_run
yield from self._async_call_service(action, variables)
File “/usr/src/app/homeassistant/helpers/script.py”, line 181, in _async_call_service
self.hass, action, True, variables, validate_config=False)
File “/usr/src/app/homeassistant/helpers/service.py”, line 74, in async_call_from_config
config[CONF_SERVICE_DATA_TEMPLATE]))
File “/usr/src/app/homeassistant/helpers/service.py”, line 70, in _data_template_creator
for key, item in value.items()}
File “/usr/src/app/homeassistant/helpers/service.py”, line 70, in
for key, item in value.items()}
File “/usr/src/app/homeassistant/helpers/service.py”, line 72, in _data_template_creator
return value.async_render(variables)
File “/usr/src/app/homeassistant/helpers/template.py”, line 101, in async_render
return self._compiled.render(kwargs).strip()
File “/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.6/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘int’

Hi @bobokun, if you paste the above in Dev Tools/Templates you will see that it returns ‘None’.
as_timestamp(now()) returns the current timestamp (1503249967.039868) but this is not what you want for your template.
If you want to compare the current hour you can use something like:

now().strftime("%H") | int < 19
1 Like

Hi @VDRainer! Thank you so much that fixed the issue :slight_smile: Sorry I’m quite new with Home Assistant. How do you test a piece of code from the template? When I copied the code it just shows up as a string in the output

Just put it in double curly brackets like:

{{ now().strftime("%H") }}

or you can paste your whole template (from first ‘{%’ to last ‘%}’) to see what you get.

1 Like