TooGoodToGo MQTT Bridge

Hi Everyone,

being a huge fan of TooGoodToGo (buy leftover food for a reduced price) for years I stripped some telegram bot I use for myself and made some Home Assistant Add-on out of the core.

Sensors are pushed to HA via MQTT AutoDiscover.

It simply fetch the favorite shops and delivers them into HomeAssistant.

Main repo can be found here: https://github.com/MaxWinterstein/toogoodtogo-ha-mqtt-bridge
or in my Add-on repository: https://github.com/MaxWinterstein/homeassistant-addons

I am mostly happy to have data at Home Assistant Level and therefore in InfluxDB so I can create some ā€˜big dataā€™ analysis for my favorite shops.

Of course somebody could create notifications out of this, e.g. when your favourite shop add some offer. But this would be bad and you should feel bad.
(If I see you catching the Sushi I hunt for my wife I will steal your soya sauce!)

Feedback is highly welcome! Doing this in my spare time to give the community something back :v:

6 Likes

Love this! will install this !

1 Like

Question: How many times will it refresh?
The stock_available is interessant to make a alert for.

When something is in stock let me now.
But if refresh time is long, maybe im behind the actual stock ā€¦

Currently it is every 30s hardcoded:

Will make it configurable and default to every 5 minutes I guess. No need for hate from the TooGoodToGo server admins :wink:

1 Like

Thanks. I have 12 shops now with sensorsā€¦ so there will be a moment I will see a error in my log hahaha

haha, awesome!

Little ugly, stores that where removed from the favourites will stay at HA until manually deleted.

Will add something for that case when i find time.

Thatā€™s ugly! Haha. I love the integration. Today I create my automation to notice me when new boxes are available. Never have to check it myself all the time!

I will share my script when done. Maybe you can add it in your wiki part on github

Maybe it is a option to change

sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box

to

sensor.tgtg_12434

ID of the shop will always be the same. When the shop change a part of its name the ID still be the same.
With this sensor. Its shortā€¦ In the attributes you already have the friendly_name.

And maybe you can stock_available from true/false to 1/0. I think you can use them in a trigger template part as numeric value?

Sadly not. The Sensor name is always created by the friendly name from HomeAssistant.
But, in the background there is some unique id that is set to the item id, this could/should prevent such things.

It can be used in a few ways.

To prevent anybody from accidentally creating notifications as soon as portions are available, here is something you should not create as automation, so be careful!

alias: TooGoodToGo Test
description: ''
trigger:
  - platform: state
    entity_id: sensor.toogoodtogo_brot_freunde
    attribute: stock_available
    from: 'false'
    to: 'true'
condition: []
action:
  - service: notify.mobile_app_iphone_max
    data:
      message: There is food!
mode: single


1 Like

Scrap what I said below, I restarted the app and now I can see the sensors and their status. Looks me just being impatient.

Many thanks,

Andy

#################
Hiya, Iā€™ve installed the app once I added the repo to the supervisor page, then added my mqtt details and Tgtg login and when the app starts I can see in both the app log and my mqtt server that it is retrieving my favourites and connecting to the mqtt server but Iā€™m not getting any sensors. When I subscribe to everything on the mqtt server I donā€™t see any tgtg messages - am I missing something?

Iā€™ve just checked the tgtg website and the first place that will be offering something is Starbucks tonight at 1900 - do I have to wait for the companies to offer something before the sensors are created?

Many thanks,

Andy

This is what I have created :slight_smile:

###########################################################################################
# NOTIFY ME WHEN BOXES ARE IN STOCK AT TOGOODTOGO
###########################################################################################
- alias: "ToGoodToGo - All shops"
  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
      attribute: stock_available
      from: "false"
      to: "true"

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

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "{{ name }} have {{ stock }} in stock. Price: {{ price }} "
2 Likes

That looks much cleaner than the automation I generated poudenes, I think Iā€™ll use yours instead.

Thank you,

Andy

1 Like

It seems this is not working.
When I remove this part:

      from: 'false'
      to: 'true'
      attribute: stock_available

Script is working. You get every time when a update is there a notification.
So when stock is go from 4 to 3 to 2 to 1 to 0 or vs.

I donā€™t know why it wonā€™t work with above extra linesā€¦

Here is my working automation!

###########################################################################################
# NOTIFY ME WHEN BOXES ARE IN STOCK AT TOGOODTOGO
###########################################################################################
- alias: "ToGoodToGo - All shops"
  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

  condition: "{{ trigger.to_state.attributes.stock_available == true }}"

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

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "Stock: {{ stock }}, Price: {{ price }}, Name: {{ name }} "
1 Like

Added this in my global customize part for nice icon colors when on:

"sensor.toogoodtogo_*":
  templates:
    icon_color: if (state == 0 ) return 'rgba(71,116,157,1)'; return 'rgba(251, 214, 67,1)';

EDIT: update script. First one was incorrect. This one is working!

1 Like

Thx for sharing @poudenes :heart:

Is there a reason that you use the portions value as state instead of the stock_available?
With stock_available you can group them also because if one is TRUE then group goes on.
You can get the portions with state_attrā€¦

Nice update. Will it be possible to keep de update_interval back min: 30 sec?
I see some stores that have some in stock and its gone within the minuteā€¦

Oh, seems like i never hit [Reply] on that :roll_eyes:


well, yeah, as i wanted some nice graphs genereated automatically :slight_smile:

This way I can see not only when portions are available, I can see how many and how quick they are sold out.

As i spend some lovely holiday in Amsterdam a few years ago (There was some awesome waffle house, so friendly and lovely) i give you some secret hint:
0.5 is also a valid value (aka ā€œhalf of a minuteā€) :wink:

1 Like

Waffles in Amsterdam are the bestā€¦ Thats why on donā€™t eat them. Otherwise i was 150kg now haha
Thanks for the hint :wink: