Need help with automation trigger of a numeric value for a specified period of time

I could use some help with a automation triggering problem I’ve been trying to work out. I’ve seen a couple of threads that seem to point to an answer but I can’t seem to put it together in my head.

I want to trigger an automation when the following exists:

The power reading of sensor.aeotec_smart_switch_6_power_10 is less than 3.3

-AND-

It has been in that state for more than 45 seconds.

I’ve tried a number of approaches and most have ended in syntax errors. It would be so easy if you could add a for statement to a numeric value state but that isn’t valid. I get the feeling I will need to create a template sensor for the power and then check the state of that sensor for the time value. But my mind is hurting from trying to figure out what is probably obvious to someone here so I’m reaching out for help!

1 Like

Shot in the dark…

#Automation

- alias: XXXX
  trigger:
    platform: numeric_state
    entity_id: sensor.aeotec_smart_switch_6_power_10
    value_template: '{{ states.sensor.aeotec_smart_switch_6_power_10.state }}'
    below: 3.3
  action:
    - service: script.turn_on
        entity_id: script.script_name

#Script
scriptname:
sequence:
- delay:
seconds: 45
- service: something.do_that
data:
entity_id: your.entity_id

1 Like

Nope that isn’t it. Gears are turning…

1 Like

Maybe not but it’s good to have another brain working on it. Mine hurts! I’ve been beating against the wall with this for a bit now.

I’m trying something using a boolean_input for the state and then measuring the time elapsed on that state and I’m going to see if that works, but if you come up with anything else - by all means, let me know!

And thanks. I’ll report my findings later.

1 Like

Will do! Got a Honey-Do-List thrown at me tonight. I will sleep on it but if you figure it out let me know!

Thanks!!

1 Like

The yaml checked out…

   - alias: XXXX
      trigger:
        platform: numeric_state
        entity_id: sensor.aeotec_smart_switch_6_power_10
        condition: template
        value_template: '{{ states.sensor.aeotec_smart_switch_6_power_10.state < 3.3 }}'
        for:
          seconds: 45  
      action:
      - service: somthing.do_something
        entity_id: entid.your_ent

I think I tried this but not sure so I’ll test it out today. Thanks, @PtP!

Not an immediate fix but perhaps a future one: vote on the feature request For-statement for automation with numeric state

1 Like

Done. Still need to test the above though.

Guess I’ll wash my sheets… LOL

Sadly, this produced the following error and failed the automations load:

Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->trigger->0->condition. (See /home/hass/automation.yaml:136)

Line 136 naturally points to this automation… DAMN! I thought we were on to something. :frowning:

At the moment, there’s no way to combine numeric_state with the for-statement in any shape or form (hence the feature request). I work around it by making a template sensor for the value I want to test. And then I use that template sensor in a state trigger with the for-statement.

I’ve tried that in various ways but can’t seem to get it right. Any chance you could show an example? I’ve been really stuck here for over two or three weeks trying to get what should be a simple automation done and it’s driving me crazy!

I’d appreciate any guidance you could offer.

Here’s what I use to switch off my office lights after it’s been bright enough for 5 minutes as an example.

My template (binary) sensor (its state will be ‘on’ when the value template returns ‘true’, otherwise it’s ‘off’):

binary_sensor:
  - platform: template
    sensors:
      office_multi_luminance_templated:
        value_template: "{{ states('sensor.office_multi_luminance_3') | int > 45 }}"
        entity_id: sensor.office_multi_luminance_3

And my automation:

automation:
  - alias: "Turn off lights when bright while working"
    trigger:
      - platform: state
        entity_id: binary_sensor.office_multi_luminance_templated
        from: 'off'
        to: 'on'
        for:
          minutes: 5
          seconds: 0
      - platform: state
        entity_id: sensor.laptop_work_david_templated
        to: 'Offline'
    condition:
      - condition: state
        entity_id: group.office_lights
        state: 'on'
    action:
      - service: light.turn_off
        data:
          entity_id:
            - light.lamp_desk_left
            - light.lamp_desk_right
          transition: 5
1 Like

I feel like this has been the missing piece for me. I knew this was the direction I wanted to go but I could never get the value template expression correct. It’s frustrating because I use value templates in other aspects of HASS and I have no problem - they work great. But for some reason I just couldn’t get it right with this one.

I’ll add this in and change my automation trigger as appropriate and let you know. I’ll restart later to see if the automations initialized properly and report that and then tomorrow, I’ll test it out in real life with a wash load.

Thanks so much for your help. Been really at a dead end on this.

1 Like

So far so good; no errors on my restart and automations are fully loaded. I’ll post tomorrow with the final results as well as the actual code I used for my example (assuming it works).

I want to thank everyone here for their help and participation and patience as I’ve been trying this over a couple of weeks and threads. Thanks for putting up with me, guys.

2 Likes

ARRRGGGHHH!!!

Still not working as expected but made some progress. Let me recap; the goal was to get a notification when the washer and dryer were done by measuring the power levels on the two Aeotec Smart Switches I have them plugged into. I’m using a combination of template sensors, input booleans and automation scripts. For the dryer, it’s pretty easy - it’s either running or not. For the washer, I get power drops in between the cycles so I needed to put in logic to allow for power to drop for at least one minute, fifteen seconds to prevent the notification from firing prematurely.

Here’s what I know can be ruled out as being an issue:

  1. The switches themselves. They are reporting fine and show the power levels in almost real time. They are both identical and both have the same firmware.

  2. The template sensors. They are showing the correct states during each phase of the washer and dryer cycles. When the power level changes, the associated sensor displayed in the card changes immediately. Here are the template sensors:

     #
     #   Automation Templates
     #
     #    True when washer power level is below 3.4 watts, high threshold of resting state
     #    Default state is true
          washer_pwrdn:
            value_template: "{{ states('sensor.aeotec_smart_switch_6_power_10') | int < 3.4 }}"
    
     #    True when Dryer power level is above 0.1 watts, showing dryer is in operation
     #    Default state is false
          dryer_pwrup:
            value_template: "{{ states('sensor.aeotec_smart_switch_6_power_11') | int > 0.1 }}"
    

And here’s the code for the boolean_inputs:

washer_switch:
    name: Toggle Washer Automation
    initial: off
    icon: mdi:water
dryer_switch:
    name: Toggle Dryer Automation
    initial: off
    icon: mdi:sync

The only thing that could be left is in the automations themselves and here I have one half of each working. The Washer Start sequence works and toggles the boolean correctly, but the Washer Done sequence never fires. In the Dryer, it’s the exact opposite. The Dryer Start sequence never fires and toggles the boolean, but if I manually toggle it, the Dryer Done sequence works.

So the two problem areas are Washer Done and Dryer Start. There are no syntax errors. No errors in the logs for the automations and they all initialize correctly. Checked for spelling errors three times (but please check again) and found nothing wrong.

Here’s the automation code for the Washer:

# Washer Notification Sequence

- alias: 'Washer Start'
  trigger:
    platform: numeric_state
    entity_id: sensor.aeotec_smart_switch_6_power_10
    above: 3.30
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.washer_switch

- alias: 'Washer Done'
  trigger:
    platform: state
    entity_id: sensor.washer_pwrdn
    from: 'false'
    to: 'true'
    for:
      minutes: 1
      seconds: 15
  condition:
      condition: state
      entity_id: input_boolean.washer_switch
      state: 'on'
  action:
    - service: notify.pushbullet
      data:
        message: 'Robert, the washer is done.'
    - service: input_boolean.turn_off
      entity_id: input_boolean.washer_switch

And the Dryer:

# Dryer Notification Sequence

- alias: 'Dryer Start'
  trigger:
    platform: state
    entity_id: sensor.dryer_pwrup
    from: 'false'
    to: 'true'
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.dryer_switch

- alias: 'Dryer Done'
  trigger:
    platform: state
    entity_id: sensor.aeotec_smart_switch_6_power_11
    state: '0.0'
  condition:
    condition: state
    entity_id: input_boolean.dryer_switch
    state: 'on'
  action:
    - service: notify.pushbullet
      data:
        message: 'Robert, the dryer is done.'
    - service: input_boolean.turn_off
      entity_id: input_boolean.dryer_switch

I feel like there’s something wrong with me that I can’t figure out what should be a fairly straight forward automation!!

What happens if you try this value template?

value_template: "{{ states.sensor.aeotec_smart_switch_6_power_10.state | float }}"

1 Like

As the sensor? But the sensor templates actually work. Can you explain why? It would help me to understand.

Ok don’t change everything. Integers are numbers without decimal points, that is why I think the template needs to change to float. This might not be the complete solution. I also notice that in the Washer code above: 3.30 is misssing the quotes around ‘3.30’

Edit: Is the state of the template sensors really true or false? I believe it should be on or off.

1 Like

I put the template sensors in a card on the same page as my switches so I could see the state change and this is how they show:

The states are correctly being interpreted and change immediately with the power levels. But they do show True and False.