Notify with Jinja stopped working

I have an automation that has been in place for a while, using the Islamic prayer time integration. I have a few template sensors setup to convert the timestamps and one that shows the current prayer time. These are used in a single automation that has been working flawlessly for a few years. Up until recently, I’m not exactly sure when, but it must have been a recent HA update, where the notify service on the automation stopped showing the prayer name in the message itself.

YAML:

# Sensors
sensor:
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Fajr"
        icon_template: mdi:weather-sunset-up
        value_template: '{{ (states("sensor.islamic_prayer_times_fajr_prayer").split("T")[1].split(":")[0]) +":"+ (states("sensor.islamic_prayer_times_fajr_prayer").split("T")[1].split(":")[1])}}'
      salah_dhuhr:
        friendly_name: "Dhuhr"
        icon_template: mdi:weather-sunny
        value_template: '{{ (states("sensor.islamic_prayer_times_dhuhr_prayer").split("T")[1].split(":")[0]) +":"+ (states("sensor.islamic_prayer_times_dhuhr_prayer").split("T")[1].split(":")[1])}}'
      salah_asr:
        friendly_name: "Asr"
        icon_template: mdi:weather-sunset
        value_template: '{{ (states("sensor.islamic_prayer_times_asr_prayer").split("T")[1].split(":")[0]) +":"+ (states("sensor.islamic_prayer_times_asr_prayer").split("T")[1].split(":")[1]) }}'
      salah_maghrib:
        friendly_name: "Maghrib"
        icon_template: mdi:weather-sunset-down
        value_template: '{{ (states("sensor.islamic_prayer_times_maghrib_prayer").split("T")[1].split(":")[0]) +":"+ (states("sensor.islamic_prayer_times_maghrib_prayer").split("T")[1].split(":")[1]) }}'
      salah_isha:
        friendly_name: "Isha"
        icon_template: mdi:weather-night
        value_template: '{{ (states("sensor.islamic_prayer_times_isha_prayer").split("T")[1].split(":")[0]) +":"+ (states("sensor.islamic_prayer_times_isha_prayer").split("T")[1].split(":")[1]) }}'
      salah_actual:
        friendly_name: Salah Actual
        value_template: >
           {%- if is_state('sensor.salah_fajr', states('sensor.time_utc')) -%}
             Fajr
           {%- elif is_state('sensor.salah_dhuhr', states('sensor.time_utc')) -%}
             Dhuhr
           {%- elif is_state('sensor.salah_asr', states('sensor.time_utc')) -%}
             Asr
           {%- elif is_state('sensor.salah_maghrib', states('sensor.time_utc')) -%}
             Maghrib
           {%- elif is_state('sensor.salah_isha', states('sensor.time_utc')) -%}
             Isha
           {%- endif -%}

# Automations
automation:
# Adhan
  - alias: Adhan
    trigger:
    - platform: time
      at:
      - sensor.islamic_prayer_times_fajr_prayer
      - sensor.islamic_prayer_times_dhuhr_prayer
      - sensor.islamic_prayer_times_asr_prayer
      - sensor.islamic_prayer_times_maghrib_prayer
      - sensor.islamic_prayer_times_isha_prayer
    action:
    - service: notify.ios_all
      data:
        message: "It is {{ states.sensor.salah_actual.state }} time, please perform wudhu."
    - service: media_player.media_pause
      data:
        entity_id:  media_player.android
    - service: switch.turn_on
      data:
        entity_id: switch.pc_mute
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room_speaker
        volume_level: 0.6
    - service: media_player.play_media
      target:
          entity_id:
            - media_player.living_room_speaker
            - media_player.kids_bedroom
      data:
        media_content_id: "http://10.10.1.200:8123/local/adhan-{{'fajr' if trigger.entity_id == 'sensor.fajr_prayer' else 'main'}}.mp3"
        media_content_type: music

The template sensor appears to be working fine as indicated in the template tester in dev tools (captured at the right time, within the minute of the sensor in question):

But the notification on the clients shows the text as “It is time, please perform wudhu.”

Other template sensors work fine in my setup and where the same notify service is used, presents the Jinja data fine.

Any ideas what why the Jinja would no longer be shown?

Thanks in advance.

I’ve not seen that syntax used. I usually use something like

message: "It is {{ states('sensor.salah_actual') }} time, please perform wudhu."

I have tried both methods (as both are defined in the Notifications doco page), both show correctly in the Template dev tools but neither work in the notify automation.

What is the state of this sensor in Developer Tools → States?

What happens if none of those if statements resolve to true?

Where is your else case to catch this?

Are there errors in your logs indicating this happened and that the sensor is now disabled?

Do this to prevent it happening:

      salah_actual:
        friendly_name: Salah Actual
        value_template: >
           {% if is_state('sensor.salah_fajr', states('sensor.time_utc')) %}
             Fajr
           {% elif is_state('sensor.salah_dhuhr', states('sensor.time_utc')) %}
             Dhuhr
           {% elif is_state('sensor.salah_asr', states('sensor.time_utc')) %}
             Asr
           {% elif is_state('sensor.salah_maghrib', states('sensor.time_utc')) %}
             Maghrib
           {% elif is_state('sensor.salah_isha', states('sensor.time_utc')) %}
             Isha
           {% else %}
             Not Known
           {% endif %}

The state of the sensor was shown in the original post, but here it is again (screenshot taken at the exact timing of the Maghrib prayer):

The state of the sensor only shows within the timing of that prayer time - e.g. Maghrib only shows when its 09:45UTC or 17:45 my time.

I could put an else statement in and would be an indicator to see if the timing is off?

Edit: Sorry, forgot to mention no errors appear in the logs either.

OK so next prayer time just occurred and it now shows ‘It is Unknown time, please perform wudhu’ on the notification, so its confirmed that its a timing thing.

Could this be because i’m using the UTC sensor reference in my templates?

I’m not sure what you think JSON is but it’s not in the examples you have posted. Perhaps you meant to say Jinja2 template?

Correct. When the current time matches any of the five prayer times, it will report the prayer’s name. This name is reported for a minute (because that’s how your template has been designed to work).

When the current time does not match any of the five prayer times, it will report whatever is in the final {% else %} section of the template. If there’s no final {% else %} section then it will report unknown.

Since sensor.salah_actual is calculated from the other sensors, that are also the trigger for the automation, it could be that sensor.salah_actual does not get updated quick enough for the notification to have the correct value, try a small 1 second delay before sending the notification in your automation actions.

Sorry yes, I meant Jinja.

Thanks for the suggestion, you might be onto something here. I will try this and report back.

Or simply change the automation’s trigger to use sensor.salah_actual.

trigger:
  - platform: state
    entity_id: sensor.salah_actual
    not_to:
      - 'unknown'
      - 'Not Known'

Thanks, this would make more sense if indeed the delay in the state update is the cause. I’m wondering what has induced this recently though, like I mentioned earlier, this has been running flawlessly for years.

I’ll test both of these and report back!

Sorted! Thanks to @codechimp for the root cause. I ended up using this approach but using to: the prayer times as a trigger and notifications are working again.

Thanks to all that assisted on this!

1 Like

I think it’s a recent change to the order of event handling and automations seem to get precedence over template sensors.
I had a similar issue with a fan automation, it was a critical automation for me so I hacked around until I got it working again without looking deeper but it was similar in its approach.

1 Like