Math/code problem: Where is the vacuum located?

You can’t calculate a percentage if you don’t have the information to calculate the percentage.

I.e. you need area cleaned so far, and total area for each room. Do you have that information?

Do you just have area cleaned and total area for the whole house?

Oh my… I do have the area cleaned so far, I said it a number of times above

but it’s the total cleaned area of the n number of rooms, it doesn’t tell the difference between rooms.

this value updates every 10 seconds

Ok, can you please show me that information on the entity itself. Also, what’s the area’s of each room?

area cleaned so far and the and the area of the whole house and the area of each room too…

Right, I feel like we are going in circles. This is the needed information:

  1. The sensor or attribute that outputs the total area.
  2. The sensor or attribute that outputs the total cleaned so far.
  3. Each rooms area.
  4. The sensor or attribute that tells you what rooms are active.

Without all 4 of those pieces of information, this is most likely dead in the water.

That’s exactly the info I mentioned I’m counting with.

From that info I want to create a sensor for each room with the current cleaned %.

then be forthcoming with the information and provide it. That’s what I’ve been asking for. Actual entity_id’s and attributes for the actual template.

I don’t have some of those entities created as of now but let’s make an example:

  • total area: 30sqm
  • total cleaned area so far: sensor.total_cleaned_so_far
  • Each room’s area: A : 15, B : 10 , C : 5
  • which rooms are active: input_boolean.zone_a, input_boolean.zone_b, input_boolean.zone_c

15 + 10 + 5 doesn’t equal 45. Do you not have this information?

I edited the post… reload your browser… 30 total for this example

and I do know as well the order, It will always clean A, B, C in order regardless of the rooms chosen

just about to ask this, ok

thanks in advanced mate

it’s quite complicated

Ok, this is… lengthy to say the least. You need 1 sensor for the calculation. n sensors for each room.

template:
- sensor:
  - name: Vacuum Calculator
    state: OK
    attributes:
      rooms: >
        {% set rooms = [ 
        {
            'name': 'A',
            'cleaning': states('input_boolean.zone_a') | bool(False),
            'area': 15
        },{
            'name': 'B',
            'cleaning': states('input_boolean.zone_b') | bool(False),
            'area': 10
        },{
            'name': 'C',
            'cleaning': states('input_boolean.zone_c') | bool(False),
            'area': 5
        }
        ] %}
        {% set total = 30 %}
        {% set total_cleaned = states('sensor.total_cleaned_so_far') | float(0) %}
        {% set ns = namespace(rooms=[], sum=0, output=[]) %}
        {% for room in rooms %}
          {% if room.cleaning %}
            {% set ns.sum = ns.sum + room.area %}
            {% set ns.rooms = ns.rooms + [ dict(position=ns.sum, name=room.name, area=room.area) ] %}
          {% else %}
            {% set ns.output = ns.output + [ (room.name, none) ] %}
          {% endif %}
        {% endfor %}
        {% set cleaned = ns.rooms | selectattr('position', '<=', total_cleaned) | list %}
        {% set cleaning = ns.rooms | selectattr('position', '>', total_cleaned) | list | first | default(none) %}
        
        {% for room in cleaned %}
          {% set ns.output = ns.output + [ (room.name, 100) ] %}
        {% endfor %}
        {% if cleaning is not none %}
          {% set previous = cleaned | map(attribute='area') | sum %}
          {% set ns.output = ns.output + [ (cleaning.name, ((total_cleaned - previous) / cleaning.area * 100) | round) ] %}
          {% set not_cleaned = ns.rooms | selectattr('position', '>', cleaning.position) | list %}
          {% for room in not_cleaned %}
            {% set ns.output = ns.output + [ (room.name, 0) ] %}
          {% endfor %}
        {% endif %}
        {{ dict(ns.output | sort(attribute='0')) }}

  - name: Vacuum Room A Progress
    state: >
      {{ state_attr('sensor.vacuum_calculator', 'rooms')['A'] }}
    unit_of_measurement: %

  - name: Vacuum Room B Progress
    state: >
      {{ state_attr('sensor.vacuum_calculator', 'rooms')['B'] }}
    unit_of_measurement: %

  - name: Vacuum Room C Progress
    state: >
      {{ state_attr('sensor.vacuum_calculator', 'rooms')['C'] }}
    unit_of_measurement: %

Sensors that are not cleaning will be unavailable.

1 Like

I’ll have a look and come back with some questions I’m afraid. Thanks mate.

That’s why I wanted your actual entities, so it could just be copy/paste.

nah that’s not a problem, I can fill it properly. But I’d like to actually understand it instead of just copy paste… For example, what’s this?

namespace(rooms=[], sum=0, output=[])

and room in rooms? there’s no room in rooms as far as I understand.

{% for room in rooms %}