Refresh service data and send notification

Hello to all HAs

Newly using Home Assistant, I enjoy creating automations.

And I actually created one which should generate a daily notification with the namesday.

To achieve this, I first created two REST calls:

- resource: https://nameday.abalin.net/api/V1/today?country=fr&timezone=Europe/Paris
  scan_interval: 172000
  sensor:
    - name: NameDay Today
      unique_id: nameday_today
      value_template: "{{ value_json['nameday']['fr'] }}"

- resource: https://nameday.abalin.net/api/V1/tomorrow?country=fr&timezone=Europe/Paris
  scan_interval: 172000
  sensor:
    - name: NameDay Tomorrow
      unique_id: nameday_tomorrow
      value_template: "{{ value_json['nameday']['fr'] }}"

Then an automation triggers one hour every day which calls the update_entity service to update my entities and which then sends a notification to my devices:

- id: '1698232436488'
  alias: NameDay
  description: ''
  trigger:
  - platform: time
    at: 07:00:00
  condition: []
  action:
  - service: notify.notifybenjy
    data:
      message: "{{ date }}\r\n{{ namedaytoday }}\r\n{{ namedaytomorrow }} {{ states.sensor.nameday_today.last_updated
        }}"
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id:
      - sensor.nameday_today
      - sensor.nameday_tomorrow
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    enabled: false
  - service: notify.notifybenjy
    data:
      message: "{{ date }}\r\n{{ namedaytoday }}\r\n{{ namedaytomorrow }} {{ states.sensor.nameday_today.last_updated
        }}"
  variables:
    date: Nous sommes le {{ states('sensor.date_du_jour') }}
    namedaytoday: Nous fêtons les {{ states('sensor.nameday_today') }}
    namedaytomorrow: Demain nous fêterons les {{ states('sensor.nameday_tomorrow')
      }}
  mode: single  

All of this works very well except that the notification I receive in the morning contains the data from the day before.

It is only when I restart the automation a second time that the data is up to date.

I suspect that the call to the update_entity service followed by the notification does not update the data.

How can I ensure that my notification contains the new updated data?

PS: I already tried to add a delay between calling the service and sending the notification, it doesn’t change anything :frowning:

Thank you for your help !

Little up.
no one knows how to help me? :frowning:

The scan interval for your REST calls is nearly 48 hours… If you run this automation daily with the update entity service calls, that means the scan interval is never being reached. You have two options:

  1. Start the automation by updating the entities
  2. Set scan interval to something more reasonable (like 12 hours) and it should work and the entity update service calls will likely be unnecessary.

This is normal, since it is my automation which updates the sensors and sends the notification.

This is already what I do, and this is the problem I have. The service refreshes the sensors and notify but the notification contains the old data.

You aren’t updating the variables that you are using for the message. Either copy or move the variables to the Action section after the service call to update the sensor so the updated values are applied.

- id: '1698232436488'
  alias: NameDay
  description: ''
  trigger:
  - platform: time
    at: 07:00:00
  condition: []
  action:
  - service: notify.notifybenjy
    data:
      message: "{{ date }}\r\n{{ namedaytoday }}\r\n{{ namedaytomorrow }} {{ states.sensor.nameday_today.last_updated}}"
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id:
      - sensor.nameday_today
      - sensor.nameday_tomorrow
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - variables:
      namedaytoday: Nous fêtons les {{ states('sensor.nameday_today') }}
      namedaytomorrow: Demain nous fêterons les {{ states('sensor.nameday_tomorrow')}}
  - service: notify.notifybenjy
    data:
      message: "{{ date }}\r\n{{ namedaytoday }}\r\n{{ namedaytomorrow }} {{ states.sensor.nameday_today.last_updated}}"
  variables:
    date: Nous sommes le {{ states('sensor.date_du_jour') }}
    namedaytoday: Nous fêtons les {{ states('sensor.nameday_today') }}
    namedaytomorrow: Demain nous fêterons les {{ states('sensor.nameday_tomorrow')
      }}
  mode: single

Thanks !
It’s ok :slight_smile: