I have a mealie instance that I’m trying to pull the shopping list items from into a rest sensor via template. My ultimate end goal is to get this data into a to-do list with an automation so I can sync the to-do list with Google Keep. I’m not sure you can do a template to-do object so I’m starting with a sensor. I’m getting hung up on a few things and I can’t seem to get past how to get the items into an array inside of the attributes list of the sensor.
If you want to make a todo list from these items, you can do that. Otherwise you can make a select entity that has a dropdwon of all the items. Or just a sensor with an attribute that has all the items.
Thank you for the help. The examples worked great. The last thing that is throwing me for a loop is deduping the data that is going to go into the todo list. For some reason it’s still being added into the todo list each time. I’m trying to compare between two lists and it’s not working. Not sure what I’m doing wrong here as I’ve not done comparison between a list and a todo list. There really isn’t many examples out there either.
Sorry if I am a bit slow to understand, but does this mean that it is not even possible to use value templates for rest sensor attributes?
I have this sensor which uses a value template for the state value:
- platform: rest
name: Trash
method: GET
icon: "mdi:trash-can"
resource: https://portal-api.kredslob.dk/api/calendar/address/07469584376925736
headers:
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36
value_template: >
{% set pickups = value_json[0]["plannedLoads"] %}
{% set expected = ["Restaffald", "Madaffald"] %}
{% set expected_join = expected | join(",") %}
{% set filtered_list = pickups | selectattr("fractions", "equalto", expected) | list %}
{% set time = filtered_list | map(attribute="date") | sort | first %}
{% if time is defined %}
{% set days_left = (strptime(time, "%Y-%m-%dT%H:%M:%S%z").date() - now().date()).days %}
{% if days_left == 0 %}
i dag
{% elif days_left == 1 %}
i morgen
{% else %}
{{ days_left }} dage
{% endif %}
{% else %}
{{ states('sensor.restaffald') }}
{% endif %}
json_attributes:
- plannedLoads
unique_id: skrald-restaffald-mad
scan_interval: 21600
Since it could not figure out how to use a value template for the attributes, I created a new template sensor:
sensor:
- name: "Restaffald Afhentning"
state: >
{% set pickups = state_attr('sensor.restaffald', 'plannedLoads') %}
{% if pickups %}
{% set expected = ["Restaffald", "Madaffald"] %}
{% set filtered_list = pickups | selectattr("fractions", "equalto", expected) | list %}
{% set time = filtered_list | map(attribute="date") | sort | first %}
{% if time %}
{% set days_left = (strptime(time, "%Y-%m-%dT%H:%M:%S%z").date() - now().date()).days %}
{% if days_left == 0 %}
i dag
{% elif days_left == 1 %}
i morgen
{% else %}
{{ days_left }} dage
{% endif %}
{% else %}
unknown
{% endif %}
{% else %}
unknown
{% endif %}
attributes:
next_pickup_date: >
{% set pickups = state_attr('sensor.restaffald', 'plannedLoads') %}
{% if pickups %}
{% set expected = ["Restaffald", "Madaffald"] %}
{% set filtered_list = pickups | selectattr("fractions", "equalto", expected) | list %}
{% set time = filtered_list | map(attribute="date") | sort | first %}
{{ time if time else 'unknown' }}
{% else %}
unknown
{% endif %}
friendly_name: "Next pickup date"
This works, but I would much rather only have one sensor for this to avoid confusion and because I have many similar sensors that I do not want to create both a rest sensor and template sensor for. Is this possible?
Thanks for the swift reply! I am, however, not sure I understand what this shows? Is is not the same rest sensor that I posted above (with no value template for the attribute)?
Hmm. As far as I can tell, the only difference between the rest sensor YAML I posted and what you posted above is that is now says
{% set pickups = value_json["plannedLoads"] %}
rather than
{% set pickups = value_json[0]["plannedLoads"] %}
But this just results in the sensor not being available (not working).
What I aim to do is to use a value template for the plannedLoads attribute similiar to what I did with the template sensor I created above.
The template sensor works, provides the correct state and has the correct attribute, but I would like to avoid having to make the tamplate sensor altogether and just just a value template for the attribute of the rest sensor. Hope that makes sense?
If the template I provided isn’t working, then there’s errors in your logs. Please post them. Otherwise removing the [0] should work based on your entity and template entity.
Logger: homeassistant.helpers.template
Source: helpers/template.py:2748
First occurred: December 1, 2024 at 9:51:20 PM (3 occurrences)
Last logged: 12:54:08 PM
Template variable warning: 'dict object' has no attribute 'position' when rendering '{{ value_json.position }}'
Template variable warning: 'list object' has no attribute 'plannedLoads' when rendering '{% set pickups = value_json["plannedLoads"] %} {% set expected = ["Restaffald", "Madaffald"] %} {% set expected_join = expected | join(",") %} {% set filtered_list = pickups | selectattr("fractions", "equalto", expected) | list %} {% set time = filtered_list | map(attribute="date") | sort | first %} {% if time is defined %} {% set days_left = (strptime(time, "%Y-%m-%dT%H:%M:%S%z").date() - now().date()).days %} {% if days_left == 0 %} i dag {% elif days_left == 1 %} i morgen {% else %} {{ days_left }} dage {% endif %} {% else %} {{ states('sensor.restaffald') }} {% endif %}'
Your original sensor should work then with the [0]. Try that again and look for errors. You should not need the additional attribute at all or template entity.
So either your rest sensor should be working without the template entity, or mine should be.