Check for file existence ? sensor template

Continuing the discussion from Custom Images not shown on moon sensor:


i have a sensor with an entity_picture template:

  entity_picture_template: >
    {{ '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png'}}

works beautifully, except for the state of the life360 sensor which hasn’t got a dedicated picture.

I’ve tried to add an existence checker:

  entity_picture_template: >
    {% if '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png' %}
    {{ '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png'}}
    {% else %}
    {% endif %}

but that simply checks alright, even when it doesn’t exist…

Can we check for the existence of an actual file in a template like this?


make a python script that verifies if the file exists. Not sure if python scripts have the os library loaded though.

What about using a file size sensor and cheeking {% if states.sensor.filename.state is defined %}?

That could work, it depends on how the sensor behaves when the file doesn’t exist.

If it’s zero when the file doesn’t exist, that would work too

yes thats what I think I was looking for … wont that work in a regular value_template for this sensor?

something like:

{% set image =  '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png' %}
{% image is defined %}

errors out with: Error rendering template: TemplateSyntaxError: Encountered unknown tag ‘image’…

Probably should be

{% if image is defined %}

What about just {% if image %}

According to the Jinja docs:

The if statement in Jinja is comparable with the Python if statement. In the simplest form, you can use it to test if a variable is defined, not empty and not false

yes, the Jinja is correct then, but the value isnt. It just takes the value to the image construction, no matter if it exists or even is unknown. rendering in m_unknown.png, or m_madeup.png if I enter the state = madeup in the dev-states…

will have to check otherwise, the file sensor as you suggested.

or maybe have a conditional value_template for the state being in the list of possible defined zones.

You are still using {% image is defined %} instead of {% if image is defined %}

Sorry, a typo here, I did test it correctly, both with ‘if image’ and ‘if image is defined’.

the template simply fills in the state no matter what

Well if this didn’t work it’s beyond my limited skills.

  entity_picture_template: >
    {% set image = '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png' %}
    {% if image %}
      {{ image }}
    {% else %}
    {% endif %}

thanks, but that’s the exact code I tested, it simply does not do a file check, it only checks the template and fills in the state.
thanks for your effort!

How many filesize sensors would you have to make to test that instead?

not sure really. I have about 10 zones, and 6 family members… Hope I wouldst have to make a file sensor per combination :wink:

havent checked how to do that yet, but maybe I could start with a non existent zone, to test if it would work at all

This won’t work btw. Because the image is defined in the line prior to it. Is defined checks to see if the object has been created. In the case that both of you are doing, the object is a string, and the string is your path. Therefore when executing, it will always be defined and it will always be true.

You need to implement the sensor @tom_l is referencing in order to use is defined.

Notice how image2 was not defined? That’s how this works.

1 Like

correct, that’s what I tried to report back :wink:
since this sensor can only create states made of Zones I have defined, it shouldn’t be too insecure relying on those states, and add one for when it happens to be out of the zones, ie moving. Thats what I have done now, creating images with overlays for those states. It shows wonderfully . very cool.

btw, id be interested to learn how the file sensor could check for file existence, thought it only would show a file size?

I mean, if the file doesn’t exist, what does the sensor do? I think the sensor wouldn’t be created, which is why @tom_l’s method would work. The sensor wouldn’t be created, so the ‘is defined’ method would work. Basically a poor mans way of checking if the file is there.

I could maybe check for image != image_unknown.png, cause thats what’s being created (defined…)

if that’s what you are trying to find, then yeah that would work.

I was just wanting to see if you got this sorted out, as I am looking to do the same or something similar.
I was wanting to have the picture if it matches the place and if the picture exists, but if there was no such file/picture, then I would want it to default to a certain image.

Any help or code would be appreciated