Unable to format timestamp sensor in home assistant / hass.io using strptime function - home-assistant

I am trying to display a sensor state value, which is of type 'timestamp' formatted as %H:%M but not able to get the strptime function to work the way I expect it.
e.g. in the Dev Tools when i add the below to the 'TEMPLATE' section
sunrise {{ strptime(state_attr("sun.sun", "next_rising"), "%H:%M") }}
it still displays the output as:
sunrise 2020-01-20T19:05:26+00:00

Per Home Assistant Templating documentation:
strptime(string, format) will parse a string to a datetime based on a format.
So that's not exactly what you need.
You may want to apply timestamp_custom(format_string, local_boolean) filter to convert a timestamp to a custom format, e.g:
{{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom('%H:%M') }}

Related

Different values for same object member in Angular

the following function receives an event from the Angular Material Design DatePicker and shall print it to the console:
applyFilter(event: MatDatepickerInputEvent<Date>) {
console.log(`${event.value}`); // prints the selected date
console.log(event.value); // prints some weird object
}
But they are each printing a different output to the console.
The first prints the actual selected date, the second one prints some weird object to the console.
This is the console output:
This is my DatePicker:
<mat-form-field>
<mat-label>Anfangsdatum</mat-label>
<input matInput (dateInput) = "applyFilter($event)" [matDatepicker] = "startDatePicker">
<mat-datepicker-toggle matSuffix [for]="startDatePicker"></mat-datepicker-toggle>
<mat-datepicker #startDatePicker></mat-datepicker>
</mat-form-field>
I just want to get the date the user picked using the DatePicker from Angular Material Design, but it seems like that this overly complicated. Can someone please help me?
You are seeing two different values.
When you call console.log(new Date()) you will get the actual date object. (replace new Date() with any date object such as your event.value)
This object is the weird object that you speak of. It's simply a Date which has all kinds of methods available to help you. But in the end, all a Date is actually is a Number that represents milliseconds since 1 January 1970 UTC, then a bunch of helper functions with it.
One of those helper functions is .toString().
When you call console.log(`${new Date}`); you are creating a date object, then putting it inside a template literal (the backticks). This template literal calls the .toString() of the date object to convert it to a string, as you are asking for a string with a template literal. So you get the string representation of the Date object. (It's actually a bit more complex than that per this post, it's not always the toString() method per this post).
So if you want the string value, use the template literal or just call event.value.toString(). If you want the date object so you can do things with it (use the full date object and all its abilities per the documentation linked at the top)... use the date object. If you have other questions, feel free to comment.
Edit:
I don't think it's the actual date object. Check out the API. You are getting the object you are expecting, but it may be the one described in this API. I noticed they both have the add method in the prototype. Either way, it's an object vs the string representation. I tried to find the type definition since I'm assuming you are in Typescript, that should list all the methods available to you.

Is it possible to write date as DateTime format in JSON using Python without converting it to string

I want to know if there is a way to write the date as DateTime format in JSON.
I have followed so many links on the internet but everywhere date is converted to string(str) in order to write it on JSON file.
I used the below code:
import json
fileName='json_output.json'
def writeToJSONFile(data):
with open(fileName, 'a+') as fp:
json.dump(data, fp, indent=4, default=str)
then calling it as :
from datetime import datetime
date_value="09-23-2019"
date_time = datetime.strptime(date_value,'%m-%d-%Y')
date_dict={"eventDate":date_time}
writeToJSONFile(date_dict)
The above code is able to write date into the JSON file but in the string format.
I have already went through link:
How to overcome "datetime.datetime not JSON serializable"?
JSON datetime between Python and JavaScript
I just want to know if it is possible or not at all possible to store date as datetime format?
Simple answer is No, JSON does not have a native datetime representation, so it will appear as a string; however, if you use a standard that is agreed upon, the receiving application can parse the variable into a datetime object, if they choose to. If you do not know what format they may agree with, I would recommend just making it a standard ISO 8601(Combined date and time in UTC), that way it can be parsed by the receiver, and retain the correct value regardless of time zone.

Understanding the Result from openweathermap in Json format

I have to get weather forecast for 7 days of a particular location(with postal code 94042). For this I make a HTTP request like this:
http://api.openweathermap.org/data/2.5/forecast/daily?q=94042&mode=json&units=metric&cnt=7
The result of the query is a long string in Json format. I did not post the result as it is messy, you can click the link to view the result:
There are some parts of the result about which I am confused.
What does the dt field in the beginning of every element of the array list signify?
What does the second last field deg of every element in the array list signify?
What does the last field clouds followed by an integer signify? I thought description about clouds was covered in the main field?
Explanation of the parameters: http://openweathermap.org/weather-data#current
dt: Data receiving time (in unix, UTC format). dt is the time of data receiving in unixtime GMT (greenwich mean time). To convert this value, use a 3rd-party library like momentjs.com or refer to this answer on how to convert it: https://stackoverflow.com/a/847196/3412545
deg: Wind direction (in degrees, meteorological)
clouds: cloudiness also known as cloud cover (in %)

Google Charts json date format

I am using Google Charts and trying to generate my own JSON format to render the chart rather than using the libraries. All is well except for trying to figure out how to represent a date format in json that google chart will understand...
Spec:
JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based.
Reference:
https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable
Reading the above spec would seem to indicate that having the date format represented in json as Date(year, date, month) would work but this does not appear to work for me.
Error:
Uncaught Error: Type mismatch. Value Date(2012, 10, 3) does not match type date in column index 0
json response:
{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)",4.0,4.0,69],["Date(2012, 10, 4)",4.0,4.0,69]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}
Nothing is jumping out at me as this should be following the spec required format. What am I missing?
your syntax is wrong, i think...
you should try something like that:
{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)"]],["Date(2012, 10, 4)"]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}
for me, this code worked:
{"c":[{"v":"Date(2012,11)"},{"v":6657}.....
but this is to change the month, not the day and using json....

What type of date/time format is this?

I'm currently attempting to use HighStock charts on my site. I'll be using a PHP file to generate the data for the chart, my only question is what date/time format does HighStock use? Here's an example file they have on their site: http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json
[1107129600000,38.45]
Above is a line of data in the json file where 1107129600000 is the date and 38.45 is the stock price. Hopefully someone will know the date/time type. Thanks.
It looks like a UNIX timestamp. It's the number of seconds(or in your case, milliseconds) since January 1 1970. Most programming languages will let you convert this into more natural date/time formats.
Here's a tool to help you convert to see for yourself: http://www.epochconverter.com/
Further reference: http://en.wikipedia.org/wiki/Unix_time