Date formatting

Thanks @petro I’m going to use that template…

1 Like

With the change for a required entity in templates, what should the entity be for these sensors?

They show up correctly on the front-end, but HA is trowing errors about “has no entity ids configured to track nor were we able to extract the entities to track from the value template(s)”.

You can put in any entity_id that updates more often than what you want to show.

so in the cases above you could use sensor.date as it updates every day or even sensor.time since it updates every minute.

@finity Thanks for the response, I ended up using “sensor.date”, all good now.

I’ve just come across this and thought I’d add what I use. It is just a variation on the theme but it might be interesting to someone. Also I suspect an even better solution could be arrived at by combining (some of) this with (some of) @petro’s version.

I use mine with device_trackers to report a persons current address and how long they have been there for.

{% set since_day = as_timestamp(state_attr('device_tracker.life360_person1', 'at_loc_since')) | timestamp_custom('%d') | int %}

{% set since_day_name = (as_timestamp(state_attr('device_tracker.life360_person1', 'at_loc_since')) | timestamp_custom('%A')) %}

{% if since_day == now().day %}
  {% set since_day = '' %}
{% elif since_day == now().day - 1 %}
  {% set since_day = ' yesterday' %}
{% elif since_day < (now().day - 6) and since_day > (now().day - 14) %}
  {% set since_day = ' last ' + since_day_name %}
{% elif since_day <= (now().day - 14) %}
  {% set since_day = ' on ' + as_timestamp(state_attr('device_tracker.life360_person1', 'at_loc_since')) | timestamp_custom('%d %b') %}
{% else %}
  {% set since_day = ' ' + since_day_name %}
{% endif %}

{% set since_time = as_timestamp(state_attr('device_tracker.life360_person1', 'at_loc_since')) | timestamp_custom('%H:%M') %}
                  
{% set composite_state = states('device_tracker.person1_composite') | title %}
{% if composite_state in ['Home', 'Zone2', 'Zone3'] %}
  Person1 has been at {{ composite_state }} since {{ since_time }}{{ since_day }}
{% else %}
  {% set address = state_attr('device_tracker.google_maps_xxxx', 'address') %}
  Person1 has been at {{ address }} since {{ since_time }}{{ since_day }}
{% endif %}

Results in things like;

Person1 has been at 23, My Street, My Town, MyCity a12 3bc, UK since 09:43
Person1 has been at Home since 17:41 yesterday
Person1 has been at Zone2 since last Friday

1 Like

@petro
thanks petro, i had a problem in my code which is " timer is not working at the date time i put"

here is the code:
configuration:

  • platform: template # determine if today is selected as a watering day for program 1
    sensors:
    program1_watering_day:
    entity_id: input_boolean.program1_monday, input_boolean.program1_tuesday, input_boolean.program1_wednesday, input_boolean.program1_thursday, input_boolean.program1_friday, input_boolean.program1_saturday, input_boolean.program1_sunday
    value_template: >
    {% set sensor_names = [“monday”, “tuesday”, “wednesday”,“thursday”,“friday”,“saturday”,“sunday”] %}
    {% set today_name = sensor_names[now().weekday()] %}
    {% set entity_id = ‘input_boolean.program1_’+today_name %}
    {{ is_state(entity_id, ‘on’) }}

automation:

  • alias: Reticulation Run Program 1 # start program 1 at designated time if it is enabled and today is selected as a watering day
    initial_state: ‘on’
    trigger:
    • platform: template
      value_template: >
      {{ now().strftime(’%d %H:%M:00’) == states(‘input_datetime.program1_start_time’) }}
      condition:
      condition: and
      conditions:
      • condition: state
        entity_id: input_boolean.program1_enable
        state: ‘on’
      • condition: state
        entity_id: sensor.program1_watering_day
        state: ‘true’
        action:
    • service: script.turn_on
      entity_id: script.program1_run

script:
program1_run: #run retic program 1 through each station for selected time
alias: Program 1 Run
sequence:
- service: switch.turn_on
data:
entity_id: switch.station_1_valve
- delay: “00:{{ states(‘input_number.program1_station1_run_time’)|int }}:00”
- service: switch.turn_off
data:
entity_id: switch.station_1_valve
- service: switch.turn_on
data:
entity_id: switch.station_2_valve
- delay: “00:{{ states(‘input_number.retic_program1_station2_run_time’)|int }}:00”
- service: switch.turn_off
data:
entity_id: switch.station_2_valve

You’ll have to format that properly. Edit, highlight, and press the code format button.

could you help me in that problem, I think the template got a problem

Read my last post. You need to format it properly for me to see the problems. I can’t read the code if the code isn’t formatted. Take a look at your post, notice bullets and odd spacing? That’s because you didn’t format the post.

aha, i got it, i will format and resend
thanks

Don’t copy from that post, either edit that post or make a new one copying from you config files. You will break the format if you copy from that post.

ok, i will do

Hi, I used your example, and it works great, but now it is 16.4. and sensor is showing me 14.4. I think it has stopped on that date. Do you know how to solve it to continue?

my code:

  - platform: template
    sensors:
      date_long: 
        friendly_name: 'Datum'
        value_template: >
          {% set months = ["januar", "februar", "marec", "april", "maj", "jun", "jul", "august", "september", "oktober", "november", "december"] %}
          {% set month = months[now().strftime('%m') | int -1] %}  
          {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}

you need to add the entity_id of the changing sensor. depending on which you have defined in your config, add:

  - platform: template
    sensors:
      date_long: 
        friendly_name: 'Datum'
        entity_id: sensor.date, # or sensor.date_time, or sensor.time_date
        value_template: >
          {% set months = ["januar", "februar", "marec", "april", "maj", "jun", "jul", "august", "september", "oktober", "november", "december"] %}
          {% set month = months[now().strftime('%m') | int -1] %}  
          {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}
1 Like

Hello how you add the icons??..

after the line containing “value_template:”, add “icon_template: mdi:weather-sunset-down” for example… You can also add some customization in the “customize.yaml” file for the corresponding sensor.

Can someone help me to localize this?

  life360_naam_lastseen:
    friendly_name: "Naam voor het laatst gezien"
    value_template: "{{ as_timestamp(state_attr('device_tracker.naam', 'last_seen')) | timestamp_custom('%A om %-H:%M') }}"

  life360_naam_since:
    friendly_name: "Naam's Locatie Sinds"
    value_template: "{{ as_timestamp(state_attr('device_tracker.naam', 'at_loc_since')) | timestamp_custom('%A om %-H:%M') }}"

Outcome now is Englisch dayname with correct time, but I want the Dayname in Dutch so any help is welcome

You can’t use %A then, the python version that’s used is English. e.g. all items passed through strftime will be in English. You’re better off making a map like what is outlined above.

Thank for the quick reply, but I’m just a noob. Can you please give me a hint how to do?