Bluhme
(Lars Bluhme)
December 1, 2023, 2:13pm
1
I have this entity and want to extract the values of current day into entities through templating. Can anyone help me?
entity:
sensor.kona_daily_driving_stats
Returns:
2023-12-01:
total_consumed: 7568
engine_consumption: 5174
climate_consumption: 1524
onboard_electronics_consumption: 870
battery_care_consumption: 0
regenerated_energy: 2618
distance: 48
Tried this, but doesn’t work. Returns 0
{% set attr = state_attr(“sensor.kona_daily_driving_stats”, now().date()) %}
{{ 0 if attr == None else attr[“total_consumed”] /1000|int }}
Troon
(Troon)
December 1, 2023, 3:34pm
2
Please format your code correctly.
Before we begin…
This forum is not a helpdesk
The people here don’t work for Home Assistant, that’s an open source project. We are volunteering our free time to help others. Not all topics may get an answer, never mind one that helps you solve your problem.
[image]
This also isn’t a general home automation forum, this is a forum for Home Assistant and things related to it. Any question about Home Assistant, and about using things with Home Assistant, is welcome here. We can’t help you with e…
It’s not completely clear from your post what the entity state and attributes are. Please post a screenshot like this showing it:
or the properly-formatted output of this in the TEMPLATE editor:
{{ states.sensor.kona_daily_driving_stats }}
In addition to everything Troon said:
now().date()
isn’t a string and state_attr()
expects a string value for the attributes name. To get the date string use now().date() | string
.
{% set attr = state_attr("sensor.kona_daily_driving_stats", now().date() | string ) %}
{{ 0 if attr == none else (attr["total_consumed"] /1000) | int }}
Bluhme
(Lars Bluhme)
December 1, 2023, 3:53pm
4
Here is a screenshot:
Many thanks for your kindness
Troon
(Troon)
December 1, 2023, 3:56pm
5
In that case, Drew’s suggestion above should work for you. Just turn the date into a string, and make sure you are applying int
to the whole statement not just the 1000.
Nick4
(Nick)
March 1, 2024, 1:25pm
8
Hi Lars, please take the time to mark the answer as solution, you do that by selecting the three dots under the post:
Then select the check box:
By doing so this can be useful to other users as well and prevents that someone else steps in and still tries to help you.