MaxWinterstein / toogoodtogo-ha-mqtt-bridge

36 stars 8 forks source link

Automatically create Push-Notifications for favorited Stores #106

Open bigbasti opened 1 year ago

bigbasti commented 1 year ago

Hello Everyone,

i am fiddling around with this plugin and basically everything works well.

I am wondering if it is possible to automate creating a push notification automation for all favorited stores.

Right now I have to update the automation for the notification manually every time I add a new favorite store in the tgtg app.

It is possible to read all created sensors from the favorites:

{%- for state in states -%}
    {%- if (state.entity_id.startswith('sensor.toogoodtogo_'))-%}
      {{state.entity_id}},
    {%- endif -%}
{%- endfor -%}

Is it somehow possible to feed this directly into the notifications automation so I just need to add the store to the favorites inside the tgtg app and automatically receive notifications for it?

Thanks

MaxWinterstein commented 1 year ago

indeed, good idea.

I searched a bit, and found this anwser to a simila question:

You can solve that by creating a template sensor which has a count of sensors below 15.5 degrees as it’s state, and a list of entities which are below 15.5 degrees as an attribute.

https://community.home-assistant.io/t/add-support-for-wildcards-in-automation-trigger-entity-id/152337/6


I did some playaround, and this automation might be a good start:

alias: TooGoodToGo Food Available
description: "Push a notification as soon as food is available"
trigger:
  - platform: event
    event_type: state_changed
    event_data:
      domain: sensor
condition:
  - condition: template
    value_template: "{{ trigger.event.data.entity_id.startswith('sensor.toogoodtogo_')}}"
action:
  - service: notify.max
    data: {}
    data_template:
      message: >-
        {{ trigger.event.data.old_state.name }} has changed from {{
        trigger.event.data.old_state.state }} to {{
        trigger.event.data.new_state.state }}
mode: single

need some filtering for like old_state.state > new_state.state but I think this might be a good start.

With all that developing i just rate limited myself again, so i cant test further 😉

Maxklos commented 1 year ago

Hey i just did this in Node RED with Notification to my Phone (with the Home Assistant app). My Notifications feature the Picture of the Store, Portion and a Link to the App (if installed on the phone and set to open with the "share" link). The notification is in german but you can easily adapt it by changing the template Node. Here is the code for Node Red:

[{"id":"d040d1f6169a6d8d","type":"server-state-changed","z":"b3ecc161.a2ba4","name":"TGTG All","server":"2b859e09.cba102","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.toogoodtogo","entityidfiltertype":"substring","outputinitially":true,"state_type":"num","haltifstate":"0","halt_if_type":"num","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"for":"1","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"id","propertyType":"msg","value":"","valueType":"triggerId"}],"x":340,"y":1220,"wires":[["01acf76b1a7fa347"],[]]},{"id":"01acf76b1a7fa347","type":"api-current-state","z":"b3ecc161.a2ba4","name":"Get Data","server":"2b859e09.cba102","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{id}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":480,"y":1220,"wires":[["11615b37a68db748"]]},{"id":"11615b37a68db748","type":"template","z":"b3ecc161.a2ba4","name":"TGTG","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Bei {{data.attributes.friendly_name}} sind {{data.state}} Portionen verfügbar","output":"str","x":610,"y":1220,"wires":[["455fcf1302bbc582"]]},{"id":"455fcf1302bbc582","type":"api-call-service","z":"b3ecc161.a2ba4","name":"","server":"2b859e09.cba102","version":5,"debugenabled":true,"domain":"notify","service":"mobile_app_nikos_pixel_6","areaId":[],"deviceId":[],"entityId":[],"data":"{\t \"message\":msg.payload,\t \"title\":\"TGTG\",\t \"data\": {\t \"image\": msg.data.attributes.picture,\t \"actions\": [\t {\t \"action\": \"URI\",\t \"title\": \"Open TGTG\",\t \"uri\": \"https://link.toogoodtogo.com\"\t \t \t }\t \t \t ]\t\t \t \t }\t\t\t\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":1220,"wires":[[]]},{"id":"2b859e09.cba102","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]