Resource_template url with this week

I am working on creating some sensors for my kids school lunches. i am able to pull the menu from a url, and i put in the start and end date.

below is example for next week

https://api.mealviewer.com/api/v4/school/school1/11-07-2022/11-11-2022/

That is monday thru friday’s dates.

Is there a jinja template command that would let me update mysensors below, to put in this weeks mon and friday date? using something similar to the example

rest:
  - scan_interval: 3600
    resource_template: https://api.mealviewer.com/api/v4/school/school1/11-07-2022/11-11-2022/
    method: GET
    sensor:
      - unique_id: lunches_today
        name: Lunch Elementary School
        value_template: 'OK'
        json_attributes:
        - menuSchedules

and yes, i know the value_template will set the state to “OK” only. I am bringing in the values into attributes because of the 255 char limit of the state.

thanks!

I think i have something that works. (need to let it run and see what all happens but so far it’s going well)

using this.

resource_template: https://api.mealviewer.com/api/v4/school/school1/{{ (now().timestamp()-(0-now().weekday())*86400) | int | timestamp_custom("%Y-%m-%d") }}/{{ (now().timestamp()+(4-now().weekday())*86400) | int | timestamp_custom("%Y-%m-%d") }}/

so it’s basically pulling “this past monday” and “this coming friday”

It was working until it moved off monday.

made an adjustment.

resource_template: https://api.mealviewer.com/api/v4/school/school1/{{ (now().timestamp()+(0-now().weekday())*86400) | int | timestamp_custom("%Y-%m-%d") }}/{{ (now().timestamp()+(4-now().weekday())*86400) | int | timestamp_custom("%Y-%m-%d") }}/

A while back I made a simple integration for Mealviewer: https://github.com/jdeath/mealviewer My kid’s school just switched to FD MealPlanner, so I made a version for that too: https://github.com/jdeath/fdmealplanner

2 Likes

This is really cool. My child’s school uses mealviewer. They also serve breakfast. I currently have it set up as per your github instructions and it is showing breakfast only. Any idea how to get it to show lunch?

I just checked the repo. Doesn’t look like it’s setup for breakfasts. Would be easy enough to add it looks like, i guess the trickier part would be making it so you can configure it to have or not have breakfast option.

I do not use mealviewer anymore, but feel free to muck around in code.

Looking quickly at the old code, line 112 says “lunch_menu = menus[0]” . try changing that index to [1] and see if it gets the lunch instead. If that does not work, try changing line 106 menus = r.json().get(‘menuSchedules’)[0].get(‘menuBlocks’) to [1] index.

Reviving this thread a bit, sorry folks.
My kid’s schools just switched from Nutrislice to Mealviewer so I just found this thread. I have the custom component from @jaaem installed, but the attributes are still [null]. I just installed it so maybe it needs to update with the next cycle.
However, I also made the rest sensor from @brunkj and all the attributes are showing, but I’m not sure how to put this into a Markdown card.
@brunkj do you have an example of how you’re displaying this sensor?

Thanks for the work you two have put into this.

My code should get an update when you restarted home assistant after installation. I have not used it in many years, so the back end could have changed. If the rest sensor works, you could probably adapt my code. But if rest works, stick with that! The markdown code in my readme should give you a clue as what to do. just change the sensor name to what you rest sensor provides.

I just got your code working. Turns out I fat-fingered one extra letter and it took me a few restarts to figure it out. haha.
In case anyone sees this in the future, our school provides breakfast and changing line 112 (not 122) from

lunch_menu = menus[0]

to

lunch_menu = menus[1]

worked to pull in the lunch items.
Thanks so much for making that custom component. So helpful.

1 Like

Thank you for this! I was wondering if anyone has insight how to add the ability to also view school breakfast?