#How to wait after trigger until a condition is met?

1 messages · Page 1 of 1 (latest)

elfin hound
#

I have a basic automation setup for my 3D printer where it triggers when the print is finished, and then is supposed to wait until both the bed and hotend temperature are below 50C.

However currently it seems like it does not wait for those conditions, and instead they must be true at the time that the print finish trigger activates. But I cannot see a field in the visual editor to say to wait indefinitely until the conditions are met?

Someone mentioned that annoyingly the only way to add a wait is to do it in the yaml editor instead of the front end, and also you cannot use "conditions" and instead they must be "actions"?

I have no idea how to use actions or convert my current conditions into them (especially given that the conditions all have random strings for names), so please could you suggest how I can change this automation to do the above indefinite wait?

Additionally if the printer starts another job (so is no longer in the state finished) then it should cancel/reset the automation, so I'm guessing I need to change the mode from the default "single" to "restart"?

I keep trying to upload a screenshot of the simple visual editor automation but I can't paste it in discord, and I can't upload the yaml file either so I'll just try and paste it as a codeblock instead:

#
alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished
condition:
  - condition: and
    conditions:
      - type: is_temperature
        condition: device
        device_id: e88295fc70cd6406760494fd0fef2cc0
        entity_id: afe0dd7cf7b0d2517189870269a6e6fd
        domain: sensor
        below: 50
      - type: is_temperature
        condition: device
        device_id: e88295fc70cd6406760494fd0fef2cc0
        entity_id: fa3f68451d1feeb1d79df33cea8019ec
        domain: sensor
        below: 50
action:
  - type: turn_off
    device_id: e88295fc70cd6406760494fd0fef2cc0
    entity_id: d1cb3d14a919281e409524ebf85d23a6
    domain: light
mode: single
lime vector
#

Conditions won't work for what you want. You need a wait_for_trigger action. You would place it above your turn_off action. This way, the automation will wait until the temperature falls below your threshold. You can choose to have it time out and continue or not after the timeout is reached.

wait_for_trigger:
  - platform: numeric_state
    entity_id:
      - sensor.office_temperature_sensor_temperature
    below: 50
timeout:
  hours: 0
  minutes: 10
  seconds: 0
  milliseconds: 0
continue_on_timeout: false
#

So, in that example, the automation will wait for 10 minutes and then if the timeout is reached, the automation will end.

elfin hound
# lime vector Also, I highly recommend you change from using device triggers and actions over ...

Ok, so how would I figure out exactly what the correct platform and entity id values are?
Presumably in my case the platform should just be "state" since the value is binary on or off, not a numeric value?

As for the entity id string, obviously I can't use those random strings that it creates when using the GUI because they tell me nothing, and I know the name of the 3D printer in HA but idk what exactly to do with it.

For example, in the example you linked, I can see that you construct the string start by just putting the domain (sensor) at the start, followed by "." and battery_level at the end since that is the name of the type. But how do you know what value to put in-between, such as mobile in the example?

lime vector
# elfin hound Ok, so how would I figure out exactly what the correct platform and entity id va...

Basically it's a matter of reading the docs on automation triggers and then understanding what your entities are named and what domain they belong to. You can get all of this info from the UI pretty easily. So, in terms of your printer, if it exposes a switch entity, that will be named switch.[whatever you named it]. You can name entities whatever you want to. I can walk you through how to do that in the UI if you want. 🙂

elfin hound
# lime vector Basically it's a matter of reading the docs on automation triggers and then unde...

Ok, so I did some digging in the entity names, since I didn't name any of them manually they were all auto generated by an integration, and I updated the automation yaml.
If I want to have it trigger on the event_print_finished, and then wait up to 1 hour until both sensor.p1s_nozzle_temperature and sensor.p1s_bed_temperature are below 50C, is this the correct way to do it?

alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished

wait_for_trigger:
  - platform: numeric_state
    entity_id:
      - sensor.p1s_nozzle_temperature
      - sensor.p1s_bed_temperature
    below: 50
timeout:
  hours: 1
  minutes: 0
  seconds: 0
  milliseconds: 0
continue_on_timeout: false

action:
  - type: turn_off
    device_id: e88295fc70cd6406760494fd0fef2cc0
    entity_id: d1cb3d14a919281e409524ebf85d23a6
    domain: light
mode: single

lime vector
#

That's a good start. I would change your action to be something like this though:

- service: light.turn_off
  target:
    entity_id: light.[your light entity]
#

Also, one thing to note is that if HA restarts while that 1 hour timeout is going, the automation will stop and the light will never go off. There are ways to handle this though using additional triggers and/or a timer with restore set to true.

elfin hound
#

Ah I see thanks, I'll have a look at that later.
I updated it again with your suggestion and just tried to save it for the first time since adding these changes, but now I get an error message saying Message malformed: extra keys not allowed @ data['wait_for_trigger']. Any idea what I need to fix?

alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished

wait_for_trigger:
  - platform: numeric_state
    entity_id:
      - sensor.p1s_nozzle_temperature
      - sensor.p1s_bed_temperature
    below: 50
timeout:
  hours: 1
  minutes: 0
  seconds: 0
  milliseconds: 0
continue_on_timeout: false

action:
- service: light.turn_off
  target:
    entity_id: light.p1s_chamber_light

mode: single
#

Is it because I'm not allowed to list two entity ids in the one wait for trigger?

#

If so how would I do an AND operation like I described where it waits for both those entity ids to be below 50C?

lime vector
#
alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished

action:
  - wait_for_trigger:
    - platform: numeric_state
      entity_id:
        - sensor.p1s_nozzle_temperature
        - sensor.p1s_bed_temperature
      below: 50
  timeout:
    hours: 1
    minutes: 0
    seconds: 0
    milliseconds: 0
  continue_on_timeout: false
  - service: light.turn_off
    target:
      entity_id: light.p1s_chamber_light

mode: single
elfin hound
#

I backed out of the automation editing, came back in and tried pasting your code again but now it just has a red outline in the yaml editor and no save icon

#

And when I try and back out or revert to the visual editor it just doesn't save the new pasted yaml code

lime vector
#

I did it here and it saves fine for me.

#

Oh wait... I see what you mean. 1 sec.

#
action:
  - wait_for_trigger:
      - platform: numeric_state
        entity_id:
          - sensor.office_temperature_sensor_temperature
          - sensor.attic_temperature_sensor_temperature
        below: 50
    timeout:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.attic_light_1
#

I had the wait_for_trigger wrong. Weird that the UI YAML editor didn't point that out on mine 🤔

elfin hound
#

Ok, so this works but idk what changed from your original code other than some extra indentation?

alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished

action:
  - wait_for_trigger:
      - platform: numeric_state
        entity_id:
          - sensor.p1s_nozzle_temperature
          - sensor.p1s_bed_temperature
        below: 50
    timeout:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: light.turn_off
    target:
      entity_id: light.p1s_chamber_light

mode: single
#

I removed the metadata: {} and data: {} from your example since I didn't think I would need them for mine

lime vector
#

Yeah, I just copied and pasted from the UI.

#

Just the extra indentation.

#

I always forget that indentation with the wait_for_trigger because it accepts a list of triggers. lol Never fails...

elfin hound
# lime vector Just the extra indentation.

Ok thanks, I think I've almost got it perfect now, just wanted to check 2 more things:

  1. Is the trigger at the top (event_print_finished) in the "correct" format? I realised that I haven't changed it from its long string id to a neat entity id like the others, but not sure how I can find the exact device id for the printer to replace that long string with?
  2. Are you sure that the wait_for_trigger condition is going to wait until BOTH sensor.p1s_nozzle_temperature and sensor.p1s_bed_temperature are less than 50?

It's just that when I save the yaml from my last message and then switch to the visual editor, it says that the wait for trigger will wait for when nozzle temperature OR bed temperature are below 50, whereas what I want is for nozzle temp AND bed temp to be < 50?

If it's currently an or condition, would I need to add two separate wait for triggers that run sequentially (effetively making them a logical AND)?

lime vector
#

As for the wait_for_trigger being an and condition. You can actually do that with a wait_for_template instead. The template would look like {{ (states('sensor.p1s_nozzle_temperature')|int(0)) > 50 and (states('sensor.p1s_bed_temperature')|int(0)) > 50 }}

elfin hound
lime vector
#

All states are strings.

#

So, to perform numeric value checking, you have to convert to float or int. the (0) is in case the state is unknown or unavailable.

elfin hound
#

Ah, I see

#

And could I also leave the argument inside the int (the (0)) blank since the default fallback value for the filter if it can't convert is 0 anyway right, or am I mistaken?

lime vector
#

You could, sure. But I just like to specify it in case I want to use some other default value.

elfin hound
#

Right

elfin hound
#

@lime vector I tried my new yaml, but unfortunately it seems to get stuck and not actually trigger the light - any idea what's going wrong?

alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished
action:
  - wait_template: >-
      {{ (states('sensor.p1s_nozzle_temperature')|float) < 50 and
      (states('sensor.p1s_bed_temperature')|float) < 50 }}
    timeout:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: light.turn_off
    target:
      entity_id: light.p1s_chamber_light
    data: {}
mode: restart
#

I would upload the trace file but annoyingly this discord server doesn't allow uploading files unlike most ones

loud sphinxBOT
#

Please use a code share site to share code or logs, for example:

Please don't use Pastebin, since it can randomly add spaces to the main view. Please also don't share text as images since it makes it harder for people to help you. Remember that others may have colour blindness, impaired vision, etc.

lime vector
#

Let me take a look real quick.

#

The YAML looks fine. So it might be the wait template.

elfin hound
lime vector
#

I can’t test the wait template (I’m mobile ATM), but I’m fairly certain that is where the error is. Can you test it in devtools > template? Also, you have it set to restart mode. I’d keep that single so it doesn’t affect the wait template.

elfin hound
#

I don't know why the value would be unavailable right after the print

#

Hmm I just checked and there was a momentary drop of the value in the history graph to 0, so ig it means it was unavailable for that moment

#

This value loss is only for a few secs as well, so maybe I could just add a 15 secs wait to the automation before checking for those 2 values?

lime vector
#

That’s why the (0) should be there. 😊

elfin hound
lime vector
#

i was actually just thinking about that. Maybe default to something like 100?

#

lol yup. GMTA

#

You got this...

elfin hound
# lime vector i was actually just thinking about that. Maybe default to something like 100?

Unfortunately I tried this and it still failed. If I instead add a delay of 15 secs between the initial trigger and the wait_template, then it does work because it skips over the "blackout" period where the printer stops sending values or something, and so by the time the wait_template is active, it is receiving valid data again.

alias: Turn off P1S light at end of print
description: ""
trigger:
  - platform: device
    device_id: e88295fc70cd6406760494fd0fef2cc0
    domain: bambu_lab
    type: event_print_finished
action:
  - delay: "00:00:15"
  - wait_template: >-
      {{ (states('sensor.p1s_nozzle_temperature')|float(100.0)) < 50 and
      (states('sensor.p1s_bed_temperature')|float(100.0)) < 50 }}
    timeout:
      hours: 1
    continue_on_timeout: false
  - service: light.turn_off
    target:
      entity_id: light.p1s_chamber_light
    data: {}
mode: restart
#

Idk why this works and the float(100.0) doesn't just take care of the unavailable values

lime vector
#

Huh, I guess that makes sense? Could also add a “default(100)” in as well. But that’s weird. Glad it works though.

elfin hound
# lime vector Huh, I guess that makes sense? Could also add a “default(100)” in as well. But t...

Thanks for the new suggestion, I just tried it but unfortunately it doesn't work:

{{ (states('sensor.p1s_nozzle_temperature')|default(100)|float(100)) < 50
      and (states('sensor.p1s_bed_temperature')|default(100)|float(100)) < 50 }}

Seems like it might be a bug to do with how HA handles these "unavailable" values during the blackout periods perhaps?

So it seems like I'll stick with the delay workaround for now as that seems to work.

lime vector
#

But yeah, I think that short delay is probably best as well.

elfin hound
lime vector
#

It does, but I cannot for the life of me get it to return the value properly.

#

Oh wait... I see why... damn I can be dense sometimes. The state string is coming back as unknown, which is a value. Probably need to do something like test if it's defined? Not sure tbh. #templates-archived would know better.