Zwaantje
(Sjors)
April 3, 2024, 3:12pm
1
Hello Guys, im trying to change the state of my picture entity card. so that it displays how many days until the trash needs to be collected, instead of displaying the date of collection.
The date is provided by my afvalbeheer entitiy.
Here is my Yaml:
type: custom:auto-entities
card:
square: true
type: grid
columns: 4
card_param: cards
filter:
include:
- entity_id: sensor.mijnafvalwijzer_gft
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/GFT1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
color: red;
- entity_id: sensor.mijnafvalwijzer_papier
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/PAPIER1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
- entity_id: sensor.mijnafvalwijzer_pmd
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/PLASTIC1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
- entity_id: sensor.mijnafvalwijzer_restafval
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/REST1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
exclude: []
sort:
method: state
numeric: true
I am not sure on how to display the days until, instead of the date itself.
Hope someone has the answer.
you could create a sensor.days_till_trash
helper template sensor with something like this:
{{ (states('sensor.mijnafvalwijzer_gft') - now()).days }}
This may be over the top but it is great …
Easy Time Macros for Templates! - Share your Projects! - Home Assistant Community (home-assistant.io)
EDIT: you can use card: “custom card templater” to allow to add templates to the card and then e.g. use the name_template to resolve your request
Zwaantje
(Sjors)
April 3, 2024, 4:36pm
5
Thanks, but no idea how to do this.
Where to implement this line to create this sensor.
Zwaantje
(Sjors)
April 3, 2024, 4:37pm
6
I will try later on, yet another card to put over my 5 types of cards haha.
Thanks
if you use the card-templater that @vingerha suggests, put my line for the name_template
to create a separate sensor, go to helpers, choose template, choose sensor template. put that line in and name it. all can be done in the ui.
that will create a new sensor with the days remaining. you can then use that sensor in place of where you have been using your current date sensor.
Zwaantje
(Sjors)
April 3, 2024, 5:54pm
8
Thanks for the help, i am currently creating the sensor, but it remains unavailable.
Do i need to enter any extra data?
petro
(Petro)
April 3, 2024, 6:43pm
9
{{ (states('sensor.mijnafvalwijzer_gft') | as_datetime | as_local - now()).days }}
It may remain unavailable upon restart. If it does, you’ll need to build it in yaml instead of the UI. Using availability.
Zwaantje
(Sjors)
April 3, 2024, 7:36pm
11
Thanks for the reply,
Reboot did not work, can you give me an example om how to make this sensor template in config.yaml?
Zwaantje
(Sjors)
April 4, 2024, 9:12am
12
i have a templates.yaml file
inside this yaml i have setup the following sensor:
##### sensor om days until date te creeeren voor afvalbeheer ophaal pictures##############
- name: afvalbeheer_gft
unique_id: afvalbeheer_gft
state: "{{ (states('sensor.mijnafvalwijzer_gft') | as_datetime | as_local - now()).days }}"
availability: >-
{% from 'availability_template.jinja' import avail %}
{{- avail(['sensor.mijnafvalwijzer_gft']) | bool -}}
In front of this i do have a line that says:
sensor:
and i have included the yaml file in my config.yaml
Also i have imported the availability template in hacs,
The sensor is now available, but now it says unknown. It still isnt giving me any data.
Hope you can help
Thanks
I don’t think anyone has asked, if you go to Developer Tools / Template and put this in :-
{{ states('sensor.mijnafvalwijzer_gft')}}
What is the resulting text?
petro
(Petro)
April 4, 2024, 11:33am
14
your availability template just needs to be
availability: "{{ 'sensor.mijnafvalwijzer_gft' | has_value }}"
Zwaantje
(Sjors)
April 4, 2024, 12:23pm
15
Thanks, i have entered this line: i will share my complete templates.yaml to be sure:
- sensor:
##### wifi plug 1 ########################################################
- name: wifi plug 01 current
unit_of_measurement: "ma"
state: "{{ state_attr('switch.wifi_stekker_01', 'current') }}"
- name: wifi plug 01 current_consumption
unit_of_measurement: "kwh"
state: "{{ state_attr('switch.wifi_stekker_01', 'current_consumption') }}"
##### wifi plug 2 ########################################################
- name: wifi plug 02 current
unit_of_measurement: "ma"
state: "{{ state_attr('switch.wifi_stekker_02', 'current') }}"
- name: wifi plug 02 current_consumption
unit_of_measurement: "kwh"
state: "{{ state_attr('switch.wifi_stekker_02', 'current_consumption') }}"
##### Low Battery list ###################################################
- name: "Low Battery Devices"
unique_id: 6420ded2-6237-4c25-97e5-5601d8f65dd3
icon: mdi:battery-low
state: >
{% set threshold = states('input_number.battery_threshold') | int %}
{%- set ns = namespace(sensors=[]) -%}
{%- for state in states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.state_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| selectattr('state', 'is_number') -%}
{%- if state.state | int <= threshold -%}
{% set ns.sensors = ns.sensors + [dict(name = state.name | replace(' battery', '') | replace(' Battery', ''), state = state.state | int)] %}
{%- endif -%}
{%- endfor -%}
{%- set batt = ns.sensors | sort(attribute='state') %}
{%- set ns = namespace(batt='') -%}
{%- for state in batt -%}
{% set ns.batt= ns.batt + (state.name ~ ' (' ~ state.state ~'%)' ~ "\n") %}
{%- endfor -%}
{% if ns.batt | count > 0 %}
{{ ns.batt | truncate(255, true, '...') }}
{% else %}
{{ 'unavailable' }}
{% endif %}
############# volgend alarm sensor#####
- name: "sjors alarm"
state: >
{{now().strftime("%a %h %d %H:%M %Z %Y") ==
(((state_attr('sensor.sjors_next_alarm', 'Time in Milliseconds') |
int / 1000)) | timestamp_custom('%a %h %d %H:%M %Z %Y'))}}
##### sensor om days until date te creeeren voor afvalbeheer ophaal pictures##############
- name: afvalbeheer_gft
unique_id: afvalbeheer_gft
state: "{{ (states('sensor.mijnafvalwijzer_gft') | as_datetime | as_local - now()).days }}"
availability: "{{ 'sensor.mijnafvalwijzer_gft' | has_value }}"
I have rebooted after this, and still it says unknown.
Again i wanna thank you for helping!
petro
(Petro)
April 4, 2024, 12:25pm
17
If the template still doesn’t work, then you should take @RoadkillUK 's advice and post the state of sensor.mijnafvalwijzer_gft
shown on Developer Tools → States page.
Zwaantje
(Sjors)
April 4, 2024, 12:26pm
18
This is what you mean right?
petro
(Petro)
April 4, 2024, 12:28pm
19
based on that, template should be
state: "{{ (strptime(states('sensor.mijnafvalwijzer_gft'), '%d-%m-%Y') | as_local - now()).days }}"
Zwaantje
(Sjors)
April 4, 2024, 12:58pm
20
Well at least now its doing something, but im not there yet:
i have put the normal file below so you can see the actual dates
Here is the yaml for the top row that says -1:
type: custom:auto-entities
card:
square: true
type: grid
columns: 4
card_param: cards
filter:
include:
- entity_id: sensor.afvalbeheer_gft
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/GFT1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
color: red;
- entity_id: sensor.afvalbeheer_papier
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/PAPIER1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
- entity_id: sensor.afvalbeheer_pmd
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/PLASTIC1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
- entity_id: sensor.afvalbeheer_rest
options:
type: picture-entity
show_name: false
image: /local/afbeeldingen/REST1.png
card_mod:
style: |
ha-card
{background: transparent;
border-style: none;
exclude: []
sort:
method: state
numeric: true
And here is the current templates.yaml:
##### sensor om days until date te creeeren voor afvalbeheer ophaal pictures##############
- name: afvalbeheer_gft
unique_id: afvalbeheer_gft
state: "{{ (strptime(states('sensor.mijnafvalwijzer_gft'), '%d-%m-%Y') | as_local - now()).days }}"
availability: "{{ 'sensor.mijnafvalwijzer_gft' | has_value }}"
- name: afvalbeheer_pmd
unique_id: afvalbeheer_pmd
state: "{{ (strptime(states('sensor.mijnafvalwijzer_gft'), '%d-%m-%Y') | as_local - now()).days }}"
availability: "{{ 'sensor.mijnafvalwijzer_pmd' | has_value }}"
- name: afvalbeheer_rest
unique_id: afvalbeheer_rest
state: "{{ (strptime(states('sensor.mijnafvalwijzer_gft'), '%d-%m-%Y') | as_local - now()).days }}"
availability: "{{ 'sensor.mijnafvalwijzer_restafval' | has_value }}"
- name: afvalbeheer_papier
unique_id: afvalbeheer_papier
state: "{{ (strptime(states('sensor.mijnafvalwijzer_gft'), '%d-%m-%Y') | as_local - now()).days }}"
availability: "{{ 'sensor.mijnafvalwijzer_papier' | has_value }}"
So its only giving minus 1, wich is the wrong number of days, and also its not saying days itself.