Problem with script

I have a problem with the following script.

script:
  tv_turn_on:
    sequence:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.tv_power_status
      - service: timer.start
        entity_id: timer.tv_delay
      - service: switch.turn_on
        entity_id: switch.av_sclx87_pwr
      - delay: 2000 ms
      - service: switch.turn_on
        entity_id: switch.tv_samsung_pwr

switch.av_sclx87_pwr will switch on but switch.tv_samsung_pwr won’t switch on

If I change the order then the samsung_tv will turn on but the sclx87 won’t.

Only the first switch in the script seems to be able to turn on and the second one doesn’t.

Here is the code in it’s entirety

binary_sensor: 
  - platform: ping
    name: tv_ping
    host: samsung_tv.local
    scan_interval: 2
    count: 2
    
input_boolean: #temporary store state of the TV while waiting for ping to update.
  tv_power_status:
    name: TV Power Status
    icon: mdi:television-classic
    
timer: #ignore tv_ping powerstate for XX seconds.
  tv_delay:
    duration: '00:00:10'
    
switch:

    # Broadlink RM Mini3
  - platform: broadlink
    host: 192.168.1.18
    mac: '34:EA:34:58:AA:65'
    timeout: 15
    switches:    
      av_sclx87_pwr:
        friendly_name: "Pioneer AV Power"
        command_on: 'JgBIAAABJpUTNhQREzYTEREUEDkQFBE4ERQSNxISETkQORISETgRFBETETgRExQ2EzYRExITEBQQORETETkRExISETkQORI3EQANBQ==='
        command_off: 'JgBIAAABJZYROBETETkRExETETkPFRE4ERMRORETETgRORETETgRExE5EDkRExE5EDkRExETExIRExETETgRFBETETgRORE4EQANBQ==='
      av_sclx87_vol:
        friendly_name: "Pioneer AV Volume"
        command_on: 'JgBIAAABJZYSNxETETkRExETETkPFRE4ERMRORETETgSNxEUETgRExEUEDkRExI3EhMSEhETERMRORETEzYRFBA5ETgROBI4EQANBQ==='
        command_off: 'JgBIAAABJZYROBISEzcRExETETkQFBE4ERQQORETETgRORETETgRExE5ETgRExE5EBQRExETERQPFRETETgRFBE4ETgRORE4EQANBQ==='        
      av_sclx87_htpc:
        friendly_name: "Pioneer AV HTPC Input"
        command_on: 'JgCQAAABJpUTNxISEjcRFBETETgQFBE5ERMROBEUEDkRNxITETkRExETERMTNxI3EjcRFBE4ExESOBI3ExERExEUEDkRExE4EQADLQABJ5QRORETETgRExEUEzYTERM3ExESNxETEDoQORISETkQFBISERMRFBAUExETERI4ETgTNhM3EzYTNhM2FDYTERMREgANBQAAAAAAAAAA===='
      av_sclx87_cd:
        friendly_name: "Pioneer AV CD Input"
        command_on: 'JgBIAAABKJMUNRQREzYTERMSEzYSEhM2FBETNRQREzYUNhMREzYUERMRExEUNRQ0FRETERM2FBESNxM2FBETERM2EzcTERM2EwANBQ=='
        
        
    # Broadlink RM Pro
  - platform: broadlink 
    host: 192.168.1.117
    mac: '78:0F:77:00:DE:68'
    timeout: 15
    switches:    
      tv_samsung_pwr:
        friendly_name: "Samsung Tv Power"
        command_on: 'JgBGAJGVETgSOBE4ERQRFBITERQRFBI4ETgSOBAVERQSExITEhMROREUERQROBI4ERQQFRE4EhMSOBE4EhMSExI4ETgSExIADQUAAA==='
        command_off: 'JgBGAJWTEzYTNhM3ExESEhISExITERM2EzcTNhMREhITEhMREhISEhMSExETNhM3ExESEhM2EzcTNhM2ExITERI3EjgTERIADQUAAA==='
      tv_samsung_hdmi2:
        friendly_name: "Samsung TV HDMI2"
        command_on: 'JgBGAJGXDjoQORE5DxYPFQ8VDxUPFRA5ETkPOhETDxYPFQ8VDxYPFA87DzoPOhE4EDsPFQ86DzkRFQ8UEBUPFRAUDzsPGAwADQUAAA==='
     
  - platform: template
    switches:
      tvpower:
        friendly_name: Television
        value_template: >-
          {% if is_state('timer.tv_delay', 'idle') %}
          {{ states("binary_sensor.tv_ping") }}
          {% else %}
          {{ states("input_boolean.tv_power_status") }}
          {% endif %}
        turn_on:
          - service: homeassistant.turn_off
            entity_id: script.tv_turn_off
          - service: script.turn_on
            entity_id: script.tv_turn_on
        turn_off:
          - service: homeassistant.turn_off
            entity_id: script.tv_turn_on
          - service: script.turn_on
            entity_id: script.tv_turn_off
        icon_template: mdi:television-classic
 

script:
  tv_turn_on:
    sequence:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.tv_power_status
      - service: timer.start
        entity_id: timer.tv_delay
      - service: switch.turn_on
        entity_id: switch.av_sclx87_pwr
      - delay: 2000 ms
      - service: switch.turn_on
        entity_id: switch.tv_samsung_pwr
  tv_turn_off:
    sequence:
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.tv_power_status
      - service: timer.start
        entity_id: timer.tv_delay
      - service: switch.turn_off
        entity_id: switch.tv_samsung_pwr
      - delay: 500 ms
      - service: switch.turn_off
        entity_id: switch.av_sclx87_pwr
      - delay: 500 ms
      - service: switch.turn_off
        entity_id: switch.tv_samsung_pwr            
      - delay: 500 ms
      - service: switch.turn_off
        entity_id: switch.av_sclx87_pwr

The problem was

- delay 2000 ms

should be

- delay '00:00:02'

The error doesn’t show up in ‘check config’ in the UI, but it causes the script to abort part way through.

If you want to specify milliseconds, the proper way is:

- delay:
    milliseconds: 2000

But please be aware that delays are implemented by monitoring time_changed events which happen only once every second. So if, e.g., you try to delay for 500 ms, the delay might be anywhere from 500 to just under 1500 ms.

1 Like