Automation not triggering from rest api data

I am using rest api to retrieve time for 5 different instances. Then using trigger template to set automation for the same time retrieved from the api. Automation not triggering when the time comes.


code for trigger template:

{{ states('sensor.time') == (as_timestamp(sensor.dhuhr_time) |int -60)| timestamp_custom('%H:%M', True) }}'

Previously used this code also which didnt work either:

{{now().strftime("H:%M") == (((state_attr('"sensor.asr_time', 'Time in Milliseconds') | int / 1000) + 0*60 ) | timestamp_custom('%H:%M'))}}

Use the time trigger set it to sensor/input datetime and add the entity.

And please don’t post images of text.
It’s very hard to read.

It says no matching entities found

I am using the following code:

{{now().strftime("%H:%M") == (((state_attr('sensor.asr_time', 'Time in Milliseconds') | int / 1000) + 0*60 ) | timestamp_custom('H:%M'))}}

but my sensor only shows time in 24hour format. Should i remove the timestamp_custom part? Thank you

Use the template tool and see what each side of the == gives you as output

Based on the information you have provided, this should work for a Template Trigger:

trigger:
  - platform: template
    value_template: "{{ now().strftime('%H:%M') == states('sensor.asr_time') }}"

the output is false

I will try and tell you

That is not what I asked for.

I asked for

{{ now().strftime('%H:%M') }}
{{ states('sensor.asr_time') }}

Perhaps that was unclear.

Result type string and its showing time

Well of course it’s false. It will only be true when the current time is exactly the same as today’s Asr Time. That’s the purpose of using it in a Template Trigger.

If you’re interested, here’s a completely different, and more efficient, way to do it.

The following configuration defines six RESTful Sensors. The first five sensors show each of the five prayer times as timestamps (meaning they look like this: 2022-01-21T20:31:00+00:00). This is important because it means they can be used in a Time Trigger. The sixth sensor is included for convenience and it shows the five prayer times (as attributes) in the traditional HH:MM format.

rest:
  - resource: http://api.aladhan.com/v1/timingsByCity?city=Lahore&country=Pakistan&method=1&school=1
    scan_interval: 3600
    sensor:
      - name: "Fajr"
        value_template: "{{ today_at(value_json.data.timings.Fajr) }}"
        device_class: timestamp
      - name: "Dhuhr"
        value_template: "{{ today_at(value_json.data.timings.Dhuhr) }}"
        device_class: timestamp
      - name: "Asr"
        value_template: "{{ today_at(value_json.data.timings.Asr) }}"
        device_class: timestamp
      - name: "Maghrib"
        value_template: "{{ today_at(value_json.data.timings.Maghrib) }}"
        device_class: timestamp
      - name: "Isha"
        value_template: "{{ today_at(value_json.data.timings.Isha) }}"
        device_class: timestamp
      - name: "Prayer Times"
        json_attributes_path: "$.data.timings"
        value_template: "OK"
        json_attributes:
          - Fajr
          - Dhuhr
          - Asr
          - Maghrib
          - Isha

Now if you want an automation to trigger at all five prayer times you can simply use a single Time Trigger:

alias: example
trigger:
  - platform: time
    at:
      - sensor.fajr
      - sensor.dhuhr
      - sensor.asr
      - sensor.maghrib
      - sensor.isha
condition: []
action:
 ... whatever it is you want to do ...

If you want to display the prayer times in an Entities Card, here’s the required configuration:

type: entities
entities:
  - entity: sensor.prayer_times
    name: Fajr
    icon: mdi:clock
    type: attribute
    attribute: Fajr
  - entity: sensor.prayer_times
    name: Dhuhr
    icon: mdi:clock
    type: attribute
    attribute: Dhuhr
  - entity: sensor.prayer_times
    name: Asr
    icon: mdi:clock
    type: attribute
    attribute: Asr
  - entity: sensor.prayer_times
    name: Maghrib
    icon: mdi:clock
    type: attribute
    attribute: Maghrib
  - entity: sensor.prayer_times
    name: Isha
    icon: mdi:clock
    type: attribute
    attribute: Isha

4 Likes

Thank you for being so kind and taking the time to carefully explain everything

2 Likes

thank you :+1: :+1: