List, select/reject attributes, mathematical operations

Hi all, thanks in advance!

Im trying to get a list of entitles, their state, and selected attributes or reject specific ones. While also doing mathematical evaluations as needed.

Sensor.baby_feeding_1 [state] 2 (oz) [attributes] time (00:30) , duration (00:30:00)
Sensor.baby_feeding_2 [state] 1.2 (oz) [attributes] time (03:30) , duration (00:42:00)
Sensor.baby_feeding_3 [state] 1.5 (oz) [attributes] time (06:30) , duration (00:35:00)
Many more entries but you get the idea

1st scenario) In the above Iā€™d like to list feeding times between 03:30 and 06:30 without the duration attribute
2nd scenario) In the above Iā€™d like to sum the total oz , as well as duration (total)
3rd scenario) In the above Iā€™d like to count the total feedings for the day

I KNOW thats quite a bit, but, figured it would be better to ask all at once than separate posts.

Again, I really appreciate the assist!

Letā€™s start with the basics. Find the name of the integration responsible for generating those sensors and use it in the following template. For example, if itā€™s ā€œbaby_managerā€ then enter the following into the Template Editor.

{{ integration_entities('baby_manager') }}

The result will be a list of all entities produced by that integration. Let me know when you have it working and then we can proceed with building upon it to produce the 3 things you want.

Roger that -


  "var.x",
  "var.y",
  "var.z",
  "var.toggle_count",
  "var.diaper_count",
  "var.diaper_1",
  "var.diaper_2",
  "var.diaper_3",
  "var.diaper_4",
  "var.diaper_5",
  "var.diaper_6",
  "var.diaper_7",
  "var.diaper_8",
  "var.diaper_9",
  "var.diaper_10",
  "var.feeding_count",
  "var.feeding_1",
  "var.feeding_2",
  "var.feeding_3",
  "var.feeding_4",
  "var.feeding_5",
  "var.feeding_6",
  "var.feeding_7",
  "var.feeding_8",
  "var.feeding_9",
  "var.feeding_10",
  "var.pumping_count",
  "var.pumping_1",
  "var.pumping_2",
  "var.pumping_3",
  "var.pumping_4",
  "var.pumping_5",
  "var.pumping_6",
  "var.pumping_7",
  "var.pumping_8",
  "var.pumping_9",
  "var.pumping_10"
]
  1. What is the integrationā€™s name? Its full name (and, ideally, the link to its Github repo because this appears to be a custom integration) and the name you used in integration_entities().

  2. According to the list of entity_ids you posted, there are no sensor entities like what you described in your first post. Whatā€™s the reason for this discrepancy?

First and foremost, most likely reason? Newborn dad brain - running on fumes and trying to use my mind to stay awake at night lol

The integration is ā€˜home-assistant-variablesā€™ and the link is GitHub - snarky-snark/home-assistant-variables: A custom Home Assistant component for declaring and setting generic variable entities dynamically. ā€¦ Iā€™m using each of these to store variable data (and templating my own attributes) from the GitHub - babybuddy/babybuddy: A buddy to help caregivers track sleep, feedings, diaper changes, tummy time and more to learn about and predict baby's needs without (as much) guess work. integration, as this only retrieves last state.

Primarily, the reason for the difference in the entities and what I posted was simply high level because I want to learn how to write them based off scenarios (clearly not helpful) and ultimately not just have a posted question then given the answer and learn nothing from it.

Hereā€™s what I was using to return the above

 {{ integration_entities('var') }}

Thanks again for the engagement

Hereā€™s the basic framework. Start with a list of entity_ids for all entities in the var integration, select the entity_ids containing the string feeding, get the state value of each one (it will be a string value), convert string to float (floating point number), and finally compute the sum. The result represents the total quantity of liquid for all feedings. Let me know if it works correctly or fails.

{{ integration_entities('var') 
  | select('search', 'feeding')
  | map('states')
  | map('float', 0)
  | sum
}}

The time and duration attributes probably contain string values as well. Letā€™s confirm that assumption. Let me know whatā€™s reported by this template:

{{ state_attr('var.feeding_1', 'time') }}
{{ state_attr('var.feeding_1', 'duration') }}

{{ state_attr('var.feeding_1', 'time') is string }}
{{ state_attr('var.feeding_1', 'duration') is string }}

Assuming time and duration contain string values and time is always in HH:MM format, try this template. It should report the total duration of all feedings whose times are between 03:30 and 06:30.

{{ integration_entities('var') 
  | select('search', 'feeding')
  | expand
  | selectattr('attributes.time', 'ge', '03:30')
  | selectattr('attributes.time', 'le', '06:30')
  | map(attribute='attributes.duration')
  | map('as_timedelta')
  | sum(start=timedelta(0))
}}

The first two lines are identical to the previous example.

It expands each feeding entity_id into its State Object (which contains lots of information about the entity).

It selects time values greater than or equal to 03:30. This is actually a string comparison, not an arithmetic comparison. However, owing to the way time values are numbers in a rigid format, a string comparison will work as desired.

It selects time values less than or equal to 06:30.

It extracts the duration values, converts them into timedelta objects and finally computes the sum.

Thanks @123 thatā€™s a huge help!!!

1 Like