Error executing script in automation

Hey guys,

I have a problem with an automation and I don’t know how to solve it.
Error message:

Raum reinigen: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

This is my automation (it is a select option for my roborock vacuum):

- id: '1589127558032'
  alias: Raum reinigen
  description: Reinigt den in der UI gewählten Raum
  trigger:
  - entity_id: input_select.cleaning_room
    from: Raum wählen
    platform: state
  condition: []
  action:
  - data_template:
      entity_id: >
        {% if is_state('input_select.vacuum_room', 'Wohnzimmer') %}
          script.vacuum_wohnzimmer 
        {% elif is_state('input_select.vacuum_room', 'Schlafzimmer') %} 
          script.vacuum_schlafzimmer 
        {% elif is_state('input_select.vacuum_room', 'Küche') %} 
          script.vacuum_kueche 
        {% elif is_state('input_select.vacuum_room', 'Badezimmer') %} 
          script.vacuum_badezimmer 
        {% elif is_state('input_select.vacuum_room', 'Büro') %} 
          script.vacuum_office 
        {% elif is_state('input_select.vacuum_room', 'Eingangsbereich') %} 
          script.vacuum_eingangsbereich 
        {% elif is_state('input_select.vacuum_room', 'Flur') %} 
          script.vacuum_flur 
        {% else %} {% endif %}
    service: script.turn_on
  - data:
      state: Raum wählen
    entity_id: input_select.cleaning_room
    service: input_select.select_option 

This is one of the called scripts:

vacuum_wohnzimmer:
  alias: Wohnzimmer saugen
  sequence:
  - service: vacuum.send_command
    entity_id: vacuum.roborock_s5_max 
    data: 
      command: app_segment_clean
      params: [18]

The script is working fine (direct call from the home assistant UI).

        {% else %} {% endif %}

You need to have an else, otherwise if none of the tests are met it’ll result in the error you’ve shown. You could make a dummy script that does nothing and use that as the else, or adapt your current logic to it by replacing the last test (Flur) with an else.

I can’t keep it straight though… is pos 1 0 indexed or 1 indexed? I’m used to seeing something like [data][0][entity_id]

1 Like

Use a service template. Scripts are services.

action:
  - service_template:
        {% if is_state('input_select.vacuum_room', 'Wohnzimmer') %}
          script.vacuum_wohnzimmer 
        {% elif is_state('input_select.vacuum_room', 'Schlafzimmer') %} 
          script.vacuum_schlafzimmer 
        {% elif is_state('input_select.vacuum_room', 'Küche') %} 
          script.vacuum_kueche 
        {% elif is_state('input_select.vacuum_room', 'Badezimmer') %} 
          script.vacuum_badezimmer 
        {% elif is_state('input_select.vacuum_room', 'Büro') %} 
          script.vacuum_office 
        {% elif is_state('input_select.vacuum_room', 'Eingangsbereich') %} 
          script.vacuum_eingangsbereich 
        {% elif is_state('input_select.vacuum_room', 'Flur') %} 
          script.vacuum_flur 
        {% endif %}

You don’t need an else case if you have all the options of your input select covered by the if/elif statements.
You will have to fix the indentation, there is something wrong with editing it on this iPad.

Also there is no ‘state’ for the input select set option service. It is ‘option’:

  - service: input_select.select_option 
    data:
      option: Raum wählen
      entity_id: input_select.cleaning_room
2 Likes

Ah nice, good catch. Yeah I suppose an else isn’t needed here but it’s good practice at least.

1 Like

It is indeed.

Thank you so much. I’ll try it :slight_smile:

// edit:
I changed added a “>” after the service_template. Otherwise the configuration check ended with an error.

But it is not working now. I get this error now:

Raum reinigen: Error executing script. Unexpected error for call_service at pos 1: Template rendered invalid service:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 87, in async_prepare_call_from_config
    domain_service = cv.service(domain_service)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 411, in service
    raise vol.Invalid(f"Service {value} does not match format <domain>.<name>")
voluptuous.error.Invalid: Service  does not match format <domain>.<name>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 154, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _prep_call_service_step
    return async_prepare_call_from_config(self._hass, self._action, self._variables)
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 95, in async_prepare_call_from_config
    ) from ex
homeassistant.exceptions.HomeAssistantError: Template rendered invalid service: 

An alternative method:

action:
  - service_template: >
      {% set values = { 'Wohnzimmer': 'wohnzimmer', 'Schlafzimmer': 'schlafzimmer', 'Küche': 'kueche', 'Badezimmer': 'badezimmer', 'Büro': 'office', 'Eingangsbereich': 'eingangsbereich', 'Flur': 'flur' } %}
      {% set v = states('input_select.vacuum_room') %}
      script.vacuum_{{ values[v] if v in values.keys() else 'error' }}

Create an extra script called script.vacuum_error that contains nothing (or sends a persistent_notification reporting that an error occurred).

3 Likes

Thank you :slight_smile:

I changed

{% set v = states('input_select.vacuum_room') %}

to

{% set v = states('input_select.cleaning_room') %}

Now it works :smile:

1 Like

trying to always use an ‘else’ clause, because the template might fail, how unexpected you might believe it to be, I mostly use the last option (elif in this case, or @123 's mapper) in the ‘else’ in my mappers:

action:
  - service_template: >
      {% set values = { 'Wohnzimmer': 'wohnzimmer', 'Schlafzimmer': 'schlafzimmer', 'Küche': 'kueche', 'Badezimmer': 'badezimmer', 'Büro': 'office', 'Eingangsbereich': 'eingangsbereich' } %}
      {% set v = states('input_select.vacuum_room') %}
      script.vacuum_{{ values[v] if v in values.keys() else 'flur' }}

serving 2 situations, either the true option for flur, or the guard (of course, you can’t determine if things went awol this way, but since you ‘know’ only these options will ever exist, you could have a go at it :wink: Cut a few extra lines creating an extra script ‘error’.

see this, I dont have any of the entities, and still flur is selected:

btw, I mostly use another notation too:

script.vacuum_{{ values[v] if v in values else 'flur' }}

cut yet again a few characters :wink: