Pic out timestamp from mqtt value template Weather Display. How to do?

I use latest HA
Home Assistant 2023.8.3 Supervisor 2023.08.1 Operating System 10.4 Frontend-version: 20230802.1 - latest

I make mqtt sensors to pic out values from my Weather Display software (on another computer)
It works good with temperatures value. But if i want values as time and date i got Unknown value.

My sensor:

- name: "WD Moonset"
      icon: mdi:theme-light-dark
      state_topic: "moonset"
      unit_of_measurement: ''
      value_template: '{{ value.split("{")[1].split("}")[0].strip() }}'

When i use MQTT explorer i got the value, but i dont know how i should pick it out in my value template.

Hope for help…


I think you need to escape the brackets in your template.

I’m not 100% sure if it will work but try this:

value_template: '{{ value.split("\{")[1].split("\}")[0].strip() }}'

Thx for your answer.
Sorry, it will not work.
Here is the logg from HA:

Logger: homeassistant.helpers.template
Source: helpers/template.py:735
First occurred: 14:10:59 (7 occurrences)
Last logged: 14:12:51

Template variable error: list object has no element 1 when rendering '{{ value.split("\{")[1].split("\}")[0].strip() }}'

Remove this line:

      unit_of_measurement: ''

and use the template shown below:

    - name: "WD Moonset"
      icon: mdi:theme-light-dark
      state_topic: "moonset"
      value_template: '{{ value[3:8] }}'

The template assumes the time is always reported using 4 digits with a colon separator.

The template won’t work correctly if the time is shown using 3 digits (like 5:45 instead of 05:45).

Works like a charm!!!

But how i do with this value, or the another ones with dates and % ?

The technique I used is described here:

It’s useful only if the value you want to extract always has the same length. If the length can vary (for example, a date can vary in length) then you must use a different technique. I suggest something like this:

    - name: "WD Next Full Moon"
      state_topic: "nextfullmoon"
      value_template: "{{ value.replace('WD{ ', '').replace('}', '') }}"

It simply replaces {WD and } with nothing. Therefore it will change this:

WD{ 31 augusti 2023}

to this:

31 augusti 2023

Like a king!!!

Thank you!!!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.