Numeric_state trigger doesn't fire

Hi,

I would like to monitor the free hard disk space on my Pi-Hole with HA. To do this I created one line script on the Pi that sends messages of the available free space on the SD card in the following format:

{
  "DiskFreeSpace" : 1200964
}

I created the following automation trigger:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: numeric_state
    entity_id: sensor.pihole_disk_free_space
    below: 1000000
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

The problem is that it doesnā€™t fire.

This is the definition of the sensor:

#   Pi-Hole
- platform: mqtt
  state_topic: "viktak/spiti/devices/pihole"
  value_template: '{{"{:,}".format(value_json.DiskFreeSpace)}}'
  name: PiHole Disk Free space
  unit_of_measurement: bytes

Anybody have any idea why it wouldnā€™t work? Of course, the code passes validationā€¦

The free disk space wouldnā€™t happen to be below 1000000 already would it ?

No, I have tried it with several values (sending different values manually using mosquitto_pub -h ip.address -t viktak/spiti/devices/pihole -m "{\"DiskFreeSpace\":15000}")

I seem to have found the root of the problem, and it may be a bug, or a feature I donā€™t know about:

The automation is correct as it is:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: numeric_state
    entity_id: sensor.pihole_disk_free_space
    below: 1000000
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

The problem is with the argument of below.
As it is in this example, I would expect to trigger when the number received becomes less than 1000000 (one million). However, in reality, it does not trigger then (I tried it with a few 6 digit numbers), and that is what threw me off at the beginning.
When I tried it with increasingly smaller numbers, I found out that it does trigger, but not when it goes below 1000000, but when it goes below 1000. Strange.

Could someone confirm this is expected behavior because some obscure reason? If not, Iā€™ll open an issue in github.

What is the state of sensor.pihole_disk_free_space under Developer Tools -> States?

The exact value I am sending to it, i.e. when I send 1000000, it is 1,000,000

Ok, thatā€™s indeed strange. Does it work if you use a template trigger like this:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: template
    value_template: "{{ states('sensor.pihole_disk_free_space') | int < 1000000 }}"
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

Nope, it is even worse. Now I could find any limit where it would trigger.
I also tried using float instead of int, same differenceā€¦

Some further experiments revealed even stranger thingsā€¦:

This is the ā€œskeletonā€ I used:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: numeric_state
    entity_id: sensor.pihole_disk_free_space
    below: 1000000
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

First I set the below attribute to 1 million as mentioned earlier, thatā€™s when the real limit was at 1000.

Now I started experimenting putting lower values to below, and I found that putting 100000, 10000 or 1000 made absolutely no difference, the real turning point stayed at 1000.
Once I started lowering that, it started to behave correctly at 100 and 10.

Now I am guessing it has to do with some variable sizesā€¦

What does this show in the template editor:

"{{ states('sensor.pihole_disk_free_space') | int }}"

It shows 0

At the same time, in States, it shows (correctly) 1000000, see screenshot (even I donā€™t want to believeā€¦:))
Untitled

So up to 999, the template page shows correctly the same value. Any number above that returns 0.

I think it could have something to do with the comma as 1000 separator.

What does this show in the template editor?

"{{ states('sensor.pihole_disk_free_space') | replace(',','') | int }}"

The commas would indeed be the issue.

Yess!! That was the problem!

I think it is still a bug though as it shouldnā€™t matter how I format a number for displaying, the value of it should be the same. I will report it on Github.

Thank you for sticking with me on this!!!

Yes, Thank you I just confirmed it to Burningstone.

Iā€™m not sure if this should be reported on Github. The value template of your PiHole sensor applies the format you want and at this point itā€™s not a number anymore but a string. However Iā€™m not really sure about this.

Ok, if you think so.

For completeness sake here are both version of the automation rule - both tested and working flawlessly:

Using template trigger:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: template
    value_template: "{{ states('sensor.pihole_disk_free_space') | replace(',','') | int < 1000000 }}"
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

Using numeric_state trigger:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: numeric_state
    entity_id: sensor.pihole_disk_free_space
    value_template: "{{ states('sensor.pihole_disk_free_space') | replace(',','') | int }}"
    below: 1000000
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"

Thanks again for your input!

1 Like

I didnā€™t read through the whole topic (I had bookmarked it and planned to provide input sooner but got busy :slightly_smiling_face:), but just an FYIā€¦ For the value_template of the numeric_state trigger, the new state (i.e., the State Object) that caused the trigger to re-evaluate is provided as a variable named state. So the following will work, too, and is probably better (in that it is using the exact state that caused the automation to re-evaluate the trigger.) Also, the trigger will convert the value to a float, so no real need for the int filter in your template (unless you really want the comparison to be done as an int instead of a float.) So, Iā€™d suggest:

- alias: "Pi-Hole low disk space"
  trigger:
    platform: numeric_state
    entity_id: sensor.pihole_disk_free_space
    value_template: "{{ state.state | replace(',','') }}"
    below: 1000000
  action:
  - service: telegram_bot.send_message
    data:
      title: "Pi-Hole"
      message: "Low disk space"
1 Like

Yep, it makes sense. Thanks for your input!

1 Like