Individually set date time for all entities in a group

Hi,

I’m still new to Home Assistant and this is my first post, so happy to take any feedback.

I have a group of datetime entities. Currently the group has 2 entities (with more to be added in future) and is defined in groups.yaml as follows:

next_feed_beaf:
  name: 'Next Feed Time beaf'
  entities:
    - input_datetime.next_feed_beaf1
    - input_datetime.next_feed_beaf2

I need a script that will look at evaluate each entity in the group, then change the state based on its current value. If the current value is in the past or less than 5 minutes into the future, the entity value should be set to 5 minutes from present. Otherwise, no change should be made.

I managed to update the value for a single entity with the following, but don’t know how to apply this to a group of entities.

- service: input_datetime.set_datetime
    entity_id: input_datetime.next_feed_beaf1
    data_template:
      timestamp: >
        {% set delta = as_timestamp(states('input_datetime.next_feed_beaf1')) - as_timestamp(now()) %}
        {% if delta >= 0 and delta <= 300 %}
          {{ as_timestamp(now()) + 300 }}
        {% elif delta < 0 %}
          {{ as_timestamp(now()) + 300 }}
        {% else %}
          {{ as_timestamp(states('input_datetime.next_feed_beaf1')) }}
        {% endif %}

Thanks in advance for any help.

Pretty sure this is a job for namespace and expanding groups, which I am not very good at. So I will let someone else help with that. However your template can be simplified to this:

- service: input_datetime.set_datetime
    entity_id: input_datetime.next_feed_beaf1
    data:
      timestamp: >
        {% set delta = as_timestamp(states('input_datetime.next_feed_beaf1')) - as_timestamp(now()) %}
        {% if delta <= 300 %}
          {{ as_timestamp(now()) + 300 }}
        {% else %}
          {{ as_timestamp(states('input_datetime.next_feed_beaf1')) }}
        {% endif %}

as less than 0 is less than 300.

Also, the use of data_template was depreciated in v0.115

EDIT: here’s an example of iteration over an expanded group:

@tom_l thanks for the reply and those recommendations. I think expand might do the job. I’ve been playing around with it a bit in conjunction with repeat. I’ve gotten it as far as:

- repeat:
      count: "{{ expand('group.next_feed_beaf') | length }}"
      sequence: 
        - service: input_datetime.set_datetime
          entity_id: "{{ expand('group.next_feed_beaf')[repeat.index].entity_id }}"
          data:
            timestamp: >
              {% set delta = expand('group.next_feed_beaf')[repeat.index].attributes.timestamp - as_timestamp(now()) %}
              {% if delta <= 300 %}
                {{ as_timestamp(now()) + 300 }}
              {% else %}
                {{ expand('group.next_feed_beaf')[repeat.index].attributes.timestamp }}
              {% endif %}

However, I get the following error:

Invalid config for [script]: not a valid value for dictionary value @ data[‘sequence’][2][‘repeat’][‘sequence’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 10).

In case this is helpful for anyone in future, the below worked. My previous post just had some formatting/spacing errors for the most part.

- repeat:
      count: "{{ expand('group.next_feed_beaf') | length }}"
      sequence: 
      - service: input_datetime.set_datetime
        target:
          entity_id: "{{ expand('group.next_feed_beaf')[repeat.index - 1].entity_id }}"
        data:
          timestamp: >
            {% set delta = expand('group.next_feed_beaf')[repeat.index - 1].attributes.timestamp - as_timestamp(now()) %}
            {% if delta <= 300 %}
              {{ as_timestamp(now()) + 300 }}
            {% else %}
              {{ expand('group.next_feed_beaf')[repeat.index - 1].attributes.timestamp }}
            {% endif %}