Template to add values of all sensors with certain name ending

right. sorry. should have phrased that differently.

what I meant was that i cant get these '-'.ljust(30, '-') and state.state.ljust(30, ' ') to behave as expected in the script…

[:-8] was my own doing, and reworked that to format(entity_id[7:-8].replace('_',' ').title()) in the python. showing nicely!

01
now the tabbing/justification … thats a whole challenge in itself

Why? Are you getting errors, and if so, what errors? Or does it just not do what you expect, and if so, what is it doing?

Those are valid Python statements, so I’m not sure why they wouldn’t work for you, even in the limited sandbox environment of python_scripts.

most of the time it just doesn’t do anything.

Try this:

sensor_list = []
total_power = 0
count = 0
for entity_id in hass.states.entity_ids('sensor'):
    if entity_id.endswith('_actueel') and 'sensors' not in entity_id:
        state = hass.states.get(entity_id)
        try:
            power = round(float(state.state), 2)
        except:
            continue
        if power > 0:
            total_power = total_power + power
            count = count + 1
            sensor_list.append('#{:21}: {:>7.2f}'.format(
                state.object_id.replace('_actueel','').replace('_',' ').title(),
                power))

summary = '\n'.join([
    '*{:=^30}'.format(' Sensors '),
    '${:-^20} : {:-^7}'.format(' Sensor ', 'Power '),
    *sensor_list,
    '*{:=^30}'.format(' Summary '),
    '$ Total: # {:<4} Power : {:>7.2f}'.format(count, total_power),
    '*' + '='*30])
    
hass.states.set('sensor.map_total_sensors', '', {
    'custom_ui_state_card': 'state-card-value_only',
    'text': summary})

o wow, thats a big change!

effect is rather small… sorry to say:

43

seems the Summary and sensor {:=^30}'.format works, their nicely centered. The order has changed, no clue yet which it is, not alphabetically, nor on value. But thats no big deal (would be nice though)

the other justifications seem not to stick yet.

Can’t explain why the spaces from the actual list of sensors seem to have disappeared. Could be something the custom UI formatting is doing. I tried this code in a Python shell and it worked fine. Here’s my example output:

>>> summary = '\n'.join([
...     '*{:=^30}'.format(' Sensors '),
...     '${:-^20} : {:-^7}'.format(' Sensor ', 'Power '),
...     *sensor_list,
...     '*{:=^30}'.format(' Summary '),
...     '$ Total: # {:<4} Power : {:>7.2f}'.format(count, total_power),
...     '*' + '='*30])
>>> print(summary)
*========== Sensors ===========
$------ Sensor ------ : Power -
#Abc 123              :    1.20
#Hey There            :  123.45
*========== Summary ===========
$ Total: # 2    Power : 1234.56
*==============================

Maybe check your HA log to see the actual state change to sensor.map_total_sensors to see if the spaces are there or not.

Not sure if the python_scripts environment includes the sorted function, but if it does you can change:

    *sensor_list,

to:

    *sorted(sensor_list),

yes, thats exactly what i am looking for!

how do you do that? try it in a Python shell?

it does!

Are you able to get to a shell (aka command line) on your HA system? If so, just execute the command python3. If not, then use any computer that has python installed, or install it. If you need help with the details I’d suggest Googling. That’s a bit beyond the scope of this forum. :wink:

not sure…duh.my Ha system is Hassio or Hassos, so no other software on that, maybe an @Frenck add-on? Will check .
Up to now I always edit in the Python script itself, as it updates immediately. Which is one of the bigger advantages using Python :+1:

making slight progress:

changed the ---- into ==== which makes a lot of difference in the spacing. took out the first line '*{:=^30}'.format(' Sensors '), which seems a bit superfluous.

your new way of rounding had unexpected effects, the values seemed unstable, and often changed into many decimals after the comma. changed into this now makes it stable as a rock:

    try:
        power = float(state.state)
    except:
        continue
    if power > 0:
        total_power = round(total_power + power,2)
        count = count + 1

and makes my other sensor:

hass.states.set('sensor.total_sensors',total_power, {
        'unit_of_measurement': 'Watt',
        'friendly_name': '{} sensors'.format(count),
        'count': count
        })

stable as well.

also found an easier way for the sensor names, no title() necessary anymore:

        sensor_list.append('#{:21}: {:>7.2f}'.format(
            state.name.replace('actueel','').replace('_',' '),
            power))

thing I noticed is that even states without a decimal now show .00 dont think I had that before, but I can appreciate this being necessary for the alignment?

Still havent found out why the sensor lines and the Total line won’t align with the '*{:=^20} : {:=^7}'.format(' Sensor ', 'Power ') line. No matter what i fill in the {}, the positions remains the same…

some further colorization implemented:

        colorCode = '%' if power < 20 else \
                    '&' if power < 100 else \
                    '+' if power < 300 else \
                    '#' if power < 500 else \
                    '=' if power < 1500 else \
                    '!' if power < 2000 else \
                    ''
        colorCodeTotal = '%' if total_power < 200 else \
                         '&' if total_power < 500 else \
                         '+' if total_power < 1000 else \
                         '#' if total_power < 1500 else \
                         '=' if total_power < 2000 else \
                         '!' if total_power < 3000 else \
                         '*'

and

        sensor = colorCode +'{:22}:'.format(state.name[:-8]) + '{:>7}\n'.format(state.state)
        sensor_list.append(sensor)

had a little go understanding your new summary style, so forgive me for changing. This is still very much so a learning process…
I kind of like the formatting placeholders in the summary, and the content or variable in the .format().

summary = '\n'.join([
          '*{:=^20} : {:=^7}',                 # = 30
          *sorted(sensor_list),
          '*{:=^30}',                            # = 30
          '/ {:^21}: {:^7}',                    # = 30
          colorCodeTotal +'{:^21}: {:>7.2f}',  # = 30
          '*' + '='*30                          # = 30
          ]).format(' Sensor ','Power ',
                    ' Summary ',
                    'Total Sensors consuming power',count,
                    'Total consumed Power',total_power,
                    total_power)

Colors are working fine now, so cool. Only thing is, the sorted(sensor_list) now sorts per color :wink: unexpected (for me) and have to see if that can be changed to sort alphabetically.

56
optimally I would like to create the list colorCode =['*','!','+','=','%','$','#'] and have the comparison check for values power <50, 100, 300, 500, 1500, 2000, or else (when >2000) turn ‘*’.

Could(Should) I do that in another way than fully written out like now?

btw, what does the * do in front of the *sorted(sensor_list) ?

thought it to be a remnant of my colorizations, so took it out, but then the script stops and errors with: TypeError: sequence item 1: expected str instance, list found.
Could there be a mixup in the code with the * being used for several things?

Most of your recent questions have to do with how to do things in Python. You really should take some time and find a nice online Python tutorial. You’d be able to figure a lot of this out yourself instead of all this trial and error.

*iterable means expand the iterable. So *['a','b','c'] is 'a', 'b', 'c'. I’m using it here to expand sensor_list into individual items that then become part of the bigger list.

You can give the function sorted a key function that can change what it sorts. So you could change *sorted(sensor_list) to *sorted(sensor_list, key=lambda x: x[1:]). So instead of sorting by the whole strings, it will sort using the strings minus their first characters. Again, this depends on whether or not the sandboxed python_scripts environment allows the use of the lambda keyword.

1 Like

why thank you indeed, that does it. This has become a nice little script, glad to share the final (for now) version… really appreciated.

##########################################################################################
# map_total_sensors.py
# reading all sensors.*_actueel using power (>0), listing and counting them,
# sorted alphabetically, and calculate summed power consumption
# colorcoded by power usage
# by @Mariusthvdb  and big hand Phil, @pnbruckner
# https://community.home-assistant.io/t/template-to-add-values-of-all-sensors-with-certain-name-ending/64488
# august 21 2018
##########################################################################################

sensor_list = []
total_power = 0
count = 0
for entity_id in hass.states.entity_ids('sensor'):
    if entity_id.endswith('_actueel') and not 'sensors' in entity_id:
        state = hass.states.get(entity_id)
        try:
            power = float(state.state)
        except:
            continue
        if power > 0:
            total_power = round(total_power + power,2)
            count = count + 1

            colorCode = '%' if power < 20 else \
                        '$' if power < 100 else \
                        '+' if power < 300 else \
                        '#' if power < 500 else \
                        '=' if power < 1500 else \
                        '!' if power < 2000 else \
                        ''
            colorCodeTotal = '%' if total_power < 200 else \
                             '$' if total_power < 500 else \
                             '+' if total_power < 1000 else \
                             '#' if total_power < 1500 else \
                             '=' if total_power < 2000 else \
                             '!' if total_power < 3000 else \
                             '*'

            sensor = colorCode +'{:22}:'.format(state.name[:-8]) + \
                                '{:>7}\n'.format(state.state)
            sensor_list.append(sensor)

summary = '\n'.join([
          '*{:=^20} : {:=^7}',
          *sorted(sensor_list,key=lambda x: x[1:]),
          '*{:=^30}',
          '/ {:^21}: {:^7}',
          colorCodeTotal +'{:^21}: {:>7.2f}',
          '*' + '='*30
          ]).format(' Sensor ','Power ',
                    ' Summary ',
                    'Total Sensors consuming power',count,
                    'Total consumed Power',total_power,
                    total_power)

hass.states.set('sensor.map_total_sensors', '', {
        'custom_ui_state_card': 'state-card-value_only',
        'text': summary
        })

hass.states.set('sensor.total_sensors',total_power, {
        'unit_of_measurement': 'Watt',
        'friendly_name': '{} sensors'.format(count),
        'count': count
        })

##########################################################################################
# map_total_sensors.py
##########################################################################################
##########################################################################################
# Codes for text_colors declared in 
# Custom card: /custom_ui/state-card-value_only.html
##########################################################################################
#      case "*": return "bold";
#      case "/": return "italic";
#      case "!": return "red";
#      case "+": return "green";
#      case "=": return "yellow";
#      case "%": return "grey";
#      case "$": return "brown";
#      case "#": return "blue";
#      default:  return "normal";
##########################################################################################

##########################################################################################
# lessons learned:
##########################################################################################
#Try. The else doesn’t go with the if, it goes with the try. Notice that it is at the same
#indentation level as try and except. In a try statement, if an exception occurs
#the except clause is executed.
#But if no exception occurs, then the else clause is executed (if there is one.)
#So, in this case, if the conversion to int works, then the else clause it executed,
#which increments the count.

#*iterable means expand the iterable. So *['a','b','c'] is 'a', 'b', 'c'. Used here to 
#expand sensor_list into individual items that then become part of the bigger list.

#sorted. You can give the function sorted a key function that can change what it sorts. 
#So you could change *sorted(sensor_list) to *sorted(sensor_list, key=lambda x: x[1:]). 
#Instead of sorting by the whole strings, it will sort using the strings minus their 
#first characters.
##########################################################################################
# Older snippets
##########################################################################################
#        logger.info('entity_id = {}, state = {}'.format(entity_id, state.state))
#        sensor_id = '{}'.format(entity_id[7:-8].replace('_',' ').title())
#        sensor_power = '\t{}'.format(state.state)
#        sensor = '#{} :'.format(sensor_id) + ' {}\n'.format(sensor_power)


#            sensor_list.append(sensor)
##            sensor_list.append('%{:21}:{:>7.2f}'.format(
#                state.name.replace('actueel','').replace('_',' '),
#                power))

#            sensor_list.append(colorCode +'{:21}: {:>7.2f}'.format(
#               state.name.replace('actueel','').replace('_',' '),
#                power))

#sensor_map = '\n'.join(sensor_list)

#summary = '*{:=^30}\n' \
#          '$----------  Sensor : Power  ----------\n' \
#          '{}\n' \
#          '*=============== Sumary ==============\n' \
#          ' Sensors consuming power: {}\n' \
#          ' Total consumed Power :  {}\n' \
#          '*====================================\n' \
#          .format(' Sensors ',
#                  sensor_map,
#                  count,
#                  total_power)
##########################################################################################
# map_total_sensors.py
##########################################################################################

@Mariusthvdb I’ll echo what pnbruckner said. You should look at a python tutorial. You’ve got a good brain for figuring this stuff out


thanks, i guess;-) ive had @petro suggest likewise…
the thing is, I started with HA to make life easier…
then found some very nice people that help me doing things I never imagined doing before, and now endup in need of extra tutoring :wink:

Believe me, i want to, and try to grab each opportunity learning things, as methodically as i think it should be done. Not very easy though with limited time and other chores. No such thing as diving in and learning on the job

Addictive it is though, so i’m in for another little hour community browsing now, trying to help others, and seek some assistance myself…

btw @nigell did you have the opportunity to see why the device_trackers aren’t shown in the orphans script?

No I haven’t looked at that, send me a PM to remind me, and I will try to look at this weekend

back for a small thing…

using this automation value_template for another sensor, showing me the last run automations. Skipping the really frequent ones, because without that, it halts the system… (activate_map_sensors_actueel is one of the culprits, as I feared…):

value_template: >
  {% set skip_list = ['automation_ran', 'count_warnings', 'count_errors',
                       'activate_map_sensors_actueel'] %}
  {{ trigger.event.data.entity_id.startswith('automation.') and
     trigger.event.data.entity_id.split('.')[1] not in skip_list and
     'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}

which works fine.

was trying to shorten the template some further doing this:

value_template: >
  {% set skip_list = ['automation_ran', 'count_warnings', 'count_errors',
                       'activate_map_sensors_actueel'] %}
  {{ trigger.event.data.entity_id.startswith('automation.') and
     trigger.event.data.entity_id.split('.')[1] not in skip_list and
     ['old_state','new_state'] in trigger.event.data }}

but this errors out stating it can run the list (not exactly but changed it back again so cant reproduce now)

would there be another way of compressing this:

and
{{ 'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}

thx

There’s no way to compress that because you are looking for individual items inside the keys of trigger.event.data. When you do someting like [‘x’,‘y’] in somelist, it will look for the entire object [‘x’,‘y’], not both objects individually.

Ok cool. Thanks .