How to edit this simpler?

Hello,

I created a sensor :

  sensor:
  - name: 'Sunset'
    unique_id: 'sensor_sunset'
    state: "{{ state_attr('sun.sun', 'next_setting') | as_datetime }}"
    device_class: timestamp
  - name: 'Sunrise'
    unique_id: 'sensor_sunrise'
    state: "{{ state_attr('sun.sun', 'next_rising') | as_datetime }}"
    device_class: timestamp

I want to edit it as that :

Zonsopgang : 25/2/2022 om 07:40:37
Zonsondergang : 24/2/2022 om 18:19:36

So I did this :

Zonsopgang        : {{ as_timestamp(states('sensor.sunrise') | as_datetime | as_local, 0)| timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S') }}
Zonsondergang  : {{ as_timestamp(states('sensor.sunset') | as_datetime | as_local, 0)| timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S') }}

This works fine, but I presume there is a simpler way to do that… ?

If you are living in the Amterdam timezone, the times for sunrise and sunset are wrong. They look to be the Londen timezone.

Without the sensor Sunset and Sunrise this works for me

Zonsopgang     : {{ state_attr('sun.sun','next_rising') | as_timestamp | timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S',False) }}
Zonsondergang  : {{ state_attr('sun.sun','next_setting') | as_timestamp | timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S',False) }}

result:

Zonsopgang     : 25/2/2022 om 06:30:24
Zonsondergang  : 24/2/2022 om 17:02:12

I live in Belgium, so my output is correct

Schermafbeelding 2022-02-24 174707

But I see this error in the log :

Template warning: ‘timestamp_custom’ got invalid input ‘07:30:00’ when rendering template ‘Zonsopgang : {{ as_timestamp(states(‘sensor.sunrise’) | as_datetime | as_local, 0)| timestamp_custom(’%d/%-m/%-Y ‘‘om’’ %H:%M:%S’) }}

Sorry, you are right. Changing the ‘local’ parameter to True in the timestamp_custom filter, gives the right local times:

Zonsopgang     : {{ state_attr('sun.sun','next_rising') | as_timestamp | timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S',True) }}
Zonsondergang  : {{ state_attr('sun.sun','next_setting') | as_timestamp | timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S',True) }}

result:

Zonsopgang     : 25/2/2022 om 07:30:24
Zonsondergang  : 25/2/2022 om 18:04:06

You don’t need the two extra sensors, the info is already available in the sun integration.

Indeed, I don’t need these sensors.

And how would you edit this sensor :

  - name: 'Sfeer morgen'
    unique_id: 'sensor_sfeer_morgen'
    state: >
      {{ state_attr('sun.sun', 'next_rising') | as_datetime + timedelta(minutes = states('input_number.number_offset_sfeer_morgen')|int(0)) }}
    device_class: timestamp

I did it like that :

Sfeer morgen      : {{ as_timestamp(states('sensor.sfeer_morgen') | as_datetime | as_local, 0)| timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S') }}

To get the local time use, delete ‘| as_local ’ and add ’ True’ as second parameter in the timestamp_custon filter.

OK, thanks !

Sfeer morgen      : {{ as_timestamp(states('sensor.sfeer_morgen') | as_datetime, 0)| timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S', true) }}

But still got the error :roll_eyes:

Template warning: 'timestamp_custom' got invalid input '07:30:00' when rendering template 'Zonsopgang : {{ state_attr('sun.sun','next_rising') | as_timestamp | timestamp_custom('%d/%-m/%-Y ''om'' %H:%M:%S',True) }}

I can’t reproduce your warning. Perhaps somebody else can help.

Works for me too: