Automation for numeric state not working

I’m stuck on why this automation is not working…I have others that work fine for a fridge but this refuses to trigger if I have the line present that prints out the friendly name which triggered it.

Anyone have suggestions?

If I remove the line {{ trigger.to_state.attributes.friendly_name }} it works fine…but the same template in other numeric-state automation works fine.

Non-working:

alias: Telegram Me Network UPS Power Alerts
id: Telegram Me Network UPS Power Alerts
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_time_left
      - sensor.kvm_host_ups_time_left
    below: 30
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_time_left
      - sensor.kvm_host_ups_time_left
    below: 15
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_time_left
      - sensor.kvm_host_ups_time_left
    below: 10
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_time_left
      - sensor.kvm_host_ups_time_left
    below: 5
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_battery
      - sensor.kvm_host_ups_battery
    below: 90
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_battery
      - sensor.kvm_host_ups_battery
    below: 50
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_battery
      - sensor.kvm_host_ups_battery
    below: 25
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_battery
      - sensor.kvm_host_ups_battery
    below: 10
  - platform: numeric_state
    entity_id:
      - sensor.pfsense_ups_battery
      - sensor.kvm_host_ups_battery
    below: 5
action:
  - target:
      entity_id: notify.telegram_me
    data_template:
      message: |
        UPS Power Alert:
          Check {{ trigger.to_state.attributes.friendly_name }}

        pfSense Core Network UPS:
          *{{ states('sensor.pfsense_ups_status') }}, Input {{ states('sensor.pfsense_ups_input_voltage') }}V
          Batt {{ states('sensor.pfsense_ups_battery') }}% Est. {{ states('sensor.pfsense_ups_time_left') }} minutes*
          Router shutdown will occur
          at {{ states('sensor.pfsense_ups_battery_shutdown') }}% or {{ states('sensor.pfsense_ups_shutdown_time') }} minutes remaining.
          Resources affected:
            -All Internet connectivity
            -All Wired/WiFi Network
            -Cellular extender
            -VoIP Landline phones

        kvm-host Server UPS:
          *{{ states('sensor.kvm_host_ups_status') }}, Input {{ states('sensor.kvm_host_ups_input_voltage') }}V
          Batt {{ states('sensor.kvm_host_ups_battery') }}%, Est. {{ states('sensor.kvm_host_ups_time_left') }} minutes*
          Server shutdown will occur
          at {{ states('sensor.kvm_host_ups_battery_shutdown') }}% or {{ states('sensor.kvm_host_ups_shutdown_time') }} minutes remaining.
          
          See /netstat for more details.
    action: notify.send_message

Trace:


What I find confusing is this one for my fridge/freezer works flawlessly:

automation:
  - alias: "Fridge Freezer Telegram Notify"
    id: "Fridge Freezer Telegram Notify"
    trigger:
      # Fridge High Temp
      - platform: numeric_state
        entity_id: sensor.fridge_temperature
        above: 38
        for: "08:00:00"
      - platform: numeric_state
        entity_id: sensor.fridge_temperature
        above: 39
        for: "04:00:00"
      - platform: numeric_state
        entity_id: sensor.fridge_temperature
        above: 40
        for: "01:00:00"
      - platform: numeric_state
        entity_id: sensor.fridge_temperature
        above: 41
        for: "00:10:00"
      - platform: numeric_state
        entity_id: sensor.fridge_temperature
        above: 42
        for: "00:05:00"
    action:
      - service: notify.send_message
        target:
          entity_id:
            - notify.telegram_me
            - notify.telegram_her
        data_template:
          message: |
            Check {{ trigger.to_state.attributes.friendly_name }} High!
            Now: *{{ trigger.to_state.state }}F*
            Exceeded limit {{ trigger.above }}F for {{ trigger.for }}

Try {{ trigger.to_state.attributes.name }} This will use the friendly name but fall back to the entity id if it does not have a friendly name.

You may need to pipe that to a default or check if it exists or something.

This is very odd…that fires the automation, but it prints out nothing:
image

And if I print out in the template-dev tab both of the entities that could potentially trigger that numeric-state automation, it looks like both of them should have friendly names:

Why would that need a default-check to print out the name of the thing that fired it?

That isn’t required anywhere else I have ever done a numeric-state triggered alert in my automatons.

Well that was my mistake. Sorry.

Should be: {{ trigger.to_state.name }}

However name is not an attribute of your sensor. It is a property of the trigger state object. So it won’t resolve in your template editor test.

Also this is very old syntax.

You should try the new format.

There are examples at the bottom of the document.

I’m confused what you mean new format? I JUST had to do a major re-write on all my Telegram message-send automations to fix a “repair” due to changes in the Telegram bot format of stuff…this changed again since December-January of this year???

Also I just did a test adding a different sensor and that fires, so it seems like it only is failing to fire correctly on friendly_name when its the apcupsd integration sensors?:

image

data_template has not been used since forever.

service: was changed to action: at least a year ago.

The telegram bot actions are currently being changed and have been altered over the last few months.

Having said that. Please re-read my post. I just edited it. Particularly the first sentence.

In the new syntax your action would be:

    actions:
      - action: telegram_bot.send_message
        data:
          target: <chat id here> # you need to specify your chat id here
          title: 'Alert!'
          message: >
            Check {{ trigger.to_state.name }} High!
            Now: *{{ trigger.to_state.state }}F*
            Exceeded limit {{ trigger.above }}F for {{ trigger.for }}

An example from my config:

  - action: telegram_bot.send_message
    data:
      target: !secret telegram_system_log_chat_group_id
      title: "🚱 <b>Pond Full</b>"
      message: >
        Pond fill valve changed to closed.

        Pond Level = {{ states('sensor.pond_monitor_pond_level')|round(1,even,'unknown') }} %

        Tank Level = {{ states('sensor.pond_monitor_tank_level')|round(1,even,'unknown') }} %

No change when its triggered by the UPS battery % numeric condition.

    action:
      - service: notify.send_message
        target:
          entity_id: notify.telegram_mattbatt
        data_template:
          message: |
            UPS Power Alert:
              Check {{ trigger.to_state.name }}

            pfSense Core Network UPS:

When I inquired about service vs action I’ve been told they are interchangeable and no reason to go and edit everything.

When I do a Telegram send message within the GUI action builder dev tool, it seems to generate the syntax I am already using for messages?

And clicking “switch to YAML” it shows (I do admit, I changed the _1234567890 to “me” for privacy when posting):

action: notify.send_message
target:
  entity_id: notify.telegram_me
data:
  message: This is a test

Though since I’m using a template not fixed-string text, everything I could find for guides seems to say I need data_template: not just data:?

I don’t have a “chat ID”, I’m not sending it to a group I’m sending it to a singular user which was set up thru the GUI as is now required for the Telegram bot?

That is not the action you should be using. This is:

As to this:

Which guides, how long ago were they written?

Got a link?

There is nothing like that in the document I linked you to.

It was changed many years ago.

You can send to groups or users.

Go to Settings → Devices & Services → Telegram bot. All your chat ids are listed there.

My example was to a group.

But the id could also be for a single user.

I don’t have a link, but that’s what I found reading thru these forums. No idea how long ago, I don’t keep a log of every of the millions of pages I have gone to.

I don’t see anything on the doc you linked that talks about messaging a USER, only chat groups and replying to interactions. This is not what I’m doing, I’m direct messaging a specific user. All I have is the user’s username and user ID that was set up thru the integration.

I also can’t find any way to add something thru the GUI that would let me tie a “chat ID” to a user to let me pick from that. Its not reasonable to have to put the user’s ID everywhere in every automation and have to keep separate track of who that is for.

When I go to the Devices & Services → Telegram bot I only see the notifiers which is what I just got done converting things over to?

Next time you do that, look at the date the post was written. There is a lot of out of date info here. Always check the docs for the latest information.

Ok, that is different from what I see but I use telegram bot webhooks.

What do you see when you click on the Matt Mill… entity then click on the cog icon in the pop-up?

It looks like maybe if I paste a user’s user-ID into the “chat ID” that can trigger a message in the dev tools but that also is not really managable.

How then could you tell apart who’s messaged by which automation when you have more than one user? Is 1234567890 or 098765431 me? It needs to be something sensible named set up in one place thru the integration, not a random number with no context spammed thru every single automation, and that if it has to be changed will be a major nightmare to untangle.

I’m not familiar with what “telegram bot webhooks” means.

I renamed mine in the GUI because I would never remember the random numbers it put by default in the name/entity ID - that way I can reference a human-readable name in automations instead of a long random string of digits:

That is changing. It is a work in progress. We will eventually be able to use entities but for now the best way is to use !secret to keep track of which id corresponds to which user/group.

e.g.
secrets.yaml

matts_telegram: <chat_id_here>

then in your action:

  - action: telegram_bot.send_message
    data:
      target: !secret matts_telegram

Don’t worry about it. What you have is fine. Lets get your problem solved first.

Interesting. Secrets would solve that problem - thanks for that suggestion.

I’m not convinced that’s related to my current issue though. I’ll try and build a test automation with the different syntax to see.

1 Like

Yep that would rule it out.

The other thing I noticed in your initial post was this:

image

Check you are using only spaces, not tabs. No tabs allowed in yaml.