Template Sensor need help (value_template)

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. :grinning: :sweat_smile:

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

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 ā€¦:frowning:

  - 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') }}"

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)

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 %}

soā€¦ it works now?

nop

:frowning:

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

Solution is sometimes more easy than you think :crazy_face:
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 ?).

Ah, Iā€™m sorry, missed that, I edited my post, try again please

PERFEKT

It is running without the second sensor.
Thanks a lot.
:grinning: