Counter range high or low as a trigger

I’m trying to automate my ceiling fan based on a counter. If the counter is below 5 and no Presence is detected then wait 5 minutes and turn off , if over 5 then wait 20 minutes however this doesn’t seem to be happening. I can see the counter is working correctly so it must be my logic.

Any suggestions or is my approach completely wrong ?

alias: Ensuite Ceiling Fan 2
description: ""
triggers:
  - entity_id: binary_sensor.shower_2_presence
    to: "on"
    id: Presence Home
    trigger: state
  - entity_id: binary_sensor.shower_2_presence
    to: "off"
    id: Presence Away
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: Presence Home
        sequence:
          - data:
              entity_id: switch.ensuite_ceiling_fan
            action: switch.turn_on
      - conditions:
          - condition: trigger
            id: Presence Away
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            below: 4
        sequence:
          - delay:
              minutes: 5
          - data:
              entity_id: switch.ensuite_ceiling_fan
            action: switch.turn_off
      - conditions:
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            attribute: initial
            above: 5
            below: 1000
          - condition: trigger
            id:
              - Presence Away
        sequence:
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
          - data:
              entity_id: switch.ensuite_ceiling_fan
            action: switch.turn_off
mode: single

Don’t really understand what that counter is supposed to achieve, but… One of your conditions is based on the actual state of the counter, but the other is based on its attribute: initial.

The first action is to turn on the ensuite fan if presence is detected

The second is supposed to be set if “presence is away” and the shower counter is below 4 then wait 5 minutes then turn off the ensuite fan.

The other is if the counter is over 5 then wait 20 minutes then turn off the ensuite fan.

The counter that is separate to this counts based on presence and waits a set time before resetting back to zero.

Does that help ?

But what does the counter represent and/or do? There is nothing in this automation that manipulates the counter, so kind of impossible to understand what it is for. How many showers have been taken during the day? How many people are currently in the shower? :stuck_out_tongue: Why does the counter influence how long the fan should be on?

Also ponder this scenario: Someone walks into the bathroom/shower, presence detector presumably flips to on, automation runs and turns on the fan. They walk out, presence detector flips to off, automation runs again and begins a delay. But what happens (or is supposed to happen) if someone walks back into the bathroom during the delay? Currently nothing will happen, as the automation is set to mode: single and is “busy” executing the delay. Is that really intended? I would’ve expected the delay to be reset.

And again, your third condition is not based on the state of the counter – it is based on its attribute: initial. Regardless of what the point is with different duration of the fan based on the counter, that does not seem right. Also both of your delays are currently set to 5 minutes…

Structuring the automation something like this would make more sense to me:

triggers:
  - id: "on"
    trigger: state
    entity_id: binary_sensor.shower_2_presence
    to: "on"
  - id: "off 5"
    trigger: state
    entity_id: binary_sensor.shower_2_presence
    to: "off"
    for:
      minutes: 5
  - id: "off 20"
    trigger: state
    entity_id: binary_sensor.shower_2_presence
    to: "off"
    for:
      minutes: 20
conditions:
  - or:
      - condition: trigger
        id: "on"
      - and:
          - condition: trigger
            id: "off 5"
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            below: 6
      - and:
          - condition: trigger
            id: "off 20"
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            above: 5
actions:
  - action: "switch.turn_{{ trigger.to_state.state }}"
    data:
      entity_id: switch.ensuite_ceiling_fan

(Obviously completely untested, probably has typos or something missing in the logic, caveat emptor, etc.)

Also note that…

…would leave a gap in the logic. What happens if the counter is exactly 5, should the fan never turn off? Presumably it should, hence why I went with below: 6 and above: 5 in my version of the automation.

Thanks for your help , your right It would probably help if I posted the counter logic. Shower wise 1,2 or more :slight_smile: it shouldn’t matter. I’m using a presence sensor over motion. Basically want to keep the switch ensuite fan on for longer if someone is in the shower for a long time vs a short time which is likely just someone in the room.

I’ve updated the code to the right home / away for the sensor and the switch for the ensuite shower however it doesn’t seem to turn on or off.

alias: Ensuite fan 3
description: ""
triggers:
  - id: "on"
    trigger: state
    entity_id:
      - binary_sensor.shower_2_presence
    to: "on"
  - id: off 5
    trigger: state
    entity_id:
      - binary_sensor.shower_2_presence
    for:
      minutes: 5
    to: "off"
  - id: off 20
    trigger: state
    entity_id:
      - binary_sensor.shower_2_presence
    for:
      minutes: 20
    to: "off"
conditions:
  - or:
      - condition: trigger
        id: "on"
      - and:
          - condition: trigger
            id: off 5
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            below: 6
      - and:
          - condition: trigger
            id: off 20
          - condition: numeric_state
            entity_id: counter.ensuite_shower_counter
            above: 5
actions:
  - action: switch.turn_{{ state.to_state.state }}
    data:
      entity_id: switch.ensuite_ceiling_fan

This is the counter automation

alias: "Ensuite Shower Counter +  & - "
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.shower_2_presence
    id: Ensuite Shower Motion Active
    to: "on"
  - trigger: state
    entity_id:
      - binary_sensor.shower_2_presence
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: Ensuite Shower Motion Clear
    to: "off"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Ensuite Shower Motion Active
        sequence:
          - action: counter.increment
            target:
              entity_id: counter.ensuite_shower_counter
            data: {}
      - conditions:
          - condition: trigger
            id:
              - Ensuite Shower Motion Clear
        sequence:
          - action: counter.reset
            target:
              entity_id: counter.ensuite_shower_counter
            data: {}
mode: single

Sorry, there was (as expected) at least one typo in there. Change state.to_state.state (in the action) to trigger.to_state.state and see if it works better.

Some progress , the switch now turns on and off if the counter is under 5 , however 20 the switch stays on and doesn’t turn off even if the counter resets back to 0

Well looking at your “counter automation” just now, I would say things are working exactly as you have designed the automations to behave.

The “20 minute off” trigger of the first automation is set to only execute if the counter is above 5. But that will obviously never ever occur, as your second automation will always reset the counter to zero after 15 minutes.

I don’t understand the purpose of the counter at all. All it does is count how many times a person either enters and exits the room(/shower?) and/or alternates between moving around and standing still long enough for the motion sensor to no longer register. Seems like a poor metric for determining how long the fan should be on. Just get a cheap humidity sensor?

In short by being under 5 that should stop any false positives of someone only being near the shower. If over 5 then chances are the shower has been used or is in use. The reason for the counter is to reset the state back. Maybe there’s a better way like presence detected so many times within 5, minutes?

I’ve had a humidity sensor found it too be too inconsistent