steeveno
(Stefan)
October 1, 2020, 12:56pm
1
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.
steeveno
(Stefan)
October 1, 2020, 1:02pm
3
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.
steeveno
(Stefan)
October 1, 2020, 1:11pm
5
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
steeveno
(Stefan)
October 1, 2020, 1:23pm
8
anon43302295:
Try thisâŚ
Thank you, but still the same: expected int for dictionary value @ data[âcolor_tempâ]
steeveno
(Stefan)
October 1, 2020, 1:25pm
9
I did, still the same error.
Unfortunately I run it on RaspPi, where it is not available.
Mutt
(Muttley)
October 1, 2020, 1:31pm
10
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?
123
(Taras)
October 1, 2020, 1:38pm
12
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.
steeveno
(Stefan)
October 1, 2020, 1:46pm
15
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.
steeveno
(Stefan)
October 1, 2020, 1:47pm
16
123:
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
.
While I am testing I have it on the whole time. And as I said, in the dev tools the template just runs fine.
steeveno
(Stefan)
October 1, 2020, 1:47pm
17
Ahh ok, thatâs great, thank youI I will definitly do that.
Mutt
(Muttley)
October 1, 2020, 1:48pm
18
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 !
steeveno
(Stefan)
October 1, 2020, 1:51pm
20
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.