Easy way to choose between either .png or .gif in python?

HI,

had this sensor in python:

hass.states.set('sensor.activity_badge', state.state, {
        'friendly_name': time,
        'entity_picture': '/local/activities/{' '}.png'.format(state.state.replace(' ','').lower()),
        'unit_of_measurement': 'Act'
        })

since I changed one of the pictures to an animated gif, I changed this into:

entityPicture = '/local/activities/activity.gif' if state.state == 'Activity' else 
                '/local/activities/{' '}.png'.format(state.state.replace(' ','').lower())
hass.states.set('sensor.activity_badge', state.state, {
        'friendly_name': time,
        'entity_picture': entityPicture, #'/local/activities/{' '}.png'.format(state.state.replace(' ','').lower()),
        'unit_of_measurement': 'Act'
        })

this works fine, but I really never like hard coded variables. Couldn’t something like

'/local/activities/{' '}.png/gif'.format(state.state.replace(' ','').lower()),
be possible? Of course the above is not correct, but it shows what I am looking for.

Or a go between like:

'/local/activities/{' '}.png'.format(state.state.replace(' ','').lower()) or '/local/activities/{' '}.gif'.format(state.state.replace(' ','').lower()),

Anyways, hope you can have a look

in fact I am looking for a way to say this in Python:

ext = 'png' or 'gif'
@petro would this be allowed?

followed by this:

'entity_picture': '/local/activities/{' '}.{}'.format(state.state.replace(' ','').lower(),ext),

the .png files are found, the gif isn’t…

don’t think the try: and except: is correct here?

tried this:

try: 
     ext = 'png'
except: 
     ext = 'gif'

and again, the png files are displayed, the gif isn’t…

You gotta make both strings. The use os.path.exists(path_to_file).

This requires the os module

import os

if os.path.exists(path_to_file):

You can’t just make the string and expect it to work. Gotta see if the file exists.

understand,
fear the python environment never allows these imports:

Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "summary.py", line 9, in <module>
ImportError: __import__ not found

have adapted the above to only check for the ext, and leave the name of the picture in the final statement:

ext = 'gif' if state.state == 'Activity1' else 'png'

with this for the final statement:

        'entity_picture': '/local/activities/{' '}.{}'.format(state.state.replace(' ','').lower(),ext)

will work too but it’s based on the activity, not based on weather the file exists or not.

yes I see that, and might be easiest at the moment.

let me try to ask in a different way. say I have

activity1,
activity2,
activity3,
activity4,
activity5

and want to show the accompanying pictures

activity1.gif,
activity2.png,
activity3.png
activity4.gif,
activity5.png

What would be the smartest way of selecting these images without hardcoding as I do now?

ext = 'gif' if state.state == 'Activity1' or 'Activity4' else 'png'

'entity_picture': '/local/activities/{' '}.{}'.format(state.state.replace(' ','').lower(),ext)
ext = 'gif' if state.state in ['Activity1', 'Activity4'] else 'png'

a yes, I did try that indeed, is that the preferred way above my ‘or’ ?

btw just for the fun of it, I tried a little hack, and called my animated gif a .png file. What do you know? It shows just fine :wink:

no need for fancy python, just fool the system with wrong extensions…

or in this situation doesn’t work the way you think it does.

Your if statement would always result in ‘gif’ being selected because your or statement is being analyzed like this:

(state.state == 'activity') or ('activity4')

'activity4' is always True because a string only resolves false when it’s empty. And your string is hardcoded as 'activity4', which is not empty. So You essentially have this as an if statement:

(state.state == 'activity1') or True
1 Like

Of course, hadn’t realized that. Makes sense, and looks more logical too, thank you for explaining so clearly.