Customizing a sensor

Hello! I have a temperature sensor:

      outdoor_temperature:
        value_template: '{{ states.weather.home_assistant.attributes.temperature }}'
        entity_id: weather.home_assistant
        device_class: temperature

And I would like my speaker to pronauns the wether when it wakes me up. So I have this:

'15517202925102':
  alias: Say test
  sequence:
    service: tts.google_translate_say
    entity_id: media_player.kabin
    data_template:
      message: "В Москве {{states('sensor.outdoor_temperature')}} градусов."

It works, but the issue is that temperature is 18.0 and it is read exactly like that. I do not need .0, how can I avoid that and hear only 18?

      message: "В Москве {{states('sensor.outdoor_temperature')|int}} градусов."

That will convert to an integer, so there won’t be any fractional part to say.

However, if you still want it to say the fractional part if the fractional part is not zero, then maybe this would be better:

      message: >
        {% set temp = states('sensor.outdoor_temperature')|float %}
        {% if temp == temp|int %}
          {% set temp = temp|int %}
        {% endif %}
        В Москве {{temp}} градусов.

This will only convert the temperature to an integer if the fractional part is zero.

1 Like

Thank you, that is what I needed. Second one is even more interesting, thank you again :slight_smile:

1 Like