Help needed: Create smarter automation

Hi All, below you see a automation that work great. But what I want is instead of all sensors want to add a

group.toogoodtogo

Each sensor have state that show amount and attributes:

price: 4.99
stock_available: false
url: http://share.toogoodtogo.com/item/71544
pickup_start: Unknown
pickup_start_human: Unknown
pickup_end: Unknown
pickup_end_human: Unknown
picture: https://images.tgtg.ninja/store/1129390a-5c0f-4933-a574-3a9b8026aaf0.png
unit_of_measurement: portions
friendly_name: Deauvuille Atrium

How can I modify this automation so it will work when I remove all sensors and add the group.toogoodtogo into the trigger? The code below I got from a forum so its not mine… ( I wish :slight_smile: )

- id: "notify_me_when_toogoodtogo"
  alias: "Notification - TooGoodToGo"
  trigger:
    - platform: state
      entity_id:
        - sensor.toogoodtogo_broodhuys_meijer_magic_box
        - sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box
        - sensor.toogoodtogo_albert_heijn_to_go_amsterdam_wtc_magic_box
        - sensor.toogoodtogo_boulangerie_noe_gustav_mahlerlaan_amsterdam
        - sensor.toogoodtogo_de_drie_graefjes_stadionplein_amsterdam
        - sensor.toogoodtogo_hema_gelderlandplein
        - sensor.toogoodtogo_lidl_amsterdamseweg_188_groente_fruit_box
        - sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box
        - sensor.toogoodtogo_starbucks_gustav_mahler_amsterdam
        - sensor.toogoodtogo_sushi_time_wtc_amsterdam_magic_box
        - sensor.toogoodtogo_the_roastary_magic_box
        - sensor.toogoodtogo_poke_perfect_zuidas_magic_box
        - sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box
        - sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box
        - sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box
        - sensor.toogoodtogo_brood_en_banketbakkerij_hulleman_buitenveldertselaan_amsterdam
        - sensor.toogoodtogo_jumbo_velu_kastelenstraat_amsterdam
        - sensor.toogoodtogo_instock_producent_abn_amro_circl
      from: false
      to: true
      attribute: stock_available

  mode: queued

  variables:
    stock: "{{ states(trigger.to_state.entity_id) }}"
    name: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }}"
    picture: "{{ state_attr(trigger.to_state.entity_id, 'picture') }}"
    url: "{{ state_attr(trigger.to_state.entity_id, 'url') }}"

  action:
    - service: script.mobile_notify_no_actionable
      data:
        title: "TooGoodToGo: {{ name }}"
        thread_id: "toogoodtogo"
        message: >
          {% set entityID = trigger.to_state.entity_id %}
          {% set names = state_attr(entityID,'friendly_name').split('-') %}
          {% set namesLength = names | length %}
          {% if namesLength > 2 %}
            {% set outName = names[1] + '-' + names[2] %}
          {% else %}
            {% set outName = names[1] %}
          {% endif %}
          {{ name }}: {{ states(entityID) }} x{{ outName }} €{{ state_attr(entityID,'price') }}
        img: "{{ picture }}"
        buttontitle: "Open TooGoodToGo App"
        uri: URI
        url: "{{ url }}"
        soundname: default
        critical: 0
        volume: 0

Greetings,
Peter

As far as I know, you can’t do that and it expect it to work the same way like what you currently have.

If you use a State Trigger with group.toogoodtogo it will only trigger when the group’s state changes. That means when it changes from off to on and that happens when all sensors in the group are false and one of them changes true. The next sensor that changes to true won’t affect the group’s state which is already on so there is no additional triggering of the State Trigger.

If you use a Template Trigger, the situation is the same as described as above. for example, if you use this:

  - platform: template
    value_template: "{{ expand('group.toogoodtogo') | selectattr('state', 'eq', 'true') | list | count }}"

it will trigger when the count changes from 0 to 1. It won’t trigger when the count continues to increase. The count must first return to 0 and only then will it trigger when it changes from 0 to 1.

Thanks for the explanation :smiley: I will play around with this part and see what it will do.
Do you have a nice website where many examples of how to do this kind of if codes things in yaml?