RobDYI
January 24, 2019, 9:48pm
1
Please help me find out the correct way to format this condition.
automation mqtt_config_entity_creator:
alias: mqtt_config_entity_creator
trigger:
platform: mqtt
topic: 'homeassistant/binary_sensor/#'
condition:
condition: template
value_template: "{{ trigger.topic.split("/")[3] == 'device_class' }}"
I get
Configuration invalid
Error loading /Users/xxx/.homeassistant/configuration.yaml: while parsing a block mapping in “/Users/xxx/.homeassistant/configuration.yaml”, line 1504, column 5 expected <block end>, but found ‘<scalar>’ in “/Users/xxx/.homeassistant/configuration.yaml”, line 1505, column 46
silvrr
January 24, 2019, 9:52pm
2
What is line 1505? Try using only single quotes inside the template with double outside.
Try value_template: "{{ trigger.topic.split('/')[3] == 'device_class' }}"
instead of value_template: "{{ trigger.topic.split("/")[3] == 'device_class' }}"
1 Like
RobDYI
January 24, 2019, 9:56pm
3
Thank you, it made it through the config check.
silvrr
January 24, 2019, 9:58pm
4
good to hear.
I think it treats the quotes in groups from left to right so all it saw was, "{{ trigger.topic.split("
I have found it useful to always use double quotes on the outside of the {{ }} and singles for anything inside the {{ }} .
3 Likes
petro
(Petro)
January 25, 2019, 12:57pm
5
Yah, a word of advice, don’t use a generic text editor. Use something like notepad++ to view your configurations. quote manipulation will stand out. For example take a look at what you wrote using the markdown on this forum:
value_template: "{{ trigger.topic.split("/")[3] == 'device_class' }}"
Notice how you have 2 red sections separated by a black slash? That essentially shows you what the parser sees. 2 strings with a non-string in between.
Now take a look at @silvrr solution.
value_template: "{{ trigger.topic.split('/')[3] == 'device_class' }}"
One large string!
Text editors easily point this stuff out visually. Always good to use something that understands yaml.
2 Likes
Next to that, I’d suggest to take it a little further and use the multi-line notation, to prevent errors with those quotes in the first place. Make life easy (as possible…):
value_template: >
{{ trigger.topic.split('/')[3] == 'device_class' }}
petro
(Petro)
January 25, 2019, 1:13pm
7
Yep, because it helps even more by splitting up your code when using something that displays markdown:
value_template: >
{{ trigger.topic.split('/')[3] == 'device_class' }}
See how numbers, strings, and objects/methods are all different colors?
petro:
all different colors?
I never understand why you show colors and I don’t… though I use the <> and have colors in my editor?
petro
(Petro)
January 25, 2019, 1:24pm
9
Thats why!
using ` x3 (top left most button on keyboard below escape, typically also has the ~ button on it)
# this will markdown like python, remove word python to just use general markdown
print('hello world')
not on a MacBook air it is
value_template: >
{{ trigger.topic.split('/')[3] == 'device_class' }}
bingo!
cool, thanks!
1 Like
now that I have you here on templates… please allow this distantly related question:
using this:
laundry_schedule_night:
friendly_name: Laundry schedule night
value_template: >
{{states('sensor.laundry_schedule')[1:-13]}}
laundry_schedule_day:
friendly_name: Laundry schedule day
value_template: >
{{states('sensor.laundry_schedule')[11:-3]}}
gives me:
based on:
would there be a better way to template these times out of the mother sensor. Using .split or some other jinja technique? I can split at the / but then I would still need to cut the last :00 and starting T…
or maybe even one step extra back: these mother sensors are created with this:
- platform: command_line
name: Laundry schedule
command: curl -X GET http://192.168.1.212/api/API/rules/93
value_template: >
{{ value_json.conditions[0].value }}
so maybe change the value_template?
hop over to Hue motion sensors + remotes: custom component for the full thread…
petro
(Petro)
January 25, 2019, 1:37pm
13
I mean, if the mother sensor never changes it’s format, then no reason to change it.
Otherwise you could do:
{{ states('sensor.laundry_schedule').split('\\')[0][4:] }}
and
{{ states('sensor.laundry_schedule').split('\\')[1][4:] }}
not really:
changed your second template cause that results in
Error rendering template: UndefinedError: list object has no element 1
vloris
January 25, 2019, 1:52pm
15
There is a forward slash (’/’), not a backslash (’\’) in there, you might want to change split('\\')
to split('/')
sorry for that…
but, had to change anyway:
{ states('sensor.laundry_schedule').split('/')[0][1:-3] }}
{{ states('sensor.laundry_schedule').split('/')[1][1:-3] }}
{{states('sensor.laundry_schedule')[1:-13]}}
{{states('sensor.laundry_schedule')[11:-3]}}
is this just a formatting thing, or would you prefer one fo these above the other, and if so why?
petro
(Petro)
January 25, 2019, 2:17pm
18
if the data changes shape, then you’d want to go with the method I posted. If it’s always the same, either works fine and it’s up to you.
a yes, of course! clever… Don’t think it will ever change, how could it in fact, but will change to your setup anyhow. Like the rationale behind it. intelligence vs brute force.cool.
1 Like
RobDYI
January 26, 2019, 1:08am
20
I got my project working, thanks again.
Great, you might want to shut off the automation after a period of time, especially with retain on.
initial_state: 'on'
- delay:
minutes: 1
- service: automation.turn_off
entity_id: mqtt_config_entity_creator