How to insert timestamp in Notification Message as part of a Script?

Hey all, looking for some help getting my script to send a timestamp along with it’s notification message. The below script works perfectly. It toggles an MQTT switch, and then waits 5 seconds, and sends a notification to the iOS App, letting me know the new state of the switch:

This script: works:

vent_mbr_toggle:
  sequence:
    - service: switch.toggle
      entity_id: switch.vent_mbr
    - delay: 00:00:05 #added so system has time to switch the state before alerting on it below
    - service: notify.ios_sc_phone_hassapp
      data_template:
        title: "Master Bedroom Vent Status"
        message:  "is now {{ states.switch.vent_mbr.state }}."

This script: errors out:

vent_mbr_toggle:
  sequence:
    - service: switch.toggle
      entity_id: switch.vent_mbr
    - delay: 00:00:05 #added so system has time to switch the state before alerting on it below
    - service: notify.ios_sc_phone_hassapp
      data_template:
        title: "Master Bedroom Vent Status"
        message:  "{{ now().strftime("%H:%M %Y-%m-%d") }}: is now {{ states.switch.vent_mbr.state }}."

{{ now().strftime("%H:%M %Y-%m-%d") }} is the only change in the second example (in the message: part). I am using the same exact format in some notifications done through the Alert: component and they work just fine (see examples below). I DO have the time_date platform loaded in my config.

Two questions:
1. Why this doesn’t work with scripts?
2. How can I get a timestamp to show in my script based notification? is there a different format?

TIA!!


Example of working timestamp in an alert: based notification:

garage_door:
  name: '{{now().strftime("%H:%M %Y-%m-%d")}}: Garage door has been OPEN for {{ relative_time(states.cover.garage_door.last_changed) }}.'
  entity_id: cover.garage_door
  state: 'open'
  repeat:
  - 15
  - 30
  - 60
  - 120
  can_acknowledge: True
  skip_first: True
  done_message: '{{now().strftime("%H:%M %Y-%m-%d")}}: Garage door is now closed :)'
  notifiers:
    - ios_sc_phone_hassapp

If you use double quotes you should wrap them in single quotes like in your working alert.

message:  '{{ now().strftime("%H:%M %Y-%m-%d") }}: is now {{ states.switch.vent_mbr.state }}.'

should work.

2 Likes

Darn quotes! :rage:

I’ve just tried it as you suggested and it works like a charm. I obviously wasn’t paying attention to my quotes. Thank you for the learning experience! :blush:

1 Like

while parsing a block mapping
in “/home/homeassistant/.homeassistant/automations.yaml”, line 13, column 10
expected , but found ‘’
in “/home/homeassistant/.homeassistant/automations.yaml”, line 14, column 68

my automations.yaml part with pushbullet message:

     message: "Nekdo zvoni v {{ now().strftime(”%H:%M %Y-%m-%d") }}"

OK this works:

message: ‘Nekdo zvoni v {{ now().strftime(“%H:%M”) }}’

2 Likes

Sorry for reviving an old thread, but is there anyway to show the time in a notification in 12hr format? (ex. 4:00pm rather than 16:00)

{{ now().strftime("%r %Y-%m-%d") }} or
{{ now().strftime("%I:%M %p %Y-%m-%d") }}
https://www.tutorialspoint.com/python/time_strftime.htm

1 Like

Thank you, sir!!