#Using the 'numeric state' trigger to perform one action when above X and another when below X

1 messages · Page 1 of 1 (latest)

raven imp
#

I'd like to switch a device off when the last person leaves home, and on when the first person enters.

I wrote the following automation, and I think it works. But it also doesn't seem quite right:

alias: Turn on lights if anyone is home
triggers:
  - trigger: state
    entity_id:
      - zone.home
actions:
  - if:
      - condition: numeric_state
        entity_id: zone.home
        above: 0
    then:
      - action: switch.turn_on
        target:
          entity_id: switch.christmas_lights
    else:
      - action: switch.turn_off
        target:
          entity_id: switch.christmas_lights

For starters, I believe this will trigger everytime the state changes, e.g. from 1 to 2. I want it to only trigger if it crosses the threshold either from below to above (0 -> x) or from above to below (x -> 0).

Shouldn't there be a way to use the numeric_state trigger in the first place (instead of the state workaround)? But then how do I perform separate actions when the numeric state crosses above vs. below the threshold?

regal hill
#

Two triggers then 😉

#

One of below: 1 and one of above: 0

raven imp
#

You mean writing two automations?

#

Or two triggers on one automation? How do I differentiate between the actions then?

regal hill
#

Two triggers in one automation

#

You don't even need to change the body of the automation if you don't want to

#

You could use trigger ids and a trigger condition to tell which trigger it was, but for what you have it's not needed

#
triggers:
  - trigger: numeric_state
    entity_id: zone.home
    above: 0
  - trigger: numeric_state
    entity_id: zone.home
    below: 1
raven imp
#

I'll try to recap what you're saying about the trigger:
In the actions, I'll need some kind of condition/switch/if-else either way to differentiate. I can do that either using trigger IDs, or by repeating the same check again (as I currently have).

regal hill
#

Literally all you have to do is swap the trigger block you have with the one I wrote there

#
alias: Turn on lights if anyone is home
triggers:
  - trigger: numeric_state
    entity_id: zone.home
    above: 0
    id: 'occupied'
  - trigger: numeric_state
    entity_id: zone.home
    below: 1
    id: 'empty'
actions:
  - if:
      - condition: trigger
        id: 'occupied'
    then:
      - action: switch.turn_on
        target:
          entity_id: switch.christmas_lights
    else:
      - action: switch.turn_off
        target:
          entity_id: switch.christmas_lights
``` or you can do that
raven imp
#

And as for the triggers, the docs [https://www.home-assistant.io/docs/automation/trigger/#numeric-state-trigger] state:

fires if the value is changing from above to below or from below to above the given threshold

Are you telling me that an above: x trigger is not triggering when the value is above the threshold and crosses the threshold to below it? Because that's absolutely not stated in the docs. I.e. the documentation uses very ambiguous phrasing.

Home Assistant

All the different ways how automations can be triggered.

regal hill
#

I'd say that's pretty clear

#

from above to below
So, not from above to above

raven imp
#

Let me quote again (selectively!):

[A numeric state trigger] fires if the value is changing from above to below [...] the given threshold

regal hill
#

Not from below to below

solemn bone
regal hill
solemn bone
#

big ole note

regal hill
#

That's pretty clear that it's only when it crosses

raven imp
#

Yeah, but it is crossing in my case!

regal hill
#

Wel, no

#

You're triggering on every state change

#

What I wrote isn't

raven imp
#

Use case:

numeric trigger with above: 50

old value: 100
new value: 1

Docs say:

[A numeric state trigger] fires if the value is changing from above to below [...] the given threshold

Does it fire?

solemn bone
#

my guy, read the note

#

also read the below field and the above field

#

you're thinking hte below field and above field react the same way

regal hill
#

Your example does not go from 50 or below to above 50, so clearly it won't fire

solemn bone
#

they do not. The below field matches:

[A numeric state trigger] fires if the value is changing from above to below [...] the given threshold

#

the above field is the opposite

#

To make it more clear:

#

2 separate options, 2 separate behaviors, both can be combined together if you want it inside a range (or outside)

raven imp
#

I mean, okay. First of all, thank you all for your help, I think I gained a lot of understanding and will be able to complete my automation exactly to my wishes now. ❤️

I think I also understand how the numeric state trigger works and I'm sure the implementation makes sense.

I don't think what you're describing is unambiguously covered by the documentation however. I believe, the way it is written, it is easily understood by those not very familiar with HA. (I.e. the probable target audience of the documentation.)

One of the core problems is probably that the docs don't define what "threshold" means. I would expect a 'threshold' to be a border line that can be crossed in two directions. It however uses phrases like "within the threshold", which just doesn't make sense with my earlier understanding.

It also uses the words "above" and "below" both to make assumptions about relative values, as well as to mean the parameters of the trigger. That's also not ideal.

solemn bone
#

By all means, write up a suggestion or reword the docs yourself (for others)

#

this is a community after all

#

It makes sense to us

raven imp
#

👍