Home Assistant Buttons for "Today", "Last Week", or "Last Month" #283
Replies: 8 comments
-
Hi @sapnho, that should doable. One could create 3 buttons triggering an automation. Based on the entity-Id of the buttons an automation could calculate the mqtt message. To calculate today {{ (now().strftime("%Y/%m/%d")) }} To calculate last week one could use timetelda()
To calculate last month:
|
Beta Was this translation helpful? Give feedback.
-
Just tested the calculation: Today
{% set today = now() %}
{{ (today.strftime("%Y/%m/%d")) }}
Last week:
{% set first_day_of_week = (today.strftime("%w")) | int %}
day of week: {{ first_day_of_week }}
first day of week: {{ as_timestamp(today - timedelta(days=first_day_of_week, weeks=1)) | timestamp_custom("%Y/%m/%d")}}
last day of week: {{ as_timestamp(today - timedelta(days=first_day_of_week - 6, weeks=1)) | timestamp_custom("%Y/%m/%d")}}
Last month:
{% set day_of_month = (today.strftime("%d")) | int %}
day of month: {{ day_of_month }}
last day of last month: {{ as_timestamp(today - timedelta(days=day_of_month)) | timestamp_custom("%Y/%m/%d")}}
first day of last month: {{ as_timestamp(today - timedelta(days=day_of_month)) | timestamp_custom("%Y/%m/01")}} output: Today
2022/06/13
Last week:
day of week: 1
first day of week: 2022/06/05
last day of week: 2022/06/11
Last month:
day of month: 13
last day of last month: 2022/05/31
first day of last month: 2022/05/01 |
Beta Was this translation helpful? Give feedback.
-
type: vertical-stack
cards:
- type: entities
entities:
- entity: sensor.picframe_image_counter
name: Bilder
- entity: select.picframe_directory
name: Directory
- entity: input_text.picframe_tags_filter
name: Stichwortsuche
- entity: input_text.picframe_location_filter
name: Ortssuche
- entity: input_text.picframe_date_from
name: Bilder ab (TT.MM.JJJJ)
- entity: input_text.picframe_date_to
name: Bilder bis (TT.MM.JJJJ)
show_header_toggle: false
theme: Backend-selected
title: Filter
state_color: true
- show_name: false
show_icon: true
show_state: false
type: glance
entities:
- entity: input_button.picframe_date_today
tap_action:
action: toggle
- entity: input_button.picframe_date_last_week
tap_action:
action: toggle
- entity: input_button.picframe_date_last_month
tap_action:
action: toggle
state_color: true Create new automation: alias: picframe_quick_date_select
description: ''
trigger:
- platform: state
entity_id:
- input_button.picframe_date_today
id: today
- platform: state
entity_id:
- input_button.picframe_date_last_week
id: last_week
- platform: state
entity_id:
- input_button.picframe_date_last_month
id: last_month
condition: []
action:
- if:
- condition: trigger
id: today
then:
- service: mqtt.publish
data:
topic: picframe/date_to
payload_template: '{{ now().strftime("%Y/%m/%d") }}'
- service: mqtt.publish
data:
topic: picframe/date_from
payload_template: '{{ now().strftime("%Y/%m/%d") }}'
else:
- if:
- condition: trigger
id: last_week
then:
- service: mqtt.publish
data:
topic: picframe/date_from
payload_template: >-
{% set first_day_of_week = (now().strftime("%w")) | int %} {{
as_timestamp(now() - timedelta(days=first_day_of_week, weeks=1))
| timestamp_custom("%Y/%m/%d")}}
- service: mqtt.publish
data:
topic: picframe/date_to
payload_template: >-
{% set first_day_of_week = (now().strftime("%w")) | int %} {{
as_timestamp(now() - timedelta(days=first_day_of_week - 6,
weeks=1)) | timestamp_custom("%Y/%m/%d")}}
else:
- condition: trigger
id: last_month
- service: mqtt.publish
data:
topic: picframe/date_from
payload_template: >-
{% set day_of_month = (now().strftime("%d")) | int %} {{
as_timestamp(now() - timedelta(days=day_of_month)) |
timestamp_custom("%Y/%m/01")}}
- service: mqtt.publish
data:
topic: picframe/date_to
payload_template: >-
{% set day_of_month = (now().strftime("%d")) | int %} {{
as_timestamp(now() - timedelta(days=day_of_month)) |
timestamp_custom("%Y/%m/%d")}}
mode: single |
Beta Was this translation helpful? Give feedback.
-
Found a bug. The date filters are not updated correctly. But setting the dates works. I have to check this. |
Beta Was this translation helpful? Give feedback.
-
Thanks, @helgeerbe! Will also give it a try and let us know, once you have found the bug. |
Beta Was this translation helpful? Give feedback.
-
When I paste the automation I get the error "Message malformed: template value should be a string for dictionary value @ data['action'][0]['else'][0]['else'][1]['data']". |
Beta Was this translation helpful? Give feedback.
-
No, this seems to be a copy error. I edited the yaml code. Could you try it again |
Beta Was this translation helpful? Give feedback.
-
I had to edit the home assistant blueprint code. The easiest way to update is to delete the automation and to delete the blueprint. Then import the blueprint again and create the automation from the blueprint. Since release 2021.10 Home Assistant requires default values in some time manipulation to create date objects from string. Without default values an exception is thrown. |
Beta Was this translation helpful? Give feedback.
-
@helgeerbe Your integration of Pi3D PictureFrame into Home Assistant last year was another milestone for this project. I was wondering about a use case that I actually have quite often and how to make it happen.
I often want to show images only of "Today", "Last Week", or "Last Month". So in Home Assistant, I manually set the FROM date. Is it possible to have buttons that send a FROM DATE MQTT message which is calculated automatically? How would that work?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions