Error in Automation lights

I am getting this error in an automation.

2020-09-27 22:17:01 ERROR (MainThread) [homeassistant.components.automation.lights_off_failsafe] Lights Off Failsafe: Repeat at step 2: Error executing script. Invalid data for call_service at pos 2: not a valid value for dictionary value @ data['entity_id']
2020-09-27 22:17:01 ERROR (MainThread) [homeassistant.components.automation.lights_off_failsafe] Lights Off Failsafe: Error executing script. Invalid data for repeat at pos 2: not a valid value for dictionary value @ data['entity_id']
2020-09-27 22:17:01 ERROR (MainThread) [homeassistant.components.automation.lights_off_failsafe] While executing automation automation.lights_off_failsafe
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 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 512, in _async_repeat_step
    await async_run_sequence(iteration, extra_msg)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 494, in async_run_sequence
    await self._async_run_script(script)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 626, in _async_run_script
    await self._async_run_long_action(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 375, in _async_run_long_action
    long_task.result()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 413, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1269, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 208, 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 287, 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 283, 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/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: not a valid value for dictionary value @ data['entity_id']

The automation is a failsafe for turning some milights on and off. Sometimes, turning them on or off doesn’t work the first time… a quirk of the radio in those lights I think. So I wrote an automation to repeat the on/off command triggered by the state of the lights changing. However I have 4 groups so I test for last_triggered to determine which lights to hit again with the command.
The template seems good in the ediitor.
The above error last night was from turning the lounge and desk lights off.
Anyway, here is the off automation:

  - id: '1532478833426'
    alias: Lights Off Failsafe
    trigger:
    - platform: state
      from: 'on'
      to: 'off'
      entity_id: light.lounge, light.dining, light.desk, light.office
    condition: []
    mode: restart
    action:
    - service: automation.turn_off
      entity_id: automation.lights_on_failsafe
    - repeat:
        count: '2'
        sequence:
        - delay: '00:00:02'
        - service: light.turn_off
          data:
            entity_id: >
              {%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed)
              < 10) -%} {%- set h0='light.lounge' -%} {%- else -%} {%- set h0='' -%} {%- endif -%} 
              {%- if (as_timestamp(now()) - as_timestamp(states.light.dining.last_changed)
              < 10) -%} {%- set h1='light.dining' -%} {%- else -%} {%- set h1='' -%} {%- endif -%} 
              {%- if (as_timestamp(now()) - as_timestamp(states.light.desk.last_changed)
              < 10) -%} {%- set h2='light.desk' -%} {%- else -%} {%- set h2='' -%} {%- endif -%} 
              {%- if (as_timestamp(now()) - as_timestamp(states.light.office.last_changed)
              < 10) -%} {%- set h3='light.office' -%} {%- else -%} {%- set h3='' -%} {%- endif -%} 
              {{ ' '+ [h0, h1, h2, h3]|join(', ')|trim(', ') }}
        - delay: '00:00:03'
    - service: automation.turn_on
      entity_id: automation.lights_on_failsafe

@petro if anyone can see my error it’s you…

try a different template

{% set t = as_timestamp(now()) %}
{% set lights = [ 
  states.light.lounge,
  states.light.dining,
  states.light.desk,
  states.light.office ] %}
{% set ns = namespace(entities=[]) %}
{% for state in lights if t-as_timestamp(state.last_changed) < 10 %}
  {% set ns.entities = ns.entities + [ state.entity_id ] %}
{% endfor %}
{{ ns.entities | join(', ') or 'none' }}

If you’re dead set on doing it with if elses use namespace to add to the list

{% set ns = namespace(lights=[]) %}
{% set t = as_timestamp(now()) %}
{% if as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 10 %}
  {% set ns.lights = ns.lights + [ 'light.lounge' ] %}
{% endif %}
{% if as_timestamp(now()) - as_timestamp(states.light.dining.last_changed) < 10 %}
  {% set ns.lights = ns.lights + [ 'light.dining' ] %}
{% endif %}
{% if as_timestamp(now()) - as_timestamp(states.light.desk.last_changed) < 10 %}
  {% set ns.lights = ns.lights + [ 'light.desk' ] %}
{% endif %}
{% if as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 10 %}
  {% set ns.lights = ns.lights + [ 'light.office' ] %}
{% endif %}
{{ ns.entities | join(', ') or 'none' }}

I’m not dead set on anything other than making it work.
Do you know why my one isn’t working? (Just out of interest?)

I’m not sure, i’d have to debug it. It could be that you need to have an entity_id for light.turn_off or the all or none. I could see that being empty if everything passes first time.

ah I didn’t think of that…
But that’s why I picked 10 seconds. 2 seconds first delay + 3 at end plus 2 at start of repeat is 7 seconds so should have time to spare without being blank when it executes the service call.
If that is the case then I should use say 15 seconds instead…

OK I see now why it’s happening (well I don’t understand WHY)

So the join is putting a blank one in for mine but not yours. So I end up with 2 commas not one…Any idea why that is happening?

well, it’s because you have if statements and a static list. One of the items is off so you join a list of [’’, ‘’, ‘’, ‘’] which results in ‘, , , ,’. If you changed your last line to:

{{ [h0, h1, h2, h3]|reject('eq','')|list|join(', ') or 'none' }}
1 Like

so now it makes sense. thanks. I actually don’t need the none option anyway because it’s never triggered unless one of those lights is on. I increased to 15 seconds in case it times out but I think my error was in the 2 commas which I hadn’t tested with those 2 lights in the template editor.