Problems with Automation & Templating

I am still quite new and I keep having problems with templating & Automations.

I do not understand whether some people use data_template and some people use service_template in automation. What is the difference?
How can It be that a template is working when I test it with dev-tools, config check runs without error, but when I trigger the automation, I get an error?
Some people use for multi-line templates >- and some use only a dash (-). What is the difference?
I often get errors regarding “value is not int”, I tried it with outputting values as {{200|int}}, since this seemed correct for me according to jinja docs. But ‘200’ seems to also do the job?

At the moment, I am especially fighting with changing the color_temp every time I press a button. I got several versions:

-service: light.turn_on
 data_template:
  color_temp: >
      {% if state_attr(''light.esstisch'', ''color_temp'') < 225 %}
        '225'
      {% elif state_attr(''light.esstisch'', ''color_temp'') < 300 %}
        '300'
      {% elif state_attr(''light.esstisch'', ''color_temp'') < 370 %}
         '370'
      {% else %}
         '153'
      {% endif %}
  transition: '1'
  entity_id: light.esstisch

I saw this very similar in some posts, but when I tested it in dev tools, the return value was put like:

color_temp: >

           153

So somewhere else I saw they put the output behind the conditions:

  - service: light.turn_on
    data_template:
        color_temp: >
            {% if state_attr('light.esstisch', 'color_temp') < 225 %} '225'
            {% elif state_attr('light.esstisch', 'color_temp') < 300 %} '300'
            {% elif state_attr('light.esstisch', 'color_temp') < 370 %} '370'
            {% else %} '153'
            {% endif %}
        transition: '1'
        entity_id: light.esstisch
  mode: single

For a short time it worked well, but now it stopped working. Get the following error:
“Error executing script. Invalid data for call_service at pos 1: expected int for dictionary value @ data[‘color_temp’]”

Anybody can help me on this? I really want to learn it.

Thank you!

You’re putting quotes round the numbers, which makes them strings. Remove the quotes.

I already tried that, I had that at the beginning and it didn’t work, still same error “no int”
I was successful with the quotes at other automations, so I tried here as well.
I also tried {{200|int}} to change it to int, still same error.

Then you have some other error. Possibly indentation.

Is there any possibility for indentation check? I normally use one tab

I reworked the indentation to 2 spaces for every layer, because that is used by home assistant automatically as well. Still not working.

  - service: light.turn_on
    data_template:
      color_temp: >
        {% if state_attr(''light.esstisch'', ''color_temp'') < 225 %}
        225
        {% elif state_attr(''light.esstisch'', ''color_temp'') < 300 %}
        300
        {% elif state_attr(''light.esstisch'', ''color_temp'') < 370 %}
        370
        {% else %}
        153
        {% endif %}
      transition: '1'
      entity_id: light.esstisch
  mode: single

Try ro replace the two single quotes you have with a proper double quote and also remove the quotes from the transition value.

Visual Studio Code is a good editor to check your code.

Try this…

  - service: light.turn_on
    data_template:
      color_temp: >
        {% if state_attr('light.esstisch', 'color_temp')|int < 225 %} 225
        {% elif state_attr('light.esstisch', 'color_temp')|int < 300 %} 300
        {% elif state_attr('light.esstisch', 'color_temp')|int < 370 %} 370
        {% else %} 153 {% endif %}
      transition: 1
      entity_id: light.esstisch

Thank you, but still the same: expected int for dictionary value @ data[‘color_temp’]

I did, still the same error.

Unfortunately I run it on RaspPi, where it is not available.

Marc’s above is just a slightly neater way of writting it than I do normally.
This should work.

Where is this automation ? /config/auotmations.yaml ?
Can you post the whole automation ?

Try this (just to test that it’s not some other problem as intimated by Marc : -

  - service: light.turn_on
    data:
      color_temp: 153
      transition: 1
      entity_id: light.esstisch

If this doesn’t work your fault lies elsewhere

Have you reloaded the automation since making the change?

I suspect light.essich doesn’t have a color_temp attribute when it is off.

That causes state_attr('light.esstisch', 'color_temp') to report None.

Install the samba add-on and then access the files from your desktop over samba.

I run it this way too

(ie A LOT of people do)

Yes, its in automations.yaml

Whole automation, I started setting it up with the UI and added the template part then directly with file editor:

- id: '1601532089129'
  alias: Neue_Automatisierung
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 46b15984031b11eb9923f1c1f3ffd571
    type: action
    subtype: up-press
    discovery_id: 0x001788010870d0ed action_up-press
  condition: []
  action:
  - service: light.turn_on
    data_template:
      color_temp: >
        {% if state_attr("light.esstisch", "color_temp")|int < 225 %} 225
        {% elif state_attr("light.esstisch", "color_temp")|int < 300 %} 300
        {% elif state_attr("light.esstisch", "color_temp")|int < 370 %} 370
        {% else %} 153 {% endif %}
      transition: 1
      entity_id: light.esstisch
  mode: single

This works perfectly.

While I am testing I have it on the whole time. And as I said, in the dev tools the template just runs fine.

Ahh ok, that’s great, thank youI I will definitly do that.

And then change it to say : -

  - service: light.turn_on
    data:
      color_temp: 370
      transition: 1
      entity_id: light.esstisch

Reload the automation, run the automation, and make sure it changes !

Also worked…

Just to make sure it is not an other fault of the light, I also tried it with this template from an other post and this works:

color_temp: '{{state_attr(''light.esstisch'', ''color_temp'') | int + 60 }}'

So it really seems to have something to do with the template.