Chouffy / home_assistant_tgtg

TooGoodToGo items stock as sensor in Home Assistant
MIT License
68 stars 18 forks source link

Markdown card for on the dashboard #73

Closed Sarnog closed 1 year ago

Sarnog commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe.

Hello,

Is there any way to create a markdown card or something like that, that fills automaticly with all the tgtg sensors, and displays only the sensors, that are active. Ofcourse i would love to see this with the attributes showen as well.

Example:

(only showen when 'available (status > 0, and not sold-out)', otherwise hidden):

'Happy Snacks (attr friendly_name)'
'10 (attr status)' pcs 'magic snack boxes (attr item_url)'
From: '€20,- (attr original_value)', for: '€5,- (attr item_price)'
Pickup from: '20:00 (attr pickup_start)' - '23:00 (attr pickup_end)'

Describe the solution you'd like

Maybe anybody can do something with auto-entities mod and/or markdown card? I tried, but can't figure it out. I'm lacking skills for this.

Describe alternatives you've considered

Not have it showen on my dashboard... :-/

Additional context

(only showen when 'available (status > 0, and not sold-out )', otherwise hidden):

'Happy Snacks (attr friendly_name)'
'10 (attr status)' pcs 'magic snack boxes (attr item_url)'
From: '€20,- (attr original_value)', for: '€5,- (attr item_price)'
Pickup from: '20:00 (attr pickup_start)' - '23:00 (attr pickup_end)'
WalterGro commented 1 year ago

I use two custom cards to see which TGTG boxes are available, the auto-enties card and the multiple-entity row.

type: custom:auto-entities
card:
  type: entities
  title: Beschikbaar
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if state.state>= '1'  and 'sensor.tgtg_' in state.entity_id %}
      {%- if state_attr(state.entity_id, 'pickup_start') is not none  -%} 
        {{
          {
            'entity': state.entity_id,
            'name': state.attributes.friendly_name[5:],
            'type': "custom:multiple-entity-row",
            'unit': false,
            'secondary_info': 'Ophalen op ' + state.attributes.pickup_start[8:10] + "-" + state.attributes.pickup_start[5:7] + ' tussen '+ state.attributes.pickup_start[11:16] + ' en ' + state.attributes.pickup_end[11:16] + ', € '+  state.attributes.item_price[:-3],
            'tap_action': { 
              'action': 'url',
              'url_path':  state.attributes.item_url}
            }

        }},  
      {%- endif -%}
      {%- endif -%}
    {%- endfor %}
Sarnog commented 1 year ago

Thanks for the card! Altho they remain empty, even when i receive a notification when there is something available. What i'm i doing wrong?

WalterGro commented 1 year ago

I don't know... indentation is very important with this card. I had some trouble with it. If you copy and paste the code in the template editor, does that work? By the way, there is an error with the pickup time in the card, it's an hour to early in the card

Sarnog commented 1 year ago

I don't know... indentation is very important with this card. I had some trouble with it. If you copy and paste the code in the template editor, does that work? By the way, there is an error with the pickup time in the card, it's an hour to early in the card

On the template editor i get this error:

TypeError: '>=' not supported between instances of 'float' and 'str'
WalterGro commented 1 year ago

That's strange. I copied the code above in my template editor, no problem. Did you copy all the code? And if you delete the part with >=?

Sarnog commented 1 year ago

That's strange. I copied the code above in my template editor, no problem. Did you copy all the code? And if you delete the part with >=?

This is what i put in the editor:


{% for state in states.sensor -%}
  {%- if state.state>= '1'  and 'sensor.tgtg_' in state.entity_id %}
  {%- if state_attr(state.entity_id, 'pickup_start') is not none  -%} 
    {{
      {
        'entity': state.entity_id,
        'name': state.attributes.friendly_name[5:],
        'type': "custom:multiple-entity-row",
        'unit': false,
        'secondary_info': 'Ophalen op ' + state.attributes.pickup_start[8:10] + "-" + state.attributes.pickup_start[5:7] + ' tussen '+ state.attributes.pickup_start[11:16] + ' en ' + state.attributes.pickup_end[11:16] + ', € '+  state.attributes.item_price[:-3],
        'tap_action': { 
          'action': 'url',
          'url_path':  state.attributes.item_url}
        }

    }},  
  {%- endif -%}
  {%- endif -%}
{%- endfor %}
Sarnog commented 1 year ago

Ssems like this code is working:

type: custom:auto-entities
card:
  type: entities
  title: Beschikbare boxen TGTG
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if state.state|float(default=0) >= 1  and 'sensor.tgtg_' in state.entity_id %}
      {%- if state_attr(state.entity_id, 'pickup_start') is not none  -%} 
        {{
          {
            'entity': state.entity_id,
            'name': state.attributes.friendly_name[5:],
            'type': "custom:multiple-entity-row",
            'unit': false,
            'secondary_info': 'Ophalen op ' + state.attributes.pickup_start[8:10] + "-" + state.attributes.pickup_start[5:7] + ' tussen '+ state.attributes.pickup_start[11:16] + ' en ' + state.attributes.pickup_end[11:16] + ', € '+  state.attributes.item_price[:-3],
            'tap_action': { 
              'action': 'url',
              'url_path':  state.attributes.item_url}
            }

        }},  
      {%- endif -%}
      {%- endif -%}
    {%- endfor %}
tjorim commented 1 year ago

Thanks, added it to the readme. image

Dinth commented 1 year ago

This is amazing. I never used auto_entities, but would it be possible to add a filter of entities for pickup today or pickup tomorrow?

Joepie007v2 commented 1 year ago

I use two custom cards to see which TGTG boxes are available, the auto-enties card and the multiple-entity row.

type: custom:auto-entities
card:
  type: entities
  title: Beschikbaar
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if state.state>= '1'  and 'sensor.tgtg_' in state.entity_id %}
      {%- if state_attr(state.entity_id, 'pickup_start') is not none  -%} 
        {{
          {
            'entity': state.entity_id,
            'name': state.attributes.friendly_name[5:],
            'type': "custom:multiple-entity-row",
            'unit': false,
            'secondary_info': 'Ophalen op ' + state.attributes.pickup_start[8:10] + "-" + state.attributes.pickup_start[5:7] + ' tussen '+ state.attributes.pickup_start[11:16] + ' en ' + state.attributes.pickup_end[11:16] + ', € '+  state.attributes.item_price[:-3],
            'tap_action': { 
              'action': 'url',
              'url_path':  state.attributes.item_url}
            }

        }},  
      {%- endif -%}
      {%- endif -%}
    {%- endfor %}

Thanks. Works great. I’m just wondering how to adjust the time. The attribute shows e.g. pickup_start 15:10, but it is actually 17:10. So 2 hours difference. I live in the Netherlands. Can I adjust it in your card example or do I have to do this in another way. Hope you can share how you are handling the time zone.

busterer commented 4 months ago

where do i paste the code?

CasperBE commented 4 months ago

You could use this line for displaying the pickup times in your local timezone: 'secondary_info': as_timestamp(as_datetime(state.attributes.pickup_start).astimezone()) | timestamp_custom('%a %d/%m') | lower + ' between ' + as_timestamp(as_datetime(state.attributes.pickup_start).astimezone()) | timestamp_custom('%H:%M') + ' and ' + as_timestamp(as_datetime(state.attributes.pickup_end).astimezone()) | timestamp_custom('%H:%M') + ', € ' + state.attributes.item_price[:-3],