Issues with Template Switch

I created Template Switch and Sensor to control the device with two Restful commands. Sensor works with 10-30 seconds delay of actual device state.

If I click turn on the switch, turn_on command work fine, but after few second switch runs into turn off state. After 10-30 seconds sensor became on, but switch’s state is still off. I expected that switch’s state will be the same as sensor state. What’s wrong?

sensor:
  - platform: rest
    name: pearl2_rec_enabled
    resource: http://192.168.88.4/admin/channelm1/get_params.cgi?rec_enabled
    username: xxx
    password: xxx
    value_template: "{{ 'on' in value }}"
    force_update: true

  - platform: template
    switches:
      pearl2_rec:
        value_template: "{{ is_state('sensor.pearl2_rec_enabled', 'true') }}"
        turn_on:
          service: rest_command.pearl2_rec_on
        turn_off:
          service: rest_command.pearl2_rec_off

rest_command:
  pearl2_rec_on:
    url: 'http://192.168.88.4/admin/channelm1/set_params.cgi?rec_enabled=on'
    username: xxx
    password: xxx
    
  pearl2_rec_off:
    url: 'http://192.168.88.4/admin/channelm1/set_params.cgi?rec_enabled=""' 
    username: xxx
    password: xxx

the state will be on/off it true/false is being returned to the sensor. Also, I recommend just making that a binary sensor instead of sensor.