Condition - Switch - Last changed attribute

Hello

i’m unable to figure out, how can i create a condition, which uses the switch last time changed attribute.

Above i used uppercase for the missing part… Can anyone help me how can i solve this problem ?

- alias: Wellpump flow check for safety -
  trigger:
    platform: time_pattern
    seconds: '/5'
  condition:
    condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.storage_tank_pulse_counter
      below: 100
      above: 15
    - condition: state
      entity_id: switch.wellpump
      state: 'on'
    - condition: , THE switch.wellpump   IS TURNED ON MORE THAN 10 SECS
    - condition: numeric_state
      entity_id: sensor.storage_tank_ultrasonic_sensor
      below: 745
  action:
  - service: switch.turn_off
    data:
      entity_id: switch.wellpump
  - service: timer.start
    target: 
      entity_id: timer.wellpump_filldelay

Let’s start with, what do you want the automation to do.
The third line immediately makes me think there are probably better alternatives.

Reading it better…
The condition you have in uppercase. Make that the trigger and everything is solved?

What does the turn on automation look like? And the turn off - normal automation look like (guessing by the name this is a second turn off automation)

A pump is in a “well” which is small, and have not too much water in it so i have to pump the water out to a remote storage tank.

Two measurements must be made, one is the flow checking which tells if the pump sucks water or air (this is the level check, because there is no space to put anything into this well except the pump), and an ultrasonic sensor which controls if the storage tank is filled or not.
If the flow rate is low, it must be turned off for a specified time (till the well restores its water level), thats why i need a timer.

Without this condition its working for years without any serious problems, but sometimes it got crazy because:

  • if i turn on the pump, it takes 5-10 secs to reach the good, measurable flow rate.

  • If i turn off the pump, the water flows back, so the sensor shows for 10 secs lower flow rate.
    So after the switch is switched, the next 10 seconds contains not real values, so in this case the trigger must be skipped.

  • The pump in the well can be started manually, and automatically, so the regular switch check seems to be the most elegant option. (and its home assistant reboot proof)

It is probably easiest to have an input_datetime which is set to the value when the switch was last turned on (which is also the officially recommended way of knowing a last-changed that survives reboots, Automation Trigger - Home Assistant)

Create an input_datetime and an automation that sets its value to now() when the switch is turned on
Example:

- alias: Pump is turned on
  trigger:
    platform: state
    entity_id: switch.wellpump
    to: 'on'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.wellpump_started
      data:
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

To make it even easier you can create a templated sensor that calculates the number of seconds the pump has been on (difference in seconds between now() and the value in your input_datetime, and in case your date is inproperly formatted it returns 0 (now() - now())):

template:
  - sensor:
      - name: "Wellpump running time"
        state: >
          {% if is_state('switch.wellpump', 'off') %}
            -1
          {% else %}
            {{ int(as_timestamp(now()) - as_timestamp(states('input_datetime.wellpump_started'), as_timestamp(now()))) }}
          {% endif %}

Now you can turn your unknown condition into this:

    - condition: numeric_state
      entity_id: sensor.wellpump_running_time
      above: 10

Note about using now() in the templated sensor (Templating - Home Assistant):

Using now() will cause templates to be refreshed at the start of every new minute.

This means that you might see values like 5 seconds, then 65 seconds, then 125 seconds.
There are other ways of evaluating time as well if you want it more finegrained, like for example using a templated sensor based on date/time (Time & Date - Home Assistant)

You don’t need any of that. State conditions support for.

condition: state
entity_id: switch.wellpump
state: 'on'
for:
  seconds: 10

He mentioned being reboot proof as well, but I agree that for is definitely the most elegant solution if it is not a requirement. The odds of restarting HA during the 10 seconds the pump is starting up are pretty slim :slight_smile:

1 Like

Wouldn’t the pump be unavailable at startup then go to on → for: 10 seconds?

But I still believe there are something strange with the normal turn off or the turn on automations given that he needs all these conditions and feel the need for a time pattern.

1 Like

I did some tests on my own switches in a running HA and it never seems to go to unavailable when starting up (HA 2022.7), if they were on they kept the state on during the entire reboot. It also seems that any running for will be stopped during reboot (like in the case here Automation recovery after HA restart - #2 by 123)

But I definitely agree that your first suggestion is the best (not considering reboots), something like

- alias: Wellpump flow check for safety
  trigger:
    platform: state
    seconds: switch.wellpump
  to: 'on'
  for:
    seconds: 10
  condition:
    condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.storage_tank_pulse_counter
      below: 100
      above: 15
    - condition: numeric_state
      entity_id: sensor.storage_tank_ultrasonic_sensor
      below: 745
  action:
    - service: switch.turn_off
      data:
        entity_id: switch.wellpump
    - service: timer.start
      target: 
        entity_id: timer.wellpump_filldelay

I think, the “for” is what i need. I’ll try it.
Now, i see its in the docs, bit it escaped my attention…

Thank you for the help.