How to extract last three digits of sensor value for automation condition

OK, that complicates things. You want notification if it drops below 500 / 100 also? I still don’t feel like we have a formal enough definition of what you want to happen. Please complete this table.

ID before after notification? rise? fall?
A 1050 1150 Y Y N
B 1150 1050
C 1450 1550 Y Y N
D 1550 1450
E 2050 1150
F 2150 1050
G 2450 1550
H 2550 1450

Which “new conditions”? My template would allow that through:

Please include the automation trace (on the trace screen, three dots, download trace, paste in here correctly formatted as code).

Currently using iPad app and the three dots on the trace are not doing anything, will try later when I have access to full browser.

Thanks. I’m only interested in the trace if the automation was using my template, which should have triggered on that state change.

It was your template I cut & pasted into the automation, there are still the previous triggers & conditions listed, but they are disabled.

{
  "trace": {
    "last_step": "condition/0",
    "run_id": "44c1dea59e3d8b20d3dd15f2ddba193d",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2024-02-14T09:30:34.092309+00:00",
      "finish": "2024-02-14T09:30:34.093572+00:00"
    },
    "domain": "automation",
    "item_id": "1707828504602",
    "trigger": "state of sensor.mysensor",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-02-14T09:30:34.092912+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.alexa_announcement",
              "state": "on",
              "attributes": {
                "id": "1707828504602",
                "last_triggered": "2024-02-13T23:30:34.094383+00:00",
                "mode": "restart",
                "current": 0,
                "friendly_name": "Alexa Announcement"
              },
              "last_changed": "2024-02-14T09:15:56.154524+00:00",
              "last_updated": "2024-02-14T09:15:56.154524+00:00",
              "context": {
                "id": "01HPKEDFBT101S2RPHHD2R6NAD",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "sensor.mysensor",
              "from_state": {
                "entity_id": "sensor.mysensor",
                "state": "20933.71",
                "attributes": {
                  "state_class": "measurement",
                },
                "last_changed": "2024-02-14T09:10:34.091304+00:00",
                "last_updated": "2024-02-14T09:10:34.091304+00:00",
                "context": {
                  "id": "01HPKE3MVBASPETXHGJWSJGAAD",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.mysensor",
                "state": "21263.11",
                "attributes": {
                  "state_class": "measurement",
                },
                "last_changed": "2024-02-14T09:30:34.089207+00:00",
                "last_updated": "2024-02-14T09:30:34.089207+00:00",
                "context": {
                  "id": "01HPKF88Q9YRXAXK8C76V1FV76",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of sensor.mysensor"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-02-14T09:30:34.093005+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ]
    },
    "config": {
      "id": "1707828504602",
      "alias": "Alexa Announcement",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": "sensor.mysensor"
        },
        {
          "platform": "template",
          "value_template": "{% set val = states('sensor.mysensor')|int % 1000 %}\n{{ 100 < val < 500 }}",
          "alias": "Trigger above 100 but below 500",
          "enabled": false
        },
        {
          "platform": "template",
          "value_template": "{% set val = states('sensor.mysensor')|int % 1000 %}\n{{ val > 500 }}",
          "id": "five",
          "alias": "Trigger above 500",
          "enabled": false
        },
        {
          "platform": "numeric_state",
          "entity_id": [
            "sensor.mysensor"
          ],
          "above": 100,
          "id": "message_100",
          "value_template": "{{ (states.sensor.mysensor|int) % 1000 }}",
          "enabled": false
        },
        {
          "platform": "numeric_state",
          "entity_id": [
            "sensor.mysensor"
          ],
          "above": 500,
          "id": "message_500",
          "value_template": "{{ (states.sensor.mysensor.state|int) % 1000 }}",
          "enabled": false
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{% set old = trigger.from_state.state %} {% set new = trigger.to_state.state %} {% set oldpre = old[:-3] %} {% set newpre = new[:-3] %} {% set oldsuf = old[-3:]|int %} {% set newsuf = new[-3:]|int %} {{ (oldsuf <= 100 and newsuf > 100) or\n   (oldsuf <= 500 and newsuf > 500) or\n   (oldpre != newpre and newsuf > 100) or\n   (oldpre != newpre and newsuf > 500) }}\n"
        },
        {
          "condition": "template",
          "value_template": "{% set diff = trigger.from_state.state |int % 1000 %}\n{{ diff <= 500 if trigger.id == 'five' else ( 100 >= diff or diff >= 500 )}}",
          "enabled": false
        },
        {
          "condition": "time",
          "after": "08:00:00",
          "before": "00:00:00",
          "weekday": [
            "mon",
            "tue",
            "wed",
            "thu",
            "fri",
            "sat",
            "sun"
          ]
        }
      ],
      "action": [
        {
          "service": "notify.alexa_media_echo_dot",
          "data": {
            "message": "\"<amazon:emotion name='excited' intensity='high'>Hey, Sensor is {{ states('sensor.mysensor')|int }}</amazon:emotion>\"",
            "data": {
              "type": "announce",
              "method": "emotion"
            }
          },
          "alias": "Send announcement to Alexa"
        }
      ],
      "mode": "restart"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HPKF88QCG5HQGQZ0GTTJJ6AK",
      "parent_id": "01HPKF88Q9YRXAXK8C76V1FV76",
      "user_id": null
    }
  },
  "logbookEntries": []
}

It is yours, the others are “enabled”: false

I have just duplicated the automation and removed everything else apart from your trigger and condition from post 18 to make it clearer on any future traces.

Apologies, it was all on one line which threw me.

The issue here is that the sensor hasn’t gone from 20933 to 21263 — it has gone from 20933.71 to 21263.11.

It wasn’t previously revealed that the sensor state might not be an integer. In fact:

So please try:

trigger:
  - platform: state
    entity_id: sensor.mysensor
condition:
  - condition: template
    value_template: >
      {% set old = (trigger.from_state.state)|int(0)|string %}
      {% set new = (trigger.to_state.state)|int(0)|string %}
      {% set oldpre = old[:-3] %}
      {% set newpre = new[:-3] %}
      {% set oldsuf = old[-3:]|int %}
      {% set newsuf = new[-3:]|int %}
      {{ (oldsuf <= 100 and newsuf > 100) or
         (oldsuf <= 500 and newsuf > 500) or
         (oldpre != newpre and newsuf > 100) or
         (oldpre != newpre and newsuf > 500) }}
action:
  - NOTIFICATION HERE

This is why we always ask a lot of questions and want to see real data: otherwise we’re just chasing our tails.

I understand completely and appologies, there are no decimal places anywhere I see the values displayed…

I have pasted the new conditions and will test :slight_smile:

1 Like

I’ve just had a couple of announcements made, which shouldn’t have been.

22024.59 went to 21992.08

22023.06 went to 21927.67

These both passed the condition but are not close to 500 or 100 (which was the intention), as the announcement could keep going off with just 32 change in sensor value (first example).

Any ideas to prevent that? :slight_smile:

Come up with a better description of the rules. These definitely trigger your original description, twice in fact: