Hello guys
i stuck inside my sensor config.
My plan is to set a state of a template sensor at a specific state attribute from a folder_watcher sensor.
my config right now is
- platform: folder
folder: /config/vm_com/
- platform: template
sensors:
file_vm_com_1:
entity_id: sensor.vm_com
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list') == */test.txt %}
test
{% elif state_attr('sensor.vm_com', 'file_list') == */share.txt %}
share
{% endif %}
Thanks for your help.
fedot
July 8, 2020, 3:50pm
2
Welcome back Lylat,
I havenāt looked into the folder_watcher part yet, however youāre mixing up is_state_attr
and state_attr
Syntax:
is_state_attr('entity_id', 'attribute', 'abc')
will be true or false, depending on whether the attribute of entity_id is abc or not.
state_attr('entity_id, 'attribute')
will return the value of the attribute, which you can then use to test against.
So try:
- platform: template
sensors:
file_vm_com_1:
entity_id: sensor.vm_com
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list', '*/test.txt' %}
test
{% elif is_state_attr('sensor.vm_com', 'file_list', '*/share.txt' %}
share
{% endif %}
Thanks for you anwser. so i will try it
fedot
July 8, 2020, 4:57pm
4
Make sure the state attribute is what youāre looking for though - go to the developer tools -> states and look into the attribute of āsensor.vm_comā to make sure youāre looking for the right file
At the first point i get one sensor running
- platform: template
sensors:
file_vm_com:
entity_id: sensor.vm_com
value_template: "{{ state_attr('sensor.vm_com', 'file_list') }}"
But the other one dontā¦
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state("sensor.file_vm_com", "/config/vm_com/test.txt" ) %}
test
{% elif is_state("sensor.file_vm_com", "/config/vm_com/share.txt" ) %}
share
{% else %}
none
{% endif %}
its a diffrence between if i use a sensor state or a group state?
because this one works ā¦
- platform: template
sensors:
hauptschalter_sensor:
value_template: >-
{% if is_state('group.wlan_steckdosen', 'on') %}
on
{% elif is_state("group.lights", "on") %}
on
{% else %}
off
{% endif %}
so now i get it fixedā¦
but it is a way how i can do it easier?
thats my config right now.
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state("sensor.file_vm_com", "['/config/vm_com/test.txt']" ) %}
test
{% elif is_state("sensor.file_vm_com", "['/config/vm_com/share.txt']" ) %}
share
{% else %}
none
{% endif %}
- platform: template
sensors:
file_vm_com:
entity_id: sensor.vm_com
value_template: "{{ state_attr('sensor.vm_com', 'file_list') }}"
fedot
July 8, 2020, 5:59pm
7
Yes, you can use is_state_attr instead of is_state in the first sensor:
This way you directly check the attribute.
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list', "['/config/vm_com/test.txt']" ) %}
test
{% elif is_state_attr('sensor.vm_com', 'file_list', "['/config/vm_com/share.txt']" ) %}
share
{% else %}
none
{% endif %}
no that isnt working for meā¦
i was trying befor.
maybe im wrong but i was testing with this config.
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list', "/config/vm_com/backup_gestartet_share" ) %}
test
{% elif is_state_attr('sensor.vm_com', 'file_list', "/config/vm_com/backup_beendet_share" ) %}
share
{% else %}
none
{% endif %}```
![sensor_vm_com|690x253](upload://3mnd8mwp9IRUUzkKNOuJpWMAczX.png)
fedot
July 8, 2020, 7:20pm
9
Well in the double-sensor scenario you had "['/config/vm_com/test.txt']"
In the one you just posted there are no brackets
Does the example Iāve provided not work?
If so, go to Developer Tools -> Template and paste this on the left side:
{{ state_attr('sensor.vm_com', 'file_list') }}
This is the output you should check for.
at the new i edit its like this
the Developer Tools tells
[ā/config/vm_com/backup_beendet_shareā]
and thats my config
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list', "['/config/vm_com/backup_gestartet_share']" ) %}
share
{% elif is_state_attr('sensor.vm_com', 'file_list', "['/config/vm_com/backup_beendet_share']" ) %}
musik
{% endif %}
fedot
July 8, 2020, 8:38pm
13
Try swapping the quotes:
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if is_state_attr('sensor.vm_com', 'file_list', '["/config/vm_com/backup_gestartet_share"]' ) %}
share
{% elif is_state_attr('sensor.vm_com', 'file_list', '["/config/vm_com/backup_beendet_share"]' ) %}
musik
{% endif %}
You can also test the template part in Developer Tools -> Template
no changes.
so i dont know whats the problem is
fedot
July 8, 2020, 9:10pm
15
Solution is sometimes more easy than you think
Have set up this sensor myself now. The file_list is a map/array of files. If youāre only expecting one file to be in that directory, you can get away with:
- platform: template
sensors:
file_vm_com_1:
value_template: >-
{% if state_attr('sensor.vm_com', 'file_list')[0] == '/config/vm_com/backup_gestartet_share' %}
share
{% elif state_attr('sensor.vm_com', 'file_list')[0] == '/config/vm_com/backup_beendet_share' %}
musik
{% endif %}
1 Like
i get an error when i just test the config.
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ā)ā) for dictionary value @ data[āsensorsā][āfile_vm_com_testā][āvalue_templateā]. Got ā{% if state_attr(āsensor.vm_comā, āfile_listā)[0] == ā/config/vm_com/backup_gestartet_shareā ) %}\n share\n{% elif state_attr(āsensor.vm_comā, āfile_listā)[0] == ā/config/vm_com/backup_beendet_shareā ) %}\n musik\n{% endif %}ā. (See ?, line ?).
fedot
July 9, 2020, 7:20am
17
Ah, Iām sorry, missed that, I edited my post, try again please
PERFEKT
It is running without the second sensor.
Thanks a lot.