Delay doesen't work

Hello, I have made an installation in our chicken house, where i installed a cam which is connected to home assitant. Every Evening at a certain time it automatically takes a picture from the chickens and send it to my phone. I can also trigger the automation with a button. So I can check if everithing is allright with them. Bevore the picture is taken, i switch on a light and then wait for 3s and then take the picture.

Unfortunately I have big problems with the ‘delay’. Sometimes the delay ist correct, but most of the time the picture ist taken right after I trigger the sequence is triggered. And then of course no light is on!

Here is my sequence:

'1594453443792':
  alias: CheckChicken
  sequence:
  - data: {}
    entity_id: light.chicken_house
    service: light.turn_on
  - data:
      entity_id: light.chicken_house
      mode: normal
    service: yeelight.set_mode
  - data:
      brightness: 50
      entity_id: light.chicken_house
      rgb_color:
      - 190
      - 158
      - 104
    service: yeelight.set_color_scene
  - delay: 00:00:05
  - condition: state
    entity_id: light.chicken_house
    state: 'on'
  - data:
      filename: /config/www/photos/snapshot_chicken.jpg
    entity_id: camera.aithinkercam
    service: camera.snapshot
  - delay: 00:00:05
  - data: {}
    entity_id: light.chicken_house
    service: light.turn_off
  mode: single
  icon: mdi:bird

Has somebody an idea, why the delay is working so bad?

I thought with the new release this problem is gone, but thats not true.

Thank you.

Put the delay in quotes… it’s seeing your delay as 00.

delay: '00:00:05'

That shouldn’t matter.

@al.ba.see I suspect the problem may have to do more with the potential variability in the camera.snapshot service. If you look in home-assistant.log it should tell you exactly how the script ran including timing details. E.g., you should see something like this:

2020-08-08 08:52:58 INFO (MainThread) [homeassistant.components.script.test] test: Running script
2020-08-08 08:52:58 INFO (MainThread) [homeassistant.components.script.test] test: Executing step event1
2020-08-08 08:52:58 INFO (MainThread) [homeassistant.components.script.test] test: Executing step delay 0:00:05
2020-08-08 08:53:03 INFO (MainThread) [homeassistant.components.script.test] test: Executing step event 2

I just checked that. Unfortunately this was not the solution. The behaviour ist strange: The light itself follows the delay, which means that after 10" the light turns off - everithing works well with the light.
BUT the CAM snapshot ist triggered right after the script is triggered. Why does the camera.snapshot not wait?

To be clear, I suspect the snapshot is taking longer than the 5 seconds you are giving it to finish. Try increasing the delay after the camera.snapshot service call.

And, BTW, you can write the delay like this:

- delay: 10

If you just give a number it is interpreted as seconds.

EDIT: Although, by the looks of it, I’m guessing you’ve created the script in the UI editor. :wink:

OK, I’m going to try that. BTW here is the log. But I don’t understand what there should be wrong?

2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.automation] Executing CheckChicken+
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.automation] CheckChicken+: Running script
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.automation] CheckChicken+: Executing step call service
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Running script
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
2020-08-08 16:11:02 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step delay 0:00:05
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Test condition state: True
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step delay 0:00:05
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.automation] Executing Folder Watcher
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.automation] Folder Watcher: Running script
2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.automation] Folder Watcher: Executing step call service
2020-08-08 16:11:08 INFO (MainThread) [homeassistant.components.mobile_app.notify] mobile_app push notification rate limits for al.ba.see.1731.iph11: 14 sent, 300 allowed, 0 errors, resets in 9:48:51
2020-08-08 16:11:12 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service

Hmm, still not working.
I set now the second delay to 15". But the camera.snaphot service is still triggered right after the whole script is triggered. Why does this service not wait for the delay?

I mean how can I create a sequence with actions an delays when some services do things like they want to?

Sorry but the camera snapshot is not starting before the first delay. Look at your log:

script starting          --> 2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Running script
light.turn_on            --> 2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
yeelight.set_mode        --> 2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
yeelight.set_color_scene --> 2020-08-08 16:11:01 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
delay: 00:00:05          --> 2020-08-08 16:11:02 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step delay 0:00:05
condition: state         --> 2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Test condition state: True
>>> camera.snapshot <<<  --> 2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service
delay: 00:00:05          --> 2020-08-08 16:11:07 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step delay 0:00:05
light.turn_off           --> 2020-08-08 16:11:12 INFO (MainThread) [homeassistant.components.script.1594453443792] CheckChicken: Executing step call service

If you want to convince yourself, set the logger this way:

logger:
  default: info
  logs:
    homeassistant.core: debug

Then you’ll be able to see all the gory detail of each service call, etc.

EDIT Alternatively, you could add an alias to the camera snapshot service call, and then you can see that alias in the log (instead of “call service”.) E.g., change this:

  - data:
      filename: /config/www/photos/snapshot_chicken.jpg
    entity_id: camera.aithinkercam
    service: camera.snapshot

to this:

  - alias: Take camera snapsot
    data:
      filename: /config/www/photos/snapshot_chicken.jpg
    entity_id: camera.aithinkercam
    service: camera.snapshot

You are absolutly right! But the strange thing is, that the photo above was taken at 16:11.01! altough the service camera.snapshot ist triggered at 16:11.07!? (same time, when the script ist starting) It looks like something others is triggering the camera.snapshot.

Where do you see that? Could it be that the timestamp on the photo is using a different clock than HA? Unless they are set precisely the same, that could explain it.

My guess would be a delay in the part before the first script delay. What hardware is involved in all this?

I don’t have a yeelight myself, but I do had some delays with other lamps, before I invested a little money into my hardware.

I would start the following test: go to the chicken house and wait for the script to start or set a specific time, that you can check from the chicken house. I wouldn’t use a manual trigger, because that could change the outcome. See, how long it takes HA to send the signal to the yeelight and how long the yeelight takes to really get that signal and turn on.

And if nothing of this works, put another step before the first script delay, to check if the light rally is on. :slight_smile:

Thanks a lot for your help. For the moment I let this. I have probably a workarround:

I made two scripts. In the first script I turn the light on, wait for 5" and then I start the second script with the camera.snapshot. I do this twice, because the first picture always got a content from the time i trigger the FIRST script. I can check this very accurate - belive me. I think there is a old frame in the buffer of the cam or something like this. The second picture looks good so far.

My hardware is an AI Thinker Cam connected via ESPHome.

All in all I love Home Assistant and ESPHome very much!

'1594453443792':
  alias: CheckChickenLightOn
  sequence:
  - data: {}
    entity_id: light.chicken_house
    service: light.turn_on
  - data:
      entity_id: light.chicken_house
      mode: normal
    service: yeelight.set_mode
  - data:
      brightness: 50
      entity_id: light.chicken_house
      rgb_color:
      - 190
      - 158
      - 104
    service: yeelight.set_color_scene
  - delay: 5  
  - data: {}
    entity_id: script.checkchickencam
    service: script.turn_on
  mode: single


checkchickencam:
  alias: CheckChickenCam
  mode: single
  sequence:
  - data:
      filename: /config/www/photos/snapshot_chicken.jpg
    entity_id: camera.aithinkercam
    service: camera.snapshot
  - delay: 1  
  - data:
      filename: /config/www/photos/snapshot_chicken.jpg
    entity_id: camera.aithinkercam
    service: camera.snapshot

Could it be that the turn on of the lights trigger a motion sensor in the camera to take a picture or something like that?