Switch template with duration

I used to have a rainbird irrigation controller but now I switched to a DIY relay switch.

My code was as follows before:

irrig_front_yard:

        friendly_name: "Front yard"

        value_template: "{{ is_state('sprinkler_zone_2', 'on') }}"

        turn_on:

          service: rainbird.start_irrigation

          data_template:

            entity_id: switch.sprinkler_zone_2

            duration: "{{ states('input_number.irrigation_frontyardduration') | int }}"

        turn_off:

          service: switch.turn_off

          data:

            entity_id: switch.sprinkler_zone_2

but now it is the following but it doesn’t work (changed service: rainbird.start_irrigation to service: switch.turn_on):

irrig_front_yard:

        friendly_name: "Front yard"

        value_template: "{{ is_state('sprinkler_zone_2', 'on') }}"

        turn_on:

          service: switch.turn_on

          data_template:

            entity_id: switch.sprinkler_zone_2

            duration: "{{ states('input_number.irrigation_frontyardduration') | int }}"

        turn_off:

          service: switch.turn_off

          data:

            entity_id: switch.sprinkler_zone_2

I get this error:
rainbirderror

I want the irrigation switch to be on until the time reaches the slider defined time.

The switch turn on service does not have a duration option. That was only an option for your rainbird service.

You will have to write a script or automation for this.

Is there an example i can take a look at?

trigger:
  - platform: state
    entity_id: switch.irrig_front_yard
    from: 'off'
    to: 'on'
    for: 
      minutes: "{{ states('input_number.irrigation_frontyardduration') | int }}"
action:
  - service: switch.turn_off
    entity_id: switch.irrig_front_yard
1 Like

just perform the actions

        turn_on:
        - service: switch.turn_on
          data:
            entity_id: switch.sprinkler_zone_2
        - delay:
            minutes: "{{ states('input_number.irrigation_frontyardduration') | int }}"
        - service: switch.turn_off
          data:
            entity_id: switch.sprinkler_zone_2
2 Likes