How can I send the current date in YYYY:MM.DD format as MQTT payload?

I am using the MQTT publishing action to control a digital picture frame and that works just fine. For a date based image smart list, I now want to send the current date in “YYYY:MM.DD” format as payload.

Is this done via templating or is there an easier way? Thanks!

  action:
    - service: mqtt.publish
      data_template:
        topic: your/topic
        payload: "{{ now().timestamp() | timestamp_custom('%Y:%m.%d') }}"

1 Like

Thanks Taras, that exactly looks like what it would need!

However, when I paste the action code into YAML of a script, I get the error message “Message malformed: extra keys not allowed @ data[‘sequence’][0][‘action’]”.

Anything I am doing wrong?

I would need to see your automation. If I had to guess, it sounds like the action section has incorrect indenting.

The example I provided was intended for use in an automation. You are creating a script. Scripts don’t use the action keyword and so that’s why the error message reports “extra keys not allowed”. Remove the entire line containing action: from your script.

1 Like

I probably still get the formatting wrong…

You should know that I never use the Script Editor and prefer to create automations and scripts using a text editor. If I remember correctly, the Automation Editor does not support templates so, perhaps, neither does the Script Editor.

Only thing I can suggest is that you try variations like these and if it still fails then it may be due to the Script Editor’s lack of support for templates (perhaps someone else can comment on my assumption).

data_template:
  topic: frame/filter
  payload: "{{ now().timestamp() | timestamp_custom('%Y:%m.%d') }}"

or

topic: frame/filter
payload: "{{ now().timestamp() | timestamp_custom('%Y:%m.%d') }}"

My guess is neither of them will be acceptable to the Script Editor.

1 Like

Hurrah! This one worked:

payload_template: '{{ now().timestamp() | timestamp_custom(''%Y:%m.%d'') }}'
topic: frame/filter

Thanks so much, Taras!

1 Like

You’re welcome! Glad to hear one of those was acceptable to the Script Editor (I learned something new).

To help other users (who may have a similar question) find the answer, please mark my first post (containing the template) with the Solution tag. Only you, the author of this topic, can do that. It will automatically place a check-mark next to the topic’s title. It signals to other users that this topic has an accepted solution.

1 Like

Hello Taras, may I ask one follow-up question which I haven’t been able to solve: How can I add 7 days to the now().timestamp()?

Convert 7 days to seconds and add the result to now().timestamp()

payload_template: '{{ (now().timestamp() + 604800) | timestamp_custom(''%Y:%m.%d'') }}'
topic: frame/filter
1 Like

Perfect, that works! I tried in vain with timedelta but that wouldn’t work…

I think that the bug here is that double-quotes fails. The data_template wants single quotes in the payload!