Timestamps handled differently

Dear community,
up to now I found all answers to my questions in this forum. Thanks very much for that great support from all of you guys.
Now it is the first time were I could not find a solution:

I want to have the sun-rise and sun-set time always for the current day only (that it can be displayed in a graph via apexcharts).

For this I created following template sensor:

- sensor:
    - name: Zeit Sonnenuntergang
      unique_id: TempSensTimSunSet
      state: >
        {%- set ts_sunset = as_timestamp(state_attr("sun.sun", "next_setting")) %}
        {%- set ts_nextday = as_timestamp(as_datetime(states("sensor.date")) + timedelta(days=1)) %}
        {%- if ts_sunset > ts_nextday %}
          {%- set ts_sunset = as_timestamp(as_datetime(ts_sunset) - timedelta(days=1)) %}
        {%- endif %}
        {{ as_timestamp(as_datetime(ts_sunset))|timestamp_local() }}
      device_class: "timestamp"

The calculation is working fine (I know at some time the values are a few seconds away from the real values after the sun-sensor updates, but no one sees that in the graph.)
In the template-section of developer-tools I see:


When I look to states of developer-tools I see:


The two hours difference in the string is not nice, as I want to access the value also via JavaScript in Apexcharts by doing a string-operation.

I hope you can provide good hints for solving the topic.


Those times are identical, one is represented in your local timezone (the +2), and one is represented in UTC (+0). JS will understand both, there’s nothing to worry about.

Thanks for your answer @petro.
Yes, indeed the timestamps are the same and also in Lovelace everything is displayed correctly.

My issue is: I will do a string operation on the second one. And the string shows me “2 hours earlier”. This is the instruction:


I hope I could explain it better now.

Don’t do that. Turn it into a date object.

var date = new Date(states["sensor.zeit_sonnenuntergang"].state);
return date.toLocaleTimeString();

Perfect @petro: It works! Thanks for your support.

This is the card I could create now.