Error in Python Script: 'dict' object has no attribute 'lower'

Hi,

I have a python script which I just don’t get running.
The script should:

  • be triggered once a day
  • loop through entities inside a group
  • compare an attribute of each entity (‘battery’ of a Homematic thermostat) against a threshold
  • drop a notification in case of exceeding the threshold

I think I’ve got most things working except the compare part. I read each battery voltage correctly (for example ‘2.6’).
But I in my if-condition I get the error
‘dict’ object has no attribute ‘lower’

This is my script:

threshold = 2.4
for entity_id in hass.states.get('group.thermostates').attributes['entity_id']:
    logger.warning('Thermostat: {}'.format(entity_id))
    logger.warning('Batterie: {}'.format(hass.states.get(entity_id).attributes['battery']))
    battery = float('{0:.1f}'.format(hass.states.get(entity_id).attributes['battery']))
    if  float(battery) > float(threshold):
        hass.services.call('persistent_notification.create', { 'message' : "Batterie vom " + entity_id + "-Thermostat fast leer." }, False )

This is the log:

2021-10-19 06:00:00 WARNING (SyncWorker_5) [homeassistant.components.python_script.thermostate_battery_empty.py] Thermostat: climate.living_room
2021-10-19 06:00:00 WARNING (SyncWorker_5) [homeassistant.components.python_script.thermostate_battery_empty.py] Batterie: 2.6
2021-10-19 06:00:00 ERROR (SyncWorker_5) [homeassistant.components.python_script.thermostate_battery_empty.py] Error executing script: 'dict' object has no attribute 'lower'

Could anyone please help me with this?

Thanks a lot!,
greetings,
Jojo

Can you show the whole script? I don’t see lower anywhere in your code.

Is there a specific reason why you use a python script instead of creating an automation in Home Assistant?

Check out the docs again for hass.services.call. There’s an example in Python Scripts - Home Assistant.

Basically, you have the wrong arugments.

you have:

hass.services.call('persistent_notification.create', { 'message' : "Batterie vom " + entity_id + "-Thermostat fast leer." }, False )

which is only 3 arguments. Your service data is being sent to the 2nd argument, which should be a string, but it’s a dict. So… when string operations are performed on it, it fails.

hass.services.call('persistent_notification', 'create', { 'message' : "Batterie vom " + entity_id + "-Thermostat fast leer." }, False )
2 Likes

Oh my… this is embarrassing… :man_facepalming:
all the time I thought that the line with the if-condition would be the problem. I tried an unknown number of things like casts, formatting things, using additional variables…

I never thought that the notification line could cause the issue. So your reference to the docs would be totally justified if I would have thought about to search the problem there :sweat_smile:.

Thanks a ton, this is finally solved!

The lines in my first post was the whole script :slight_smile: .
I solved this by a script which is called regularly by an automation. I am not very good in python but even worse in writing templates. Thats why I solved this in a script.

The whole script now is just:

for entity_id in hass.states.get('group.thermostates').attributes['entity_id']:
    if hass.states.get(entity_id).attributes['battery'] < 2.4:
        hass.services.call('persistent_notification', 'create', { 'message' : "Batterie vom " + entity_id + "-Thermostat fast leer." }, False )

This just loops through my group of thermostats, checks the battery level of each entity and drops a notification in case of a battery level lower than 2,4 V.

fyi, i’m not sure why you’re even using a python script. This is a simple service call with a jinja script that does basically the same thing.

- service: persistent_notification.create
  data:
    message: >
      Batterie vom {{ expand('group.thermostates') | selectattr('attributes.battery', '<', 2.4) | map(attribute='entity_id') | list | join(', ') }} -Thermostat fast leer.
1 Like

I started my python script attempts several years ago when I started with HA. Those days python scripts were just “the way to go” for a little bit more tricky stuff.

No doubt that today there might be much better (or more pretty) ways! Thank you for your suggestion!