Hope somebody can help me, I’m at a dead end. I have a RESTful sensor with a GET request that returns a string in this exact format:
[“Jan 23 2022”]
How can I convert that string to a regular date value? Thanks!
Hope somebody can help me, I’m at a dead end. I have a RESTful sensor with a GET request that returns a string in this exact format:
[“Jan 23 2022”]
How can I convert that string to a regular date value? Thanks!
Thanks. I’ve been trying
{{ strptime(states('sensor.date'), '"%b-%d-%Y"') }}
but I get this result:
[
"Jan 23 2022"
]
I’ve also gone through the Epic Time Conversion and Manipulation Thread but I’m still at a dead end.
That was really helpful. I ended up using this template with perfect results:
{% set date_string = states('sensor.date_first_opened') %}
{% set dt = strptime(date_string[2:-2], "%b %d %Y") %}
{{ as_timestamp(dt) | timestamp_custom('%b %d %Y') }}
Thanks again!
It appears that all you actually wanted to do was eliminate the first two and last two characters from the sensor’s value.
It can be done with this:
{{ states('sensor.date_first_opened')[2:-2] }}
That’s what I thought, but I assume it would then be a text string, and I need it to be a date variable, so I can calculate for example how many days have passed since the sensor changed…
The result of AllHail’s example and my example is a text string.
I don’t know what mean by “date variable” but it’s a string. If you meant to say a datetime object, it’s not that; it’s a string.
Every entity’s state value is a string. The value’s appearance may be numeric or contain date and/or time information or even be in JSON format but its type is string. Only an entity’s attributes can have a type other than string (such as list, number, dict, datetime, etc).
My bad, and lesson learned. What I’m actually trying to do is create a sensor to calculate and show how many days have passed since the date provided by sensor.date_first_opened. I’m assuming I should use as_timestamp(now()) minus the date provided by sensor.date_first_opened and format the results to show only the number of days. But it’s getting too complicate for my limited coding abilities. Thanks for helping.
Try this template in the Template Editor:
{{ (now() - (strptime(states('sensor.date_first_opened')[2:-2], "%b %d %Y", now()) | as_local)).days }}
If it works to your satisfaction, you can use it to create a Template Sensor:
template:
- sensor:
- name: Days Elapsed
state: "{{ (now() - (strptime(states('sensor.date_first_opened')[2:-2], "%b %d %Y", now()) | as_local)).days }}"
unit_of_measurement: 'days'
To convert the GET response to a date without doing all the character stripping, and assuming the actual response contains normal double quotes not smart ones, try this in the Template Editor:
{% set sensor_output = '["Jan 23 2022"]' %}
{{ strptime(sensor_output, '["%b %d %Y"]') }}
TBH, he should just change the restful sensor to output days
value_template: "{{ (now() - (strptime(value_json | first | default, "%b %d %Y", now()) | as_local)).days }}"
@123 Your template + sensor template works perfectly.
@Troon I got unneeded extra time information (2022-01-23 00:00:00) when trying in the Template Editor.
@petro That’s a great shortcut, but I also want to keep a sensor with the “date first opened” with a clean output (Jan 23 2022).
The way I have it now, I have 3 sensors, the restful sensor that outputs ["Jan 23 2022"], the template sensor that turns it into Jan 23 2022 and the second template sensor that does the math for the number of days since then, as suggested by @123. I guess I could save one sensor with a value_template in the Restful sensor…
It’s all been great help and a lot of learning. I hope others can benefit from it. Date handling is complex for some of us. Thanks!
At a minimum, i’d peel the listed item from the state in the restful sensor so you don’t have to remove it in the template sensor.
Do you really need 3 sensors though? You are already creating a template sensor - why not just add the number of days as an attribute of the template sensor?
Yep, also a good idea.