Custom state for sensor ON OFF in Telegram message

Hello There!

So I’ve been using Home Assistant for a couple of months now and this is definitely the best, most user-friendly solution for home automation projects by far - thank you!

Now to the topic:

I have set up a Telegram bot which works very much like it should, but have come across one “problem” that kind of annoys me.

This is an Automations that sends me a message which informs me of the water-level in my basement “well” - especially in the winter when there’s a lot snowmelt, it can happen that it owerflows. I already have an automation that starts the emergency pump if the level is critical and sends a message if it doesn’t go back to normal within 3 minutes, but being able to check the waterlevel independently is a nice bonus.

I have following automation set up:

- id: '584720367256842'
  alias: Status
  hide_entity: true
  trigger:
  - event_data:
      data: "/schacht1"
    event_type: telegram_callback
    platform: event
  action:
  - service: telegram_bot.send_message
    data_template:
        target: '{{ trigger.event.data.user_id }}'
        title: '*Wasserstand Schacht Keller*'
        message: " Der Wasserstand ist: *{{ states.binary_sensor.schacht_wasserstand_alarm.state }}* 
          
          Der kritische Wasserstand wurde zuletzt *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht "
        disable_notification: true

which results in the message:

Wasserstand Schacht Keller
Der Wasserstand ist: off
Der kritische Wasserstand zuletzt 11.01.2019 um 23:21 erreicht

so far so good. BUT I would like it to say:

Wasserstand Schacht Keller
Der Wasserstand ist: OK [or “Kritisch”]
Der kritische Wasserstand zuletzt 11.01.2019 um 23:21 erreicht

So instead of the sensor binary_sensor.schacht_wasserstand_alarm saying on or off, I would like it so say OK or Critical

Is this possible in a simple way (like the custom time stamp for example)?

Thanks in advance

I have a follow up question:

Is there a way to change this part

{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}

so it doesn’t say the “last changed” date, but the actual “last ON” date?! “last changed” also shows the time when I restart HA - which is kind of pointless, because I only care when the water level was actually dangerously high for the last time.

You can use templating. I’m not super experienced with the templating (if you use AppDaemon, I can give more succinct suggestions), but perhaps something like:

  - service: telegram_bot.send_message
    data_template:
        target: '{{ trigger.event.data.user_id }}'
        title: '*Wasserstand Schacht Keller*'
        message: >
        {%- if is_state('states.binary_sensor.schacht_wasserstand_alarm.state', 'off') -%}
            " Der Wasserstand ist: OK
            Der kritische Wasserstand wurde zuletzt *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht "
        {%- else -%}
            " Der Wasserstand ist: Kritisch
            Der kritische Wasserstand wurde zuletzt *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht "
        {%- endif -%}

I’m not sure about the timestamp for on only, but I’m almost certain you can template you way out of that as well.

Thanks! That seems to be exactly what I’m looking for!

However, there appears to be a small error: I always get the “else” message :thinking:

Also is it possible that the quotes are not necessary?! They also appear in the message.

Wasserstand Keller Schacht
“Der Wasserstand ist: Kritisch
Der kritische Wasserstand wurde zuletzt 12.01.2019 um 14:04 erreicht”

And I edited the indentation, because the configurator didn’t like it your way xD

message: >
  {%- if is_state('states.binary_sensor.schacht_wasserstand_alarm.state', 'off') -%}
      Der Wasserstand ist: *OK*
      Der kritische Wasserstand wurde zuletzt am
      *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht
  {%- else -%}
      Der Wasserstand ist: *Kritisch*
      Der kritische Wasserstand wurde zuletzt am
      *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht
  {%- endif -%}

No biggie - usually I don’t restart for weeks anyway :wink:

Yes, I actually don’t think the quotes are necessary, I just copied your message over.

Again, I’m not that experienced with this type of templating, but perhaps the dashes are not used in this case, so maybe something like:

{% if is_state('states.binary_sensor.schacht_wasserstand_alarm.state', 'off') %}

Otherwise just go to the dev_state in the UI, and make sure that it shows on and off as states?

I figured it out

message: >
  {% if is_state('binary_sensor.schacht_wasserstand_alarm', 'off') %}
      Der Wasserstand ist: *OK*
      Der kritische Wasserstand wurde zuletzt am
      *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}* erreicht
  {% else %}
      Der Wasserstand ist: *Kritisch*
      Der kritische Wasserstand besteht seit:
      *{{ as_timestamp(states.binary_sensor.schacht_wasserstand_alarm.last_changed) | timestamp_custom('%d.%m.%Y um %H:%M') }}*
  {% endif %}

You copy pastet states.binary_sensor.schacht_wasserstand_alarm.state :wink:

Now it works like it should

THANKS!

Ahh, perfect! Good to hear.

And I’m sure someone else can help with the last_changed part. If not here, try the discord channel.

will try - thanks again!