Vacation mode triggered by alarm

ok so what do we do now? since my lights are not going on ? and randomizing?

fwiw, I use this script in another randomization lighting automation and works just fine:

  lights_partying_on:
    alias: Lights partying on
    sequence:
      service: light.turn_on
      data_template:
        entity_id: light.{{['dining_corner','cabinet','kist','home_theater']|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) }}'

no groups, but individual lights listed. Since you’d have to list them too in a group, if you use the group, that’s not much of a difference I guess?

the timing is organized int the automation calling this script, which obviously has another purpose ;-):

automation:
  - alias: 'Lights partying'
    id: 'Lights partying'
    initial_state: 'off'
    trigger:
      platform: time
      seconds: '/4'   # {{ "/" ~ ((range(2, 5) | random) | int) }}
    condition:
      condition: template
      value_template: >
        {{is_state('automation.sense_lights_change','off') }}
    action: 
      service: script.lights_partying_on

thanks I had a script that worked but problem was it could turn on all lights as mentioned in a previous post.
Will yours limit the number of lights being open at the same time?

that wouldn’t be much of a party… no it simple turns the lights on, no matter which other lights are on already. Just posted it for you to see if this way of randomizing the lights turning on would work for you. Not as a full replacement.

btw if your interested, Ive had a vacation lights automation before, which is obsolete now, but has another way of setting the delay. to make it even more random, and take the sun into account:

  - alias: Vacation lights
    trigger:
      - platform: sun
        event: sunset
        offset: '-00:45:00'
      - platform: time
        at: '22:00:00'
    condition:
      condition: template
      value_template: >
        {{ is_state('input_boolean.home_mode_vacation', 'on') and 
           is_state('group.family','not_home')}}
    action:
      - delay: >
          {% if trigger.platform == 'sun' %}00:00:10
          {% else %}0{{ (range(0, 1)|random|int) }}:{{ (range(10, 59)|random|int) }}:{{ (range(10, 59)|random|int) }}
          {% endif %}
      - service_template: >
          light.turn_{% if trigger.platform == 'sun' %}on{% else %}off{% endif %}
        entity_id:
          - light.office_outside
          - light.etc.

thanks
I liked mine it worked except would like to limit to only 2 lights at a time but choosing from 7 available lights.

Going out tonight so I will Petro’s suggestion run a few hours and see how it goes. Although history seems to be broken…

Hi tried running this last night from 7:30 to 11Pm while I was out. Looked at the overview page once and a while and nothing happens. When I came home only one light was one which is the one I left on before leaving.
What can we try?
Thanks

is there anything in the logs? Does it show that the service is executing?

Unfortunately since new updates my logs and history are not working

I’m not talking about logger or history in the GUI, not even the GUI i button with a circle around it. I’m referring to the homeassistant.log text file inside your config folder. This will always work and it will contain your errors. In fact, you should always consult this file because it will contain errors that do not appear anywhere else.

Hi I rebooted system armed alarm
2 errors seem to show up often
ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2:

File “”, line 1, in top-level template code
TypeError: ‘<’ not supported between instances of ‘int’ and ‘str’

I have included a partial log file since all was too big to see if it helps

2019-02-01 18:53:34 WARNING (Recorder) [homeassistant.components.recorder] Ended unfinished session (id=62 from 2019-02-01 22:58:52.927583)
2019-02-01 18:53:37 ERROR (Recorder) [homeassistant.components.recorder.util] Error executing query: (sqlite3.DatabaseError) database disk image is malformed [SQL: 'INSERT INTO events (event_type, event_data, origin, time_fired, created, context_id, context_user_id) VALUES (?, ?, ?, ?, ?, ?, ?)'] [parameters: ('service_registered', '{"domain": "recorder", "service": "purge"}', 'LOCAL', '2019-02-01 23:53:30.553296', '2019-02-01 23:53:37.950369', 'f903adda71b142c882f9f6581642ee8a', None)] (Background on this error at: http://sqlalche.me/e/4xp6)
2019-02-01 18:59:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:01:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:03:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:05:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:07:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:09:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:11:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:13:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 138, 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 "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'int' and 'str'
2019-02-01 19:17:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.random_away_lights. Unknown error for call_service at pos 2: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1129, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1151, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 123, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 181, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 326, in _async_check_condition
    check = config(self.hass, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 316, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/condition.py", line 298, in

@aqus555

Ok, i see the problem. But I also see other problems in you stuff. Anyways to the problem at hand:

This script should work now. I’m guessing you put light_count_limit in quotes in the automation? If so, remove the quotes. Either way, this should with with or without the quotes.

script:
  my_random_light:
    sequence:
      - condition: template
        value_template: >
          {%- set count = states | selectattr('entity_id','in',state_attr(random_group,'entity_id')) | selectattr('state','eq','on') | list | length %}
          {{ is_state(random_entity_id, 'on') or (is_state(random_entity_id, 'off') and count < light_count_limit | int) }}
      - service: homeassistant.toggle
        data_template:
          entity_id: "{{ random_entity_id }}"

Also, you are getting a malformed database error. Your home assistant DB is corrupted, you’ll probably have to delete that and restart the system.

Thank-you It is working fine.
BTW there were no quotes in light_count_limit.
tried last night while I was home and sometimes one light was on sometimes none briefly and most of the times two lights, Exactly what I wanted. Thank-you again for your time.

1 Like

HI getting back to this for a second, Im wondering if this is correct:

since it outputs to:
- delay: 00:8:00

shouldn’t it be something like this:

      - delay: 00:{{ range(0,2) | random | int }}{{ range(2,7) | random | int }}:00

for the template to calculate the minutes correctly?

Im trying to have a second action with a few (random) seconds and added this, seems alright in the template editor:

      - delay: 00:{{ range(0,2) | random | int }}{{ range(2,7) | random | int }}:00
      - service: script.home_mode_locked_lights
        data_template:
          random_group: group.home_mode_locked_lights_1
          random_entity_id: >
            {{ state_attr('group.home_mode_locked_lights_1','entity_id') | random }}
          light_count_limit: 4
      - delay: 00:00:{{ range(0,2) | random | int }}{{ range(2,7) | random | int }}
      - service: script.home_mode_locked_lights
        data_template:
          random_group: group.home_mode_locked_lights_2
          random_entity_id: >
            {{ state_attr('group.home_mode_locked_lights_2','entity_id') | random }}
          light_count_limit: 3

I always thought that, but there are a number of people on the forums here who aren’t bothering making that 2 characters long, and it appears to work for them. So why bother if it works?

I mean, the variation there is hard to know off the bat. If you REALLY want to constrain it to 2 characters…

00:00:{{ '{:02}'.format(range(1, 60) | random) }}

EDIT: Probably should add a range of 1 to 60 so that you don’t get zero.

1 Like

thanks Petro,

much nicer.

using it in both places like this:

    action:
      - delay: 00:{{ '{:02}'.format(range(2, 27) | random) }}:00
#      - delay: 00:{{ range(0,2) | random | int }}{{ range(2,7) | random | int }}:00
      - service: script.home_mode_locked_lights
        data_template:
          random_group: group.home_mode_locked_lights_1
          random_entity_id: >
            {{ state_attr('group.home_mode_locked_lights_1','entity_id') | random }}
          light_count_limit: 4
      - delay: 00:00:{{ '{:02}'.format(range(9, 45) | random) }}
#      - delay: 00:00:{{ range(0,2) | random | int }}{{ range(2,7) | random | int }}
      - service: script.home_mode_locked_lights
        data_template:
          random_group: group.home_mode_locked_lights_2
          random_entity_id: >
            {{ state_attr('group.home_mode_locked_lights_2','entity_id') | random }}
          light_count_limit: 3