I assume that this doesn’t work because of the new line that is added (content += u"\n"
) at the end of the script even if the shopping list is empty.
Ok, then your advice with length should work. But I can’t find the documentation of “length” even here
How should I write that?
I don’t use a lot of templating but try this:
- id: '1530354314132'
alias: What to buy
initial_state: 'on'
trigger:
platform: zone
zone: zone.zoo
entity_id: person.gayane
event: leave
condition:
- condition: template
value_template: "{{ states.sensor.shopping_list.state | length >= 10 }}"
action:
service: notify.mobile_app_iphone
data_template:
message: "{{states('sensor.shopping_list')}}"
Thank you that works. And
"{{ states('sensor.shopping_list') != 'To buy:' }}"
should work too, I just was triggering automation manually and that is why conditions was ignored
I’m a little confused. If it’s working, fine, but I don’t see in the Python script where it’s outputting a first line of 'To buy:'
. Also in the picture of the States page you showed sensor.shopping_list
and it had something in a completely different alphabet.
Sorry, I am just constantly changing “to buy” to russian word and trying different things. In the end, as I understand Issue was only in me, because I was triggering automation manually and that is why condition was ignored
So maybe I’m too late too the party, but I figured out the sensor part with the following jq statement:
- platform: command_line
name: shopping_list
command: "jq length .shopping_list.json"
It removes the dependency on the .py file to parse the json + you get the amount of items in your shopping list as a number.
This is only giving you the number of items, but after some trial and error I found this jq string who gives me only shopping list that are not ticked “complete”.
- platform: command_line
name: shopping_list
command: "jq '.[] | select(.complete==false) | .name' .shopping_list.json "
Now I just need to find a way to send this to my phone as a message, since I don’t have access to HA outside of home and if you click or clear the notification it get’s lost forever
The HA Android App doesn’t require remote access to your instance for sending notifications as it is based on Firebase, for iOS I don’t know.
Indeed notifications arrive to the app even when not connected (just tested now by disabling wifi on my iphone), the problem is they are just notifications and you accidentally click on them you lose them, let’s say I’m in at the supermarket and I want to check the list, I don’t want to lose them and I don’t have access to my home assistant dashboard.
I think I will try to link it with a telegram bot
You can make the notifications sticky so you can’t just wipe it away.
Hey, guys,
Sorry if I’m a total noob, but I can’t get this to work:
I’m trying to get the number of items in the list as a number (as you, @Thumbleweed, built), but filtering for those that are not marked as completed (as @djlorenz built)…
Has anyone figured out how to retrieve the length of a filtered down array?
The closest I got was with the following:
sensor:
- platform: command_line
name: "Shopping List"
unit_of_measurement: "items"
command: "jq '.[] | select(.complete==false) | .name | length' .shopping_list.json"
But it ends up returning the length of the string of each uncompleted item (number of characters of each uncompleted item), separated by a space.
Thanks, guys!
I got it! Hahaha
It was quite simple once I started working/testing through the terminal instead.
Here is the code:
sensor:
- platform: command_line
name: "Shopping List"
unit_of_measurement: "items"
command: "jq '[.[] | select(.complete==false) | .name] | length' .shopping_list.json"
As you can notice, it was really just about arranging the results from the filter into an array that is afterwards checked for how many objects it has inside.
Hope this is useful for someone!
- platform: command_line
name: shopping_list
command: "jq '.[] | select(.complete==false) | .name' .shopping_list.json "
That is working perfect for me. thanx very much!
But I have a cosmetical issue:
the email I receive all items listed in quotation marks like this:
“Brot”
“Toast”
“Möhren”
“Zucchini”
“Gehacktes”
“Hühnchenbrust”
“Wein”
Is there a way to avoid sending the Quotation marks?
in my automation I use following script:
action:
- service: notify.dirk
data_template:
message: '{{states.sensor.einkaufsliste.state}}'
would be very nice to get it like this:
Brot
Toast
Möhren
Zucchini
Gehacktes
Hühnchenbrust
Wein
It would be easier to read, wouldn’t it?
How to avoid to get the message text in the email with Quotion marks? And with space between?
Thanx
Add option -r to jq command, example:
- platform: command_line
name: shopping_list
command: "jq -r '.[] | select(.complete==false) | .name' .shopping_list.json"
@frosty:
not working. When option -r is added the email has no content and is completely empty
Here is the implementation I went with. It includes both complete and incomplete items (in separate attributes), and uses the length of the incomplete
list for the state.
sensor:
- platform: command_line
name: Shopping List
json_attributes:
- complete
- incomplete
command: "jq -r '{complete: [.[] | select(.complete==true)], incomplete: [.[] | select(.complete==false)]}' /config/.shopping_list.json"
value_template: "{{ value_json.incomplete | length }}"
I tested all the answers here, but i get all items on one line. Did you get it to work ?
I know this is an old thread, but if not helpful to you anymore, @Marcus_Lundblad and @Legromorph, it might be for others.
This does both of what you wanted to do, it joins the items into a string and puts them on separate lines:
{{ state_attr('sensor.einkaufsliste', 'incomplete') | join('\n') }}
And this is my sensor, based on @dale3h’s code, but only picking the item names:
sensor:
- platform: command_line
name: Einkaufsliste
json_attributes:
- complete
- incomplete
command: "jq -r '{complete: [.[] | select(.complete==true) | .name], incomplete: [.[] | select(.complete==false) | .name]}' .shopping_list.json"
value_template: "{{ value_json.incomplete | length }}"