Why does template not working

Hi everyone.

Im trying to automize some lights and came across the error: Error rendering data template: Result is not a Dictionary

The template im using is:

service_template: >
  {% set name = 'light.led' %}{% set state = 'ein' %}
  {% set entity = name | string %} {% set case = entity.split('.')[0] %} {%set
  this_state = state | string %} {% if case == 'light' %}
    {% if this_state == 'ein' %}
      homeassistant.turn_on
    {% else %}
      homeassistant.turn_off
    {% endif %}
  {% else %}
    {% if this_state == 'ein' %}
      homeassistant.turn_on
    {%else %}
      homeassistant.turn_off
    {% endif %}
  {% endif %}
data_template: >
  {% if case == 'light' and this_state == 'ein' %}
    entity_id: '{{ name }}'
  {% else %}
    entity_id: '{{ name }}'
  {% endif %}

and the complete script is

alias: turn lights on
fields:
  name:
    description: toggle Entity
    example: light.led
  state:
    description: State to change the light to
    example: 'on'
sequence:
  - service_template: >
      {% set name = 'light.led' %}{% set state = 'ein' %} {% set entity = name |
      string %} {% set case = entity.split('.')[0] %} {%set this_state = state |
      string %} {% if case == 'light' %}
        {% if this_state == 'ein' %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      {% else %}
        {% if this_state == 'ein' %}
          homeassistant.turn_on
        {%else %}
          homeassistant.turn_off
        {% endif %}
      {% endif %}
    data_template: |
      {% if case == 'light' and this_state == 'ein' %}
        entity_id: '{{ name }}'
      {% else %}
        entity_id: '{{ name }}'
      {% endif %}
mode: single

the services and data here are just dummies and I will change them later.

Im quite new to templating, so could anyone help me with what is wrong here?

  1. In the future please just post your actual automation/script, instead of one filled with dummy information.

  2. You are trying to use variables in the data section that aren’t defined for that section. You need to either add them to that section or move them so they are available to the whole sequence. (Variable Scope)

  3. data_template and service_template are deprecated, just use data and service.

  4. In most cases the output of a template is a string, so converting them to strings likely isn’t necessary. I’m leaving them in the example below, because there are too many “dummies” to guess about…

EDIT: See below.

1 Like

Thanks for the answer.

I updated the script to have the things I wanted it to do:

sequence:
  - variables:
      name: 'light.led'
      state: 'ein'
      entity: '{{ name | string }}'
      case: '{{ entity.split(".")[0] }}'
      this_state: '{{ state | string }}'
  - service: >
      {% if case == 'light' %}
        {% if this_state == 'ein' %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
      {% else %}
        {% if this_state == 'ein' %}
          homeassistant.turn_on
        {%else %}
          homeassistant.turn_off
        {% endif %}
      {% endif %}
    data: >
      {% if case == 'light' and this_state == 'ein' %}
        entity_id: '{{ name }}'
        color_temp: '{% if now().hour > 15 or now().hour < 4 %}{{500}}{% else %}{{153}}{% endif %}'
      {% else %}
        entity_id: '{{ name }}'
      {% endif %}

the problem is it still retruns the error that the result is not a dictionary.

What I basicly try to do is turn on the entity and if its a light, than the color temperature should be changed based on time.

Because you cannot use a Jinja2 template to generate YAML. In data, a Jinja2 template uses an if-else to determine the appearance of the entity_id and/or color_temp options (which is YAML). That’s an invalid operation and is the reason for the error message.

You need to restructure this script to use a choose.

1 Like

Sorry about that, in my haste I didn’t properly change the data section… Anyway, do what Taras said and use a Choose action.

The only part that needs special treatment is light.turn_on because you want to vary the color_temp. Everything else will work with the generic homeassistant.turn_on/off so use that for the default instead of writing so many “elses”.


sequence:
  - variables:
      name: 'light.led'
      state: 'ein'
      entity: '{{ name | string }}'
      case: '{{ entity.split(".")[0] }}'
      this_state: '{{ state | string }}'
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ case == 'light' and this_state == 'ein' }}"
        sequence:
          - service: light.turn_on
            data:
              color_temp: "{{ 500 if (15 < now().hour or now().hour < 4) else 153 }}"
              target:
                entity_id: "{{ name }}"
    default:
      - service: "homeassistant.turn_{{on if this_state == 'ein', else off}}"
        data:
          target:
            entity_id: "{{ name }}"
mode: single
alias: 'Dummy Script'
1 Like

Thanks guys, it worked now.
Although I had to change

data:
  target:
    entity_id: "{{ name }}"

to

    data:
      entity_id: '{{ name }}'

Thank you both for your help.

That should be:


    default:
      - service: "homeassistant.turn_{{on if this_state == 'ein', else off}}"
        target:
          entity_id: "{{ name }}"