Jinja template: all same state

Hi everybody,

I would like to create a template sensor that is only True if the state of all entities is False.

Unfortunately, the source sensors are sensor entities, not binary_sensor entities. This means, that I cannot use group for this. It will not report the state as expected.

I can never get jinja right… Would somebody please help me finish the sensor?

{% set my_entities = [
  'sensor.00_currently_backing_up',
  'sensor.01_currently_backing_up',
  'sensor.02_currently_backing_up',
  'sensor.03_currently_backing_up',
  'sensor.04_currently_backing_up',
  'sensor.05_currently_backing_up'
  ] %}
{% set my_state=False %}

# what goes here?

I need something like (pseudo-code) if every entity in entity == my_state, then return true; else return false.

So if all these currently_backing_up sensors’ state is False, then I’d like my template sensor to be True or On.

In any other case (either one/some of them is/are not False, or perhaps one/some of them is/are unknown or anything else but False, ) my template sensor needs to be False or Off as well).

Thank you in advance for your help :slight_smile:

{% set my_entities = [
  'sensor.00_currently_backing_up',
  'sensor.01_currently_backing_up',
  'sensor.02_currently_backing_up',
  'sensor.03_currently_backing_up',
  'sensor.04_currently_backing_up',
  'sensor.05_currently_backing_up'
  ] %}
{{ true not in my_entities | map('states') | map('bool', true) | list }}
2 Likes

Yes! Thank you.

1 Like

Can I add one more thing to this so I can improve my automation?

{% set my_entities = [
  'sensor.00_next_scheduled_backup',
  'sensor.01_next_scheduled_backup',
  'sensor.02_next_scheduled_backup',
  'sensor.03_next_scheduled_backup',
  'sensor.04_next_scheduled_backup',
  'sensor.05_next_scheduled_backup'
  ] %}

All these will return a different time value, for example 2024-05-05T11:30:00+00:00.

Is it possible to give me the closest time? Let’s say I get these values from those sensors:

2024-05-05T12:30:00+00:00
2024-05-05T11:30:00+00:00
2024-05-05T12:45:00+00:00
2024-05-05T14:30:00+00:00
2024-05-05T15:30:00+00:00
2024-05-05T10:30:00+00:00

If we were to sort these from earliest to latest, they’d look like this

2024-05-05T10:30:00+00:00
2024-05-05T11:30:00+00:00
2024-05-05T12:30:00+00:00
2024-05-05T12:45:00+00:00
2024-05-05T14:30:00+00:00
2024-05-05T15:30:00+00:00

Which means, I would like to my template to be 2024-05-05T10:30:00+00:00.

My current automation is triggered by input_datetime helpers, but I would rather use these sensor readings, then slap a timedelta on them (so that the device can WOL a couple minutes before the backups are actually scheduled) – so if I were to change the backup times (which happens on my synology NAS), the automation would adapt to them automatically.

{{ my_entities|sort|first }}

Correctly-ordered date strings (like these ISO8601 strings) can be sorted alphabetically and yield correct chronological results.

EDIT: this only works on a list of states rather than entities — see posts below.

Thank you, but this will only return the name (i.e. sensor.04_next_scheduled_backup).

If I switch it to {{ states(my_entities)|sort|first }}, I get TypeError: unhashable type: 'list'.

Of course, sorry. Too early in the morning.

{{ my_entities|map('states')|sort|first }}

You can’t run the states() function on a list. The map filter allows you to run another filter (here, states as a filter) on each item in turn.

2 Likes

That’s it, thank you :slight_smile: