How to "count" how many power plug are on? HASS strategy on lowering power needs

Since you have an energy meter, would it not be more accurate to just adjust the on devices when energy consumption hits a certain level. You need to decide the logic for what gets turned off and for how long etc.

Sure, but I already know that with 2 A/C is ok. With 3+ problems.

The logic I wrote in post, 2 ON together and every 15-20 minutes (I have to decide which the best interval) change the 2 that are ON

well if you really want an easy and fail save way how to count whatever you want, here’s a short Python_script, I’ve just edited for you to count family members in my group.family.

if you want, you can leave out all the other counters, otoh, you might find them useful, for counting your ac’s…:wink: just fill in the correct group, and adapt the wording in the script.

you throw this script in the folder /config/python_scripts and point your configuration to use python simply by adding:

python_script:

heres the script whats_on, creating an input_boolean (originally was called anything on, and if anything on the boolean switched on, of not switches off)

38

save this as whats_on.py:

familyEntities = 'group.family'
switchEntities = 'group.switches'
lightEntities = 'group.all_lights'
applianceEntities = 'group.appliances'

filteredLightEntities = [ entity_id for entity_id in lightEntities if ' ' not in entity_id ]

def CountMyEntities(hass, entity_list, entity_state):
    cnt = 0

#  using a full list:  for entity_id in entity_list:
#  using groups in a [list] :  for group_entity in entity_list:
#                                 for entity_id in hass.states.get(group_entity).attributes['entity_id']:
    for entity_id in hass.states.get(entity_list).attributes['entity_id']:
        state = hass.states.get(entity_id)
        if state.state == entity_state:
            cnt = cnt + 1
    return cnt
familyHome = CountMyEntities(hass, familyEntities, 'home')
switchesOn = CountMyEntities(hass, switchEntities, 'on')
appliancesOn = CountMyEntities(hass, applianceEntities, 'on')
lightsOn = CountMyEntities(hass, lightEntities, 'on') #if needed, use filteredLightEntities

totalOn = switchesOn + lightsOn + appliancesOn
whichIcon = "mdi:lightbulb-on-outline" if totalOn else "mdi:lightbulb-outline" # will evaluate "off" if totalOn == 0.
status = 'on' if totalOn else 'off' # will evaluate "off" if totalOn == 0.

hass.states.set('input_boolean.whats_on', status, { 
    'friendly_name': "What's On?",
    'icon': whichIcon,
    'family_home': familyHome,
    'lights_on': lightsOn,
    'switches_on': switchesOn,
    'appliances_on': appliancesOn,
    'total_on': totalOn,
    'extra_data_template':'{total_on} on'
})

You then can play with the attributes of this input_boolean in your automations, or even template_sensors.

45

{% if states.input_boolean.whats_on.attributes.family_home <= 3 %} scenario 1
{% else %} scenario 2
{% endif %}

if you want a yaml template, and dont want to use Python, use this:

  {{ states|selectattr('entity_id','in',state_attr('group.family','entity_id'))
           |selectattr('state','eq','home')|list|count +
     (1 if is_state('input_boolean.guests','on') else 0) }}

or spit out the names of people home:

value_template: >
  {% if is_state('group.family', 'home') %}
    {{ dict((states|selectattr('entity_id', 'in', state_attr('group.family', 'entity_id'))
    |list)|groupby('state'))['home']|map(attribute='name')|list|join(', ') }}
  {%else%}
    Nobody home
  {%endif%}

have fun!

1 Like

Many thanks, will have a look at it, very interesting although a bit complicated for me, so I will use it in second phase

Right now I wish to start with the automation / script first (without counting, for night use)