Turn sensor entities into timestamp

Hi, I’ve been trying to turn these entities into timestamp entities for the last 8 hours so I can use them in the time automation to play audio when each entities time is reached. but haven’t had any luck.
Please help :frowning:

rest:
  method: GET
  scan_interval: 172800
  resource: "https://ezanvakti.herokuapp.com/vakitler/13978"
  sensor:
    - name: Imsak
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Imsak }} {% endif %} {% endfor %}
    - name: Ogle
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Ogle }} {% endif %} {% endfor %}
    - name: Ikindi
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Ikindi }} {% endif %} {% endfor
        %}
    - name: Aksam
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Aksam }} {% endif %} {% endfor %}
    - name: Yatsi
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Yatsi }} {% endif %} {% endfor %}
    - name: hicri
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.HicriTarihUzun }} {% endif %} {%
        endfor %}
    - name: Gunes
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ entry.Gunes }} {% endif %} {% endfor %}

Add device_class and use the today_at() function:

  sensor:
    - name: Imsak
      device_class: timestamp
      value_template: >-
        {% for entry in value_json %} {% if entry.MiladiTarihKisa ==
        now().strftime('%d.%m.%Y') %} {{ today_at(entry.Imsak) }} {% endif %} {% endfor %}

Also, rather than looping through all the entries, you could do:

      value_template: "{{ today_at(value_json|selectattr('MiladiTarihKisa','eq',now().strftime('%d.%m.%Y'))|map(attribute='Imsak')|first) }}"

1 Like

Perfect, thank you so much this was killing me not being able to work this out. I even tried chatgp without success lol
How would I make sensors that show true when the time is at the same time as these entities?
Also what is the code you have on the top in developer tools?

Desperation indeed!

Lots of ways. I’d do it like this, once you’ve installed sensor.time as described here:

template:
  - binary_sensor:
      - name: Imsak is now
        state: "{{ as_datetime(states('sensor.imsak')).strftime('%H:%M') == states('sensor.time') }}"

The {% set value_json...? That is a copy-paste of the response from your URL, in effect, pretending to be the rest sensor in order to prove out & demonstrate the template below.

1 Like

I have added these to configuration.yaml but I’m getting unavailable as a result

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
template:
  - binary_sensor:
      - name: Imsak is now
        state: "{{ states('sensor.imsak').strftime('%H:%M') == states('sensor.time') }}"
      - name: Ogle is now
        state: "{{ states('sensor.ogle').strftime('%H:%M') == states('sensor.time') }}"
      - name: Ikindi is now
        state: "{{ states('sensor.ikindi').strftime('%H:%M') == states('sensor.time') }}"
      - name: Aksam is now
        state: "{{ states('sensor.aksam').strftime('%H:%M') == states('sensor.time') }}"
      - name: Yatsi is now
        state: "{{ states('sensor.yatsi').strftime('%H:%M') == states('sensor.time') }}"

Sorry, my fault. I forgot that the rest sensors’ states are strings, and need converting with as_datetime:

template:
  - binary_sensor:
      - name: Imsak is now
        state: "{{ as_datetime(states('sensor.imsak')).strftime('%H:%M') == states('sensor.time') }}"

(went back and fixed the post above)

1 Like

Legend!
One last thing, in my dashboard I want to add them in a horizontal stack with the glance card but it is showing time remaining rather than the times. How do I get it to show the times rather than the time remaining?

Update
All good, figured it out. Just added format: time to the card under entity:
Thanks again for the fast solutions!

1 Like

@Troon hey this seems to only have worked the first day I did it. I did some home assistant updates that were pending either that day or the day after which may have effected it. Any idea why it worked the first day only? All the automations are showing last run 3 days ago.

I think you should change your templates to timestamp sensors and just use a normal time trigger.

rest:
  method: GET
  scan_interval: 172800
  resource: "https://ezanvakti.herokuapp.com/vakitler/13978"
  sensor:
    - name: Imsak
      device_class: timestamp
      value_template: >
        {% set which = 'Imsak' %}
        {{ value_json | selectattr('MiladiTarihKisa', 'eq', now().strftime('%d.%m.%Y')) | map(attribute='Ogle') | map('today_at') | first | default }}

then your trigger…

- platform: time
  at: sensor.imsak