I can't figure out why this automation doesn't work

It’s as simple as it gets but for some reason it doesn’t trigger and turn on the fan. I’m probably missing something obvious but I don’t know what:

alias: Fans - Ensuite Fan On at 53% humidity
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
condition: []
action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.ensuite_bathroom_fan
mode: single

It’s not triggering because the sensor’s value already exceeds the Numeric State Trigger’s threshold value.

The Numeric State Trigger will trigger at the moment the sensor’s value increases from below 53 to above 53. Once it’s above 53 it won’t continue to trigger the automation. The sensor’s value will first have to decrease below 53 then rise above it to trigger the automation.

1 Like

Have you read about how numeric_state works?

Fires when the numeric value of an entity’s state (or attribute’s value if using the attribute property) crosses a given threshold.

Unless it starts below your threshold and then going up passing your threshold, the automation will not be triggered.

…shoot ninja’d by 123

1 Like

Thanks, somehow I missed that behaviour when I was reading about numeric_state.

Is there some way of making it behave like it does in Node Red where as long as a state change occurs and it’s above the threshold it’ll trigger the automation rather than having to cross the threshold?

Maybe just by using state trigger changes without specifying threshold -

Then using state condition above: xx? Also condition only if your switch is in OFF position.

Sorry if my wording is unclear. I am using mobile phone therefore cannot write YAML codes.

alias: New Automation
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.ensuite_bathroom_humidity
condition:
  - condition: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
  - condition: state
    entity_id: switch.ensuite_bathroom_fan
    state: 'off'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.ensuite_bathroom_fan

Totally get what you’re saying. I’ll give that a try and see if it works. I use an input_select to set the state of the fan when it’s been turned off so that it won’t turn back on again for 10 minutes if it’s manually turned off (WAF request lol).

Thanks!

alias: Fans - Ensuite Fan On at 53% humidity
description: ''
trigger:
  - platform: state
    entity_id: sensor.ensuite_bathroom_humidity
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.ensuite_bathroom_humidity
        above: '53'
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.ensuite_bathroom_fan_state
            state: Just Turned Off
action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.ensuite_bathroom_fan
mode: single

Sure! If you are using 2021.7, you can even combine the trigger for turn on and off in a single automation - taking advantage of trigger condition and trigger ID feature. See this post below-

That’s cool. Thanks for pointing that out…I skimmed over the release notes but that one didn’t really sink in.

1 Like

Well, it appears to have worked! BUT! According to the trace it shouldn’t have? Or am I reading it wrong? Maybe I’m not using the NOT condition correctly.

Executed: July 21, 2021, 10:33:29 AM
Result:
result: true
conditions/0
Executed: July 21, 2021, 10:33:29 AM
Result:
result: true
conditions/0/entity_id/0
Executed: July 21, 2021, 10:33:29 AM
Result:
result: true
state: 54
conditions/1
Executed: July 21, 2021, 10:33:29 AM
Result:
result: true
conditions/1/conditions/0
Executed: July 21, 2021, 10:33:29 AM
Result:
result: false
conditions/1/conditions/0/entity_id/0
Executed: July 21, 2021, 10:33:29 AM
Result:
result: false
state: 'Off'
wanted_state: Just Turned Off

I think the state of your input_select is incorrect? Can you check it in developer tools?

Sorry, or maybe you need to add ‘’?

It’s set to Off which is what I’m basically looking for…any condition other than “Just Turned Off”

Continuously triggering it when it’s above a value is a stupid approach. Just trigger it when you want to. Probably:

  • When the value goes above 53
  • When the input select goes to a state it allows it to turn on and (condition, not trigger!) the value is above 53
  • HA restart and [(condition, not trigger) the value is above 53 and the input select allows it

The whole idea of events is that you can be pretty specific. Triggering it on every change would be as wast-full as looking at the timer ever second when you cook pasta instead of waiting till it beeps.

2 Likes

Yes, I agree. The better approach is to use the trigger of numeric_state and HA restart.

Well, “stupid” is a bit of a strong way to describe what I’m trying to accomplish.

I want the fan to run whenever the humidity is over the threshold, not just when it crosses. I’m not familar with “HA restart” that your refer to.

To add, I’ve been using Node Red for this stuff for the last few years but in an effort to simplify I’m trying to move some of my simpler automations to the native automation engine. But obviously this isn’t as “simple” as I thought it would be considering the different behaviours between it and Node Red’s logic.

Trigger type: homeassistant & start event. It is to anticipate if the threshold is crossed when HA is restarting.

I believe the sentiment is that the ‘always be triggering’ approach isn’t the most efficient way of using Home Assistant’s resources. I concur with that opinion.

The issue you are currently experiencing is that the Numeric State Trigger won’t trigger at this point in time because the sensor’s value (59) is already above the trigger’s threshold value (53). Had you created the automation when the sensor’s value was below 53, we wouldn’t be having this discussion (because it would have triggered the moment it increased above 53).

The easiest remedy is to turn on the switch manually (just this very first time). I assume you’re using another automation to turn off the switch (both can be done with a single automation but that’s another discussion). The next time the sensor’s value increases from below 53 to above 53, it will turn on the switch. Easy-peasy.

However, if you prefer the ‘always be triggering’ approach, use ardysusilo’s example. It triggers every time the sensor’s value changes (even when its value is above or below the threshold) but employs a condition to prevent repeatedly turning on the switch if it’s already on.

Stupid as in not smart because it just creates a triggerstorm :slight_smile:

Here an example with just the right triggers:

alias: Fans - Ensuite Fan On at 53% humidity
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
  - platform: state
    entity_id: input_select.ensuite_bathroom_fan_state
    from: Just Turned Off
  - platform: homeassistant
    event: start
condition:
  - condition: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
  - condition: not
    conditions:
      - condition: state
        entity_id: input_select.ensuite_bathroom_fan_state
        state: Just Turned Off
action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.ensuite_bathroom_fan
mode: single

Or, if the only purpose of the input_select is to have a dead time of 10 minutes you can do without:

alias: Fans - Ensuite Fan On at 53% humidity
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
  - platform: homeassistant
    event: start
  - platform: state
    entity_id: switch.ensuite_bathroom_fan
    to: "off"
    for: "00:10:00"
condition:
  - condition: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity
    above: '53'
  - condition: state
    entity_id: switch.ensuite_bathroom_fan
    state: 'off'
    for: "00:10:00"
action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.ensuite_bathroom_fan
mode: single

Although turning on at a fixed level will probably create issues when there is high humidity…

1 Like

Thanks for the tweaks. You’re right, the for: condition eliminates the need for the input_boolean. That was a hold over from Node Red.

Also, I think it’s best if I explain my logic here for using a state change for the trigger along with a condition for numeric_state rather than a numeric_state for the trigger.

Case A: trigger with numeric_state

Humidity rises above 53%. The numeric_state threshold is crossed 53% → 54%. The automation is triggered and the conditions (numeric_state > 53% and fan has been off for 10 minutes) are true so the action is executed.

If the humidity is sill above 53% but someone comes in and turns off the fan the automation will never be executed again until after the humidity falls below 53%…but I want the fan to be on as long as the humidity is above 53%, not just when it exceeds 53% the first time.

Case B: trigger with state change + numeric_state condition

Humidity state changes. Humidity is above 53%. The automation is triggered and the conditions (numeric_state > 53% and fan off for 10 minutes) are true so the action is executed.

Now if someone comes in and turns off the fan the next time the state changes the fan will turn back on if the humidity is still above 53% and the fan’s been off for 10 minutes. This is the desired behaviour.

I’m not worried about trigger storms because the humidity doesn’t change more than 1 percent per five minutes or so.

Does it make sense now why I’d want to evaluate every state change instead of when a state has crossed a threshold?

If the fan is switched off manually, does the state of switch changes to off in your HA?

If yes, what if you add another trigger that detects if the switch is turned off? But the trigger will always be evaluated with your condition. So, unless the humidity is higher than 53, it won’t turn on your fan?

Note: the above suggestion assumes Case A.