Sensor output from english to another language for tts

The output of this

{{ relative_time(states.binary_sensor.torzustand.last_changed) }}

looks like this

45 minutes

I want to use this for a german alexa tts message. So instead of seconds, minutes, hours I need the to replace those by the german words.

If that isn’t possible, I could also only use the number and than place “Minuten” after it. Of course the output has to be in minutes than.

Any good ideas on that.

maybe @petro or @pnbruckner?

You can use |replace("old text", "new text") for this, e.g.

{{ relative_time(states.binary_sensor.torzustand.last_changed) | replace("hours", "Stunden") | replace("minutes", "Minuten") | replace("seconds", "Sekunden") }}

(Ich hoffer ich have my Deutsch nicht zu viel verlernt)

4 Likes

Thanks, your german is fine. I changed my mind for that automation.

My goal now looks a little different.

The reed sensor is attached to a post box.
Usually one time a day the post man open and close it, this will turn on and off my binary sensor.

Besides being notified about that (already have this)
I want to be able to ask alexa about the postbox.

Pseudocode:

If binary sensor was on today (since 00:00)
   Postman was here at {{ sensordate time, should output E.g. 9:33}}
If binary sensor was not on today
   Post man wasn’t here today
{{as_timestamp(states.binary_sensor.torzustand.last_changed) | timestamp_custom("%H:%M", false)}}

Thanks, and how can I do this:

If binary sensor was on today (since 00:00) ?

try this condition:

{{as_timestamp(now()) | timestamp_custom("%x", false) == as_timestamp(states.binary_sensor.torzustand.last_changed) | default(0) | timestamp_custom("%x", false)}}

If sensor’s last updated date = today’s date (regardless of time of day), it was updated sometime after midnight

Thanks, I think that would work. Unfortunately I did tell that there are two guys that open the postbox. The first one usually at around 6AM is not interesting, as he only delivers ad’s.

So I think I need the condition to only get true after lets say 8 AM (postman comes from arround 9 to open end).

Additionally I have another issue:
{{as_timestamp(now()) | timestamp_custom("%H:%M", false)}}
gives me the wrong time.

it gives me 12:30 and should be 13:30 (UTC+2).
I run hassio and my time sensor shows the right time. But the template above doesn’t.

I can’t remember which option is what, play with true/false in below line.
{{as_timestamp(now()) | timestamp_custom("%H", false) }}

I’m currently on GMT+0 so can’t test.

For your condition, try this:

{{as_timestamp(now()) | timestamp_custom("%x", false) == as_timestamp(states.binary_sensor.torzustand.last_changed) | default(0) | timestamp_custom("%x", false) and as_timestamp(states.binary_sensor.torzustand.last_changed) | default(0) | timestamp_custom("%H", false) | int >= 8}}

Ok, what does true/false stand for in that case.

Summer and winter time?

one of them is GMT, one of them is local timezone

Ok, setting it to true and it shows the right time now.

However I’m confused about the timezone setting now.
The sensor.time sensor of home assistant feeded with my location data shows the right time but tells me UTC+2.
But actually the right timezone (wintertime) would be UTC+1 currently, what explains the one hour difference in the example above when switching true/false.

So all times are right, but it seems like the sensor name UTC+2 is wrong.
Maybe because I set up this sensor when we had summertime? And afterwards the sensornamed stayed?

As long as it shows the right time, I would not worry too much about it.
“if it ain’t broke, don’t fix it” :slight_smile:

Ok than back to my issue

{{as_timestamp(states.binary_sensor.zigbee_contact.last_changed) | timestamp_custom("%H:%M", true) }}

gives 09:50

Feeding this to alexa doesn’t work as expected. She says Zero Nine … .

So I tried this:

{{as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) |timestamp_custom("%H:%M", true) | replace(‘0’, ‘’) }}

This won’t work because it cuts off all zeros.

I need somthing does this:

09:50 --> 9 Uhr 50
10:02 --> 10 Uhr 2

So only first zeros should be deleted.

and there is still the issue how to I change the condition to only get true after 8:00 Am

OK try this:

{{as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) | timestamp_custom("%H", true) | int }}:{{as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) | timestamp_custom("%M", true) }}

the condition I posted above had this for after 8AM :slight_smile:

{{as_timestamp(states.binary_sensor.torzustand.last_changed) | default(0) | timestamp_custom("%H", false) | int >= 8}}

Ah I missed that, ok will try both. Thanks

I think I have to add int for the minutes too, because 09:08, will get 9:08 right? I think I got everything to make it work now. Will post a working script when its ready.
Nevertheless other questions could appear :wink:

1 Like

Ok, thank I think this will work now:

routine_postkasten:
  sequence: 
    - service: media_player.alexa_tts
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}'
        message: >
          {% if as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) | default(0) | timestamp_custom("%H", true) | int >= 8 %}
            Post war heute um {{as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) | timestamp_custom("%H", true) | int }} Uhr {{as_timestamp(states.binary_sensor.zigbee_postkasten_contact.last_changed) | timestamp_custom("%M", true) | int }} da
          {% else %}
            Post war heute noch nicht da
          {% endif %}

With this I’m able to ask Alexa if the postman was already here.

Danke @lolouk44 :wink:

Ok, so I tried it out now and it basically works, but after restarting ha the last_changed value was changed too.

So this will not work reliable when I restart my ha.
Is there a way to make it look when it was last 'on'?

I have the same issue. When restarting HA all last_changed and last_updated attributes are reset.
Only way round would be to have an automation that updates a template sensor that tracks that data, you could try something like (not tested):

- platform: template
  sensors:
    zigbee_postkasten_contact_last_changed:
      entity_id: sensor.zigbee_postkasten_contact
      value_template: >-
        {% if (states("sensor.uptime") | float > 0.1)%}{{states.binary_sensor.zigbee_postkasten_contact.last_changed}}{% else %}states("sensor.zigbee_postkasten_contact_last_changed"){%endif%}

and use sensor.zigbee_postkasten_contact_last_changed in your above script

Thanks I will try that, but I also like too know if @petro knows a way to look for the last ‘on’ state of a sensor in a template.

Unfortunately this did not work.
The state of that sensor looks like this:

states("sensor.zigbee_postkasten_contact_last_changed")