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 )
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 )
Oh my… this is embarrassing…
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 .
The lines in my first post was the whole script .
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.
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!